/// <summary> /// Add the Toggle states with event listeners. /// </summary> public void AddToggleStates() { // Adds both the ToggleOn and ToggleOff state interactiveElement.AddToggleStates(); // Toggle On Events ToggleOnEvents toggleOnEvent = interactiveElement.GetStateEvents <ToggleOnEvents>("ToggleOn"); toggleOnEvent.OnToggleOn.AddListener(() => { Debug.Log($"{gameObject.name} Toggled On"); }); // Toggle Off Events ToggleOffEvents toggleOffEvent = interactiveElement.GetStateEvents <ToggleOffEvents>("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(); }
public IEnumerator TestToggleEvents() { // Create an interactive cube InteractiveElement interactiveElement = CreateInteractiveCube(); yield return(null); interactiveElement.AddToggleStates(); yield return(null); // Get the event configuration for the ToggleOn state var eventConfigurationToggleOn = interactiveElement.GetStateEvents <ToggleOnEvents>(toggleOnStateName); // Get the event configuration for the ToggleOn state var eventConfigurationToggleOff = interactiveElement.GetStateEvents <ToggleOffEvents>(toggleOffStateName); bool onToggleOn = false; bool onToggleOff = false; eventConfigurationToggleOn.OnToggleOn.AddListener(() => { onToggleOn = true; }); eventConfigurationToggleOff.OnToggleOff.AddListener(() => { onToggleOff = true; }); interactiveElement.SetToggleStates(); yield return(null); // Make sure the toggle is on at the start Assert.True(onToggleOn); // Create a new hand and initialize it with an object in focus var leftHand = new TestHand(Handedness.Left); // Show hand at starting position yield return(ShowHandWithObjectInFocus(leftHand)); // Click the hand yield return(leftHand.Click()); Assert.True(onToggleOff); }