Change FacePrefab in ARFaceManager programmatically

Hi!

I just started learning AR and started with face masks. I managed to add a mask, but I don't understand how to change it in runtime.

I wrote this code to change the mask, but when I call the "ChangeFaceMask" method, I see that it has changed in ArFaceManager (arFaceManager.facePrefab), but the mask does not change visually.

What am I doing wrong?

public class FaceMaskHelper : MonoBehaviour
{
    public GameObject[] faceMasks;
    ARFaceManager arFaceManager;

    void Start()
    {
        arFaceManager = gameObject.GetComponent<ARFaceManager>();
    }

    void Update()
    {

    }

    void ChangeFaceMask(String maskName)
    {
        for (int i = 0; i < faceMasks.Length; i++)
        {
            GameObject go = faceMasks[i];

            if (go.name == maskName)
            {
                arFaceManager.facePrefab = go;
                break;
            }
        }
    }
}

I solved the problem with dynamically changing the mask in runtime. It turns out that you need to change the material at ARFace, and not the prefab at ARFaceManager.

[RequireComponent(typeof(ARFaceManager))]
public class FaceMaskHelper : MonoBehaviour
{
    public Material[] faceMasks;

    private ARFaceManager arFaceManager;

    void Start()
    {
        arFaceManager = gameObject.GetComponent<ARFaceManager>();
    }

    void Update()
    {

    }

    void ChangeFaceMask(String faceMaskName)
    {
        for (int i = 0; i < faceMasks.Length; i++)
        {
            Material faceMaskMaterial = faceMasks[i];

            if (faceMaskMaterial.name == faceMaskName)
            {
                foreach (ARFace arFace in arFaceManager.trackables)
                {
                    arFace.GetComponent<MeshRenderer>().material = faceMaskMaterial;
                }

                break;
            }
        }
    }
}
1 Like