// Copyright (c) Microsoft Corporation. // Licensed under the MIT License using UnityEngine; namespace Microsoft.MixedReality.Toolkit.Experimental.InteractiveElement.Examples { /// /// Contains examples on how to add states with event listeners to an Interactive Element. /// public class InteractiveElementRuntimeExample : MonoBehaviour { private InteractiveElement interactiveElement; void Start() { interactiveElement = gameObject.AddComponent(); // Interactive Element is initialized with the Default and Focus state AddStateActivatedListeners(); AddFocusFarState(); AddSelectFarState(); CreateNewState(); } /// /// Add State Activated and State Deactivated event listeners. /// public void AddStateActivatedListeners() { interactiveElement.StateManager.OnStateActivated.AddListener((state) => { Debug.Log($"{state.Name} is now active"); }); interactiveElement.StateManager.OnStateDeactivated.AddListener((stateDeactivated, activeState) => { Debug.Log($"{stateDeactivated.Name} is no longer active"); }); } /// /// Add Touch state with event listeners. /// public void AddTouchState() { interactiveElement.AddNewState("Touch"); TouchEvents touchEvents = interactiveElement.GetStateEvents("Touch"); touchEvents.OnTouchStarted.AddListener((touchData) => { Debug.Log($"{gameObject.name} Touch Started"); }); } /// /// Add SelectFar state with event listeners. /// public void AddSelectFarState() { interactiveElement.AddNewState("SelectFar"); SelectFarEvents selectFarEvents = interactiveElement.GetStateEvents("SelectFar"); selectFarEvents.OnSelectClicked.AddListener((pointerEventData) => { Debug.Log($"{gameObject.name} Far Interaction Click"); }); } /// /// Add Default state event listeners. The Default state is present by default /// and does not need to be added. /// public void AddDefaultStateListeners() { StateEvents defaultEvents = interactiveElement.GetStateEvents("Default"); defaultEvents.OnStateOn.AddListener(() => { Debug.Log($"{gameObject.name} Default State On"); }); defaultEvents.OnStateOff.AddListener(() => { Debug.Log($"{gameObject.name} Default State Off"); }); } /// /// Add FocusNear state with event listeners. /// public void AddFocusNearState() { interactiveElement.AddNewState("FocusNear"); FocusEvents focusNearEvents = interactiveElement.GetStateEvents("FocusNear"); focusNearEvents.OnFocusOn.AddListener((pointerEventData) => { Debug.Log($"{gameObject.name} Near Interaction Focus"); }); } /// /// Add FocusFar state with event listeners. /// public void AddFocusFarState() { interactiveElement.AddNewState("FocusFar"); FocusEvents focusFarEvents = interactiveElement.GetStateEvents("FocusFar"); focusFarEvents.OnFocusOn.AddListener((pointerEventData) => { Debug.Log($"{gameObject.name} Far Interaction Focus"); }); } /// /// Add Clicked state with event listeners. /// public void AddClickedState() { interactiveElement.AddNewState("Clicked"); ClickedEvents clickedEvent = interactiveElement.GetStateEvents("Clicked"); clickedEvent.OnClicked.AddListener(() => { Debug.Log($"{gameObject.name} Clicked"); }); // Note: // To customize the timing of when the clicked state is triggered use: // interactiveElement.TriggerClickedState(); } /// /// Add the Toggle states with event listeners. /// public void AddToggleStates() { // Adds both the ToggleOn and ToggleOff state interactiveElement.AddToggleStates(); // Toggle On Events ToggleOnEvents toggleOnEvent = interactiveElement.GetStateEvents("ToggleOn"); toggleOnEvent.OnToggleOn.AddListener(() => { Debug.Log($"{gameObject.name} Toggled On"); }); // Toggle Off Events ToggleOffEvents toggleOffEvent = interactiveElement.GetStateEvents("ToggleOff"); toggleOffEvent.OnToggleOff.AddListener(() => { Debug.Log($"{gameObject.name} Toggled Off"); }); // Note: // To customize the timing of when the toggle states are changed use: // interactiveElement.SetToggleStates(); } /// /// Add the SpeechKeyword state with event listeners. /// public void AddSpeechKeywordState() { interactiveElement.AddNewState("SpeechKeyword"); SpeechKeywordEvents speechKeywordEvents = interactiveElement.GetStateEvents("SpeechKeyword"); KeywordEvent keywordEvent = new KeywordEvent() { Keyword = "Change" }; // Any new keyword MUST be registered in the speech command profile prior to runtime // To register a keyword: // 1. Select the MixedRealityToolkit game object // 2. Select Copy and Customize at the top of the profile // 3. Navigate to the Input section and select Clone to enable modification of the Input profile // 4. Scroll down to the Speech section in the Input profile and clone the Speech Profile // 5. Select Add a New Speech Command keywordEvent.OnKeywordRecognized.AddListener(() => { Debug.Log($"The Change Keyword was recognized"); }); speechKeywordEvents.Keywords.Add(keywordEvent); speechKeywordEvents.OnAnySpeechKeywordRecognized.AddListener((speechEventData) => { Debug.Log($"{speechEventData.Command.Keyword} recognized"); }); } /// /// Create a state with a new name. This state will be initialized with the StateEvents /// event configuration that contains the OnStateOn and OnStateOff events. /// public void CreateNewState() { interactiveElement.AddNewState("MyNewState"); // A new state is initialized with a the default StateEvents configuration which contains the // OnStateOn and OnStateOff events StateEvents myNewStateEvents = interactiveElement.GetStateEvents("MyNewState"); myNewStateEvents.OnStateOn.AddListener(() => { Debug.Log($"MyNewState is On"); }); // Creating a new state with a custom event configuration requires the creation of 2 new files: // 1. A receiver file // 2. An event configuration file // // An example of a new state with a custom event configuration is the Keyboard state. // Example file names are KeyboardReceiver.cs + KeyboardEvents.cs files } } }