// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using Microsoft.MixedReality.Toolkit.Input; using System.Collections.Generic; using UnityEngine; namespace Microsoft.MixedReality.Toolkit.UI { /// /// Example of building an event system for Interactable that still uses ReceiverBase events /// [AddComponentMenu("Scripts/MRTK/SDK/InteractableReceiver")] public class InteractableReceiver : ReceiverBaseMonoBehavior { // list of events added to this interactable [HideInInspector] public List Events = new List(); protected virtual void Awake() { SetupEvents(); } /// /// set up only one event /// protected virtual void SetupEvents() { if (Events.Count > 0) { Events[0].Receiver = InteractableEvent.CreateReceiver(Events[0]); Events[0].Receiver.Host = this; } } /// /// A state has changed /// public override void OnStateChange(InteractableStates state, Interactable source) { base.OnStateChange(state, source); if (Events.Count > 0) { if (Events[0].Receiver != null) { Events[0].Receiver.OnUpdate(state, source); } } } /// /// click happened /// public override void OnClick(InteractableStates state, Interactable source, IMixedRealityPointer pointer = null) { base.OnClick(state, source, pointer); if (Events.Count > 0) { if (Events[0].Receiver != null) { Events[0].Receiver.OnClick(state, source, pointer); } } } /// /// voice command happened /// public override void OnVoiceCommand(InteractableStates state, Interactable source, string command, int index = 0, int length = 1) { base.OnVoiceCommand(state, source, command, index, length); if (Events.Count > 0) { if (Events[0].Receiver != null) { Events[0].Receiver.OnVoiceCommand(state, source, command, index, length); } } } } }