Last active
April 15, 2020 20:10
-
-
Save DanMillerDev/1fe9a386cd61ad03925e917868db4409 to your computer and use it in GitHub Desktop.
Unity ARKit Blend shape object activator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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