MixedRealityToolkit-Unity icon indicating copy to clipboard operation
MixedRealityToolkit-Unity copied to clipboard

Add Interactable event via script

Open fcagnetta opened this issue 7 years ago • 8 comments

Describe the problem

It would be useful to add events to interactables via script. Imagine cases in which we add the script at runtime.

  • [x] Add OnClick from dynamically instantiated Interactable

Describe the solution you'd like

It would be great a simple way to do that (still I can't find out how). Ok, maybe not same as OnClick event that is completely exposed, but something easy to implement.

Describe alternatives you've considered

Use a global listener instead of interactable script

Additional context

Same for interactable receiver script, and so on.

fcagnetta avatar Jun 21 '19 16:06 fcagnetta

@fcagnetta , hello! Can you elaborate on what you mean by "add events"? Do you mean just subscribe to the OnClick Unity event inside Interactable.cs and thus be able to assign actions at runtime when a user selects the Interactable? If so, this is possible.

var interactable = myGameObject.AddComponent<Interactable>();
interactable .OnClick.AddListener(YourFunction);

Troy-Ferrell avatar Jun 22 '19 07:06 Troy-Ferrell

Hi Troy, sorry I try to explain better:

If you programmatically add Interactable component to an object, and then you want to add a listener for OnClick event and for OnFocus (on and off) event, and X other event you may need, how can you do that? OnClick event is automatically provided so you can just add a listener to it, but what about the others?

In the inspector you can just click add event and choose among a list of events, then you drag the script on which the method you want to call resides onto.

How to script this? Thanks

fcagnetta avatar Jun 22 '19 14:06 fcagnetta

Ah I see now. I know in Unity inspector it says "Add event", but to be all on the same page, you mean add InteractableEvent objects dynamically. The "Add event" in the inspector I am pretty sure just represent InteractableEvent objects. The property we want is Interactable.Events and I believe if added correctly this could work. But the InteractableEvent creation isn't super obvious. For example, protected SetupEvent() in Interactable gets called on Awake so you would have to link in those pieces yourself?

@killerantz can you help with better guidance?

Troy-Ferrell avatar Jun 22 '19 20:06 Troy-Ferrell

Meanwhile I would like to add a bit of code of a use case for the OnClick() event registration:

Interactable interactable = gameObject.AddComponent<Interactable>();
interactable.IsGlobal = true;
interactable.OnClick.AddListener(() => SomeGuy.Instance.HelloWorld());

I get a null exception when Interactable.cs runs SetupStates() (line 272) StateManager = States.SetupLogic(); - States is null. Maybe a call to GetStates() is needed instead of using States as is.

fcagnetta avatar Jun 24 '19 08:06 fcagnetta

A good way to do this is implement the IInteractableHandler interface and call the Interactable.AddHandler(IInteractableHandler handler) method. This will call two events methods and track all the state changes currently being set in Interactable.

        public virtual void OnStateChange(InteractableStates state, Interactable source)
        {
            print(source.HasFocus);
            // the state has changed, do something new
            /*
            bool hasDown = state.GetState(InteractableStates.InteractableStateEnum.Pressed).Value > 0;

            bool focused = state.GetState(InteractableStates.InteractableStateEnum.Focus).Value > 0;

            bool isDisabled = state.GetState(InteractableStates.InteractableStateEnum.Disabled).Value > 0;

            bool hasInteractive = state.GetState(InteractableStates.InteractableStateEnum.Interactive).Value > 0;

            bool hasObservation = state.GetState(InteractableStates.InteractableStateEnum.Observation).Value > 0;

            bool hasObservationTargeted = state.GetState(InteractableStates.InteractableStateEnum.ObservationTargeted).Value > 0;

            bool isTargeted = state.GetState(InteractableStates.InteractableStateEnum.Targeted).Value > 0;

            bool isToggled = state.GetState(InteractableStates.InteractableStateEnum.Toggled).Value > 0;

            bool isVisited = state.GetState(InteractableStates.InteractableStateEnum.Visited).Value > 0;

            bool isDefault = state.GetState(InteractableStates.InteractableStateEnum.Default).Value > 0;

            bool hasGesture = state.GetState(InteractableStates.InteractableStateEnum.Gesture).Value > 0;

            bool hasGestureMax = state.GetState(InteractableStates.InteractableStateEnum.GestureMax).Value > 0;

            bool hasCollistion = state.GetState(InteractableStates.InteractableStateEnum.Collision).Value > 0;

            bool hasCollistion = state.GetState(InteractableStates.InteractableStateEnum.VoiceCommand).Value > 0;

            bool hasCustom = state.GetState(InteractableStates.InteractableStateEnum.Custom).Value > 0;
            */
        }

        /// <inheritdoc />
        public virtual void OnVoiceCommand(InteractableStates state, Interactable source, string command, int index = 0, int length = 1)
        {
            // Voice Command Happened
        }

        /// <inheritdoc />
        public virtual void OnClick(InteractableStates state, Interactable source, IMixedRealityPointer pointer = null)
        {
            // Click Happened
        }

Another option is to have a component that monitors Interactable.HasFocus or Interactable.HasPress on update. Since we could be trying to tack any combination of states to detect the pattern we care about, we didn't set up all the state changes as events, but an OnStateChanged event may be helpful.

killerantz avatar Jun 24 '19 16:06 killerantz

At the moment, I workarounded this with a script which implement a couple of interfaces, including IMixedRealityInputHandler and its "sisters", and programmatically attached to the gameobjects I need to interact with.

fcagnetta avatar Jun 25 '19 09:06 fcagnetta

Meanwhile I would like to add a bit of code of a use case for the OnClick() event registration:

Interactable interactable = gameObject.AddComponent<Interactable>();
interactable.IsGlobal = true;
interactable.OnClick.AddListener(() => SomeGuy.Instance.HelloWorld());

I get a null exception when Interactable.cs runs SetupStates() (line 272) StateManager = States.SetupLogic(); - States is null. Maybe a call to GetStates() is needed instead of using States as is.

I've just verified that adding OnClick event handler to dynamically instantiated interactable actually works in the GA release, added a test for this. Yay!

julenka avatar Aug 30 '19 20:08 julenka

Fixed

julenka avatar Sep 04 '19 17:09 julenka