// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Input { /// /// The object cursor can switch between different game objects based on its state. /// It simply links the game object to set to active with its associated cursor state. /// [AddComponentMenu("Scripts/MRTK/SDK/ObjectCursor")] public class ObjectCursor : BaseCursor { [Serializable] public struct ObjectCursorDatum { public string Name; public CursorStateEnum CursorState; public GameObject CursorObject; } [SerializeField] public ObjectCursorDatum[] CursorStateData; /// /// Sprite renderer to change. If null find one in children /// public Transform ParentTransform; /// /// On enable look for a sprite renderer on children /// protected override void OnEnable() { if (ParentTransform == null) { ParentTransform = transform; } base.OnEnable(); } /// /// Override OnCursorState change to set the correct animation /// state for the cursor /// public override void OnCursorStateChange(CursorStateEnum state) { base.OnCursorStateChange(state); if (state != CursorStateEnum.Contextual) { // Hide all children first for (int i = 0; i < ParentTransform.childCount; i++) { ParentTransform.GetChild(i).gameObject.SetActive(false); } // Set active any that match the current state for (int i = 0; i < CursorStateData.Length; i++) { if (CursorStateData[i].CursorState == state) { CursorStateData[i].CursorObject.SetActive(true); } } } } } }