// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Input { /// /// Object that represents a cursor comprised of sprites and colors for each state /// [AddComponentMenu("Scripts/MRTK/SDK/SpriteCursor")] public class SpriteCursor : BaseCursor { [Serializable] public struct SpriteCursorDatum { public string Name; public CursorStateEnum CursorState; public Sprite CursorSprite; public Color CursorColor; } [SerializeField] public SpriteCursorDatum[] CursorStateData; /// /// Sprite renderer to change. If null find one in children /// public SpriteRenderer TargetRenderer; /// /// On enable look for a sprite renderer on children /// protected override void OnEnable() { if (CursorStateData == null) { CursorStateData = Array.Empty(); } if (TargetRenderer == null) { TargetRenderer = GetComponentInChildren(); } base.OnEnable(); } /// /// Override OnCursorState change to set the correct sprite /// state for the cursor /// public override void OnCursorStateChange(CursorStateEnum state) { base.OnCursorStateChange(state); if (state != CursorStateEnum.Contextual) { for (int i = 0; i < CursorStateData.Length; i++) { if (CursorStateData[i].CursorState == state) { SetCursorState(CursorStateData[i]); } } } } /// /// Based on the type of state info pass it through to the sprite renderer /// private void SetCursorState(SpriteCursorDatum stateDatum) { // Return if we do not have an animator if (TargetRenderer != null) { TargetRenderer.sprite = stateDatum.CursorSprite; TargetRenderer.color = stateDatum.CursorColor; } } } }