Skip to content

Instantly share code, notes, and snippets.

@DanMillerDev
Last active April 15, 2020 20:10
Show Gist options
  • Save DanMillerDev/1fe9a386cd61ad03925e917868db4409 to your computer and use it in GitHub Desktop.
Save DanMillerDev/1fe9a386cd61ad03925e917868db4409 to your computer and use it in GitHub Desktop.
Unity ARKit Blend shape object activator
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.XR.iOS;
public class BlendShapeObjectSwitcher : MonoBehaviour {
private static float THRESHOLD = 0.4f;
bool enabled = false;
Dictionary<string, float> currentBlendShapes;
float blendshapeValue = 0.0f;
public string BlendShapeName = "";
public GameObject ToggleObj;
// Use this for initialization
void Start () {
UnityARSessionNativeInterface.ARFaceAnchorAddedEvent += FaceAdded;
UnityARSessionNativeInterface.ARFaceAnchorUpdatedEvent += FaceUpdated;
UnityARSessionNativeInterface.ARFaceAnchorRemovedEvent += FaceRemoved;
}
void FaceAdded (ARFaceAnchor anchorData)
{
enabled = true;
currentBlendShapes = anchorData.blendShapes;
}
void FaceUpdated (ARFaceAnchor anchorData)
{
currentBlendShapes = anchorData.blendShapes;
}
void FaceRemoved (ARFaceAnchor anchorData)
{
enabled = false;
}
// Update is called once per frame
void Update ()
{
if(enabled)
{
if(currentBlendShapes.ContainsKey(BlendShapeName))
{
blendshapeValue = currentBlendShapes[BlendShapeName];
if(blendshapeValue >= THRESHOLD)
{
ToggleObj.SetActive(true);
}
else
{
ToggleObj.SetActive(false);
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment