Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Collections;
- using System.Collections.Generic;
- using UnityEngine;
- using UnityEngine.XR.MagicLeap;
- using UnityEngine.UI;
- public class GestureScript : MonoBehaviour
- {
- private bool OKHandPoseDetected = false;
- private float speed = 30.0f; // Speed of our cube
- private float distance = 2.0f; // Distance between Main Camera and Cube
- private GameObject cube; // Reference to our Cube
- private List<MLHandKeyPose> gestures; // Holds the different gestures we will look for
- private static bool awakened = false;
- // The default DirectionalLight only illuminates half the scene. Objects
- // that fall in the other half do not get lit and appear invisible in
- // additive HMDs. This boolean will auto-add a Point Light at the center
- // of the scene to help with global illumination, but set it to false if
- // the Scene already contains proper lighting.
- //
- public bool CreatePointLight = true;
- void Awake()
- {
- if (awakened)
- {
- ShowMessage("Multiple Gesture Scripts detected. Please only use one. This one is on GameObject \"" + name + "\"");
- return;
- }
- awakened = true;
- if (Camera.main == null)
- {
- ShowMessage("No MainCamera object in the scene. Please replace the scene camera with the \"Main Camera\" prefab from " +
- "Assets\\MagicLeap\\CoreComponents");
- return;
- }
- if (Camera.main.GetComponent<UnityEngine.XR.MagicLeap.Rendering.MagicLeapCamera>() == null)
- {
- ShowMessage("No MagicLeapCamera component attached to the Main Camera. Please replace the scene camera with the \"Main Camera\" prefab from " +
- "Assets\\MagicLeap\\CoreComponents");
- return;
- }
- // First find and hide the cube
- cube = GameObject.Find("Cube");
- if (cube == null)
- {
- ShowMessage("Could not find GameObject named \"Cube\". Please add a " +
- "3d model to the hierarchy and name it \"Cube\" (no quotes)");
- return;
- }
- cube.SetActive(false);
- if (CreatePointLight)
- {
- GameObject pointLight = new GameObject("Light");
- pointLight.AddComponent<Light>();
- }
- // Then verify gestures are available
- if (!MLHands.Start().IsOk)
- {
- //#if !UNITY_EDITOR
- ShowMessage("Gesture permissions not granted. Please relocate the manifest from the default location " +
- "Assets\\MagicLeap\\Examples\\Plugins\\Lumin\\manifest.xml to Assets\\Plugins\\Lumin\\manifest.xml");
- //#endif
- return;
- }
- gestures = new List<MLHandKeyPose>();
- // To recognize more gestures, add to this list.
- gestures.Add(MLHandKeyPose.Ok);
- gestures.Add(MLHandKeyPose.Fist);
- gestures.Add(MLHandKeyPose.OpenHandBack);
- gestures.Add(MLHandKeyPose.Finger);
- gestures.Add(MLHandKeyPose.L);
- MLHands.KeyPoseManager.EnableKeyPoses(gestures.ToArray(), true, false);
- ShowMessage("Ready for OK Gesture");
- }
- private void OnDisable()
- {
- if (this.name.Equals("Cube"))
- {
- ShowMessage("Please do not put this script directly on the Cube object.");
- }
- }
- void OnDestroy()
- {
- if (MLHands.IsStarted)
- {
- MLHands.Stop();
- }
- }
- void Update()
- {
- // Do nothing if the cube does not exist
- if (cube == null)
- {
- return;
- }
- // If the OK HandPose was detected, check for pose modifiers
- if (OKHandPoseDetected)
- {
- // Open Hand Back (Shift-B in Editor): Either hand rotates counter-clockwise around the Y-Axis.
- if ((GetLeftHandGesture(MLHandKeyPose.OpenHandBack) || GetRightHandGesture(MLHandKeyPose.OpenHandBack)))
- {
- cube.transform.Rotate(Vector3.up, +speed * Time.deltaTime);
- }
- // Fist (Shift-G in Editor): Either hand rotates clockwise around the Y-Axis.
- if (GetLeftHandGesture(MLHandKeyPose.Fist) || GetRightHandGesture(MLHandKeyPose.Fist))
- cube.transform.Rotate(Vector3.up, -speed * Time.deltaTime);
- // Finger (LShift-F in Editor): Left hand rotates down under the X-Axis.
- if (GetLeftHandGesture(MLHandKeyPose.Finger))
- cube.transform.Rotate(Vector3.right, +speed * Time.deltaTime);
- // Finger (RShift-F in Editor): Right hand rotates up over the X-Axis.
- if (GetRightHandGesture(MLHandKeyPose.Finger))
- cube.transform.Rotate(Vector3.right, -speed * Time.deltaTime);
- // Add other responses here as desired, making sure to add their recognition in Awake()
- // L with both hands resets the rotation
- if ((GetLeftHandGesture(MLHandKeyPose.L) && GetRightHandGesture(MLHandKeyPose.L)))
- {
- cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward * distance;
- cube.transform.rotation = Camera.main.transform.rotation;
- }
- }
- else
- {
- if (GetLeftHandGesture(MLHandKeyPose.Ok) || GetRightHandGesture(MLHandKeyPose.Ok))
- {
- OKHandPoseDetected = true;
- cube.SetActive(true);
- cube.transform.position = Camera.main.transform.position + Camera.main.transform.forward * distance;
- cube.transform.rotation = Camera.main.transform.rotation;
- if (_canvasObject != null)
- {
- _canvasObject.SetActive(false);
- }
- }
- }
- }
- bool GetLeftHandGesture(MLHandKeyPose type)
- {
- return GetGesture(false, type);
- }
- bool GetRightHandGesture(MLHandKeyPose type)
- {
- return GetGesture(true, type);
- }
- bool GetGesture(bool handIsRight, MLHandKeyPose type)
- {
- #if UNITY_EDITOR
- if (GetEditorGesture(handIsRight, type))
- {
- return true;
- }
- #endif
- if (!MLHands.IsStarted)
- {
- return false;
- }
- MLHand hand = handIsRight ? MLHands.Right : MLHands.Left;
- if (hand != null)
- {
- if (hand.KeyPose == type)
- {
- if (hand.KeyPoseConfidence > 0.9f)
- {
- return true;
- }
- }
- }
- return false;
- }
- /* Only runs in the Unity Editor as a way to test code prior to headset deployment.
- *
- * Allows you to hold left shift and type in a corresponding keycode to implement
- * a left-hand gesture, or right shift to do that gesture with the right hand.
- *
- * Hold both shift keys to do the same gesture with both hands.
- *
- * Note that this example does not allow for both hands to be doing different
- * identified key poses in the editor, but the deployed app will be able to do
- * so on the headset.
- */
- bool GetEditorGesture(bool handIsRight, MLHandKeyPose type)
- {
- #if UNITY_EDITOR
- if (!handIsRight && !Input.GetKey(KeyCode.LeftShift))
- {
- return false;
- }
- if (handIsRight && !Input.GetKey(KeyCode.RightShift))
- {
- return false;
- }
- switch (type)
- {
- case MLHandKeyPose.C:
- return Input.GetKey(KeyCode.C);
- case MLHandKeyPose.Finger:
- return Input.GetKey(KeyCode.F);
- case MLHandKeyPose.Fist:
- return Input.GetKey(KeyCode.G);
- case MLHandKeyPose.L:
- return Input.GetKey(KeyCode.L);
- case MLHandKeyPose.NoPose:
- return Input.GetKey(KeyCode.X);
- case MLHandKeyPose.Ok:
- return Input.GetKey(KeyCode.O);
- case MLHandKeyPose.OpenHandBack:
- return Input.GetKey(KeyCode.B);
- case MLHandKeyPose.Pinch:
- return Input.GetKey(KeyCode.P);
- case MLHandKeyPose.Thumb:
- return Input.GetKey(KeyCode.T);
- }
- #endif
- return type == MLHandKeyPose.NoHand;
- }
- /* The below manages errors by dynamically constructing a canvas
- * to display an error to the user when wearing the Leap.
- *
- * It can be ignored for the purpose of this lesson.
- */
- GameObject _canvasObject;
- GameObject CanvasObject
- {
- get
- {
- if (_canvasObject == null)
- {
- Vector3 forward = Camera.main.transform.forward;
- forward.y = 0;
- _canvasObject = new GameObject("World Canvas");
- Canvas canvas = _canvasObject.AddComponent<Canvas>();
- canvas.renderMode = RenderMode.WorldSpace;
- canvas.GetComponent<RectTransform>().position = forward.normalized * 2;
- canvas.GetComponent<RectTransform>().sizeDelta = new Vector2(840, 600);
- canvas.GetComponent<RectTransform>().localScale = Vector3.one * .001f;
- canvas.transform.SetParent(Camera.main.transform, false);
- RawImage background = canvas.gameObject.AddComponent<RawImage>();
- background.color = new Color32(20, 20, 20, 128);
- GameObject textObject = new GameObject("Text");
- Text text = textObject.AddComponent<Text>();
- text.GetComponent<RectTransform>().sizeDelta = new Vector2(720, 480);
- text.color = Color.white;
- text.resizeTextForBestFit = true;
- text.resizeTextMaxSize = 48;
- text.resizeTextMinSize = 24;
- text.alignment = TextAnchor.MiddleLeft;
- text.font = Resources.GetBuiltinResource(typeof(Font), "Arial.ttf") as Font;
- textObject.transform.SetParent(canvas.transform, false);
- }
- return _canvasObject;
- }
- }
- void ShowMessage(string message)
- {
- CanvasObject.GetComponentInChildren<Text>().text = message;
- CanvasObject.SetActive(true);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement