Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an example of how to instantiate Interactable at runtime #4456

Closed
julenka opened this issue May 17, 2019 · 7 comments
Closed

Add an example of how to instantiate Interactable at runtime #4456

julenka opened this issue May 17, 2019 · 7 comments

Comments

@julenka
Copy link
Contributor

julenka commented May 17, 2019

Filed in response to #4433.

In that PR, I updated the change to no longer use Resources folder. I think it would be useful in future to provide an example of how to instantiate Interactable at runtime, since even though the null ref is fixed, the resulting interactable is not very useful.

The task is to create an example of the following:

  • Create a game object at runtime
  • Add Interactable component to it
  • Configure the component so it behaves like default interactable, as if it were added in the editor.

Here is some example starter code:

using Microsoft.MixedReality.Toolkit.UI;
using System.Collections.Generic;
using UnityEngine;

public class InteractableTest : MonoBehaviour
{
    [SerializeField]
    private InteractableProfileItem defaultInteractableProfile;
    [SerializeField]
    private States defaultInteractableStates;

    void Start()
    {
        GameObject test = GameObject.CreatePrimitive(PrimitiveType.Cube);
        var interactable = test.AddComponent<Interactable>();
        interactable.States = defaultInteractableStates;
        interactable.Profiles.Add(defaultInteractableProfile);
        Debug.Log($"profiles: {interactable.Profiles.Count} events: {interactable.Events.Count}");
        interactable.OnClick.AddListener(()=>Debug.Log("Clicked!"));
    }
}
@StarWardo
Copy link

StarWardo commented May 28, 2019

Hello

Thank you Julia for this example. Although I would like to add the OnfocusEnter and OnfocusExit events at runtime. Optionally using the InteractableOnfocusReciever event type.

Could you give me a hint on how to do that ?

My case:

User can place objects (prefabs) at runtime using hands/gaze.
Voice command ""Remove" should remove the current focussed (looked at) object

@julenka
Copy link
Contributor Author

julenka commented May 28, 2019

It is unfortunately not very easy to add speech commands to Interactable at run time. The Interactables are currently written in a way that makes them very easy to configure in editor, but they weren't really intended to be configured in code, as I understand. @killerantz is the interactable expert and can give you more detail.

But if you want to just listen for a voice command when looking at an object, there might be a different approach you want to take. Try adding a SpeechInputHandler instead. You can use "IsFocusRequired" field to require that the user is looking at the object before it says remove.

In fact, your question about how to place a prefab and then remove it via voice when looked at would actually be a good candidate for stackoverflow, since it could be easier to find in the
future and others could find it more easily. If you put it on SO I'd be happy to give you a more detailed answer with an example. Just don't forget to use the tag MRTK.

@killerantz
Copy link
Member

It would be easier to have a custom script handle focus events from interactable, if you are instantiating it with code, then using the InteractableOnFocusReceiver, which is more for setting things up in the editor. You can use ReceiverBaseMonoBehavior as an example of getting state changes from Interactable, extend it to check for focus changes. Setting up a Speech Input for the "Remove" command and an ISpeechInputHandler would be the way to go for the removal part. Otherwise, you can setup a prefab with all the events and themes wired up in the editor and instantiate that.

@killerantz
Copy link
Member

Interactable has an ISpeechInputHandler built in, so it can listen for "Remove" if it is setup in the Speed Input profile.

@JShull
Copy link

JShull commented May 29, 2019

My case:

User can place objects (prefabs) at runtime using hands/gaze.
Voice command ""Remove" should remove the current focussed (looked at) object

I also posted this to @StarWardo in Slack so if you need to remove it from here feel free too - just trying to help :)

I didn't tackle the voice input yet as I am not there yet on my own MRTK project, but I've had fun playing with all of the massive amounts of capability already built in 👍

Hope this helps @StarWardo , I spent about 30 minutes slapping together a possible solution that should lay the groundwork for a way to do what you're asking about.

For run-time placement you would just need to add a method to instantiate an object that contains all of the information I setup in this example. The solutions they already mentioned above should facilitate adding in the various components, my example is assuming you have some sort of already default prefab with interactable built on.
Video is here: https://www.youtube.com/watch?v=47OExTOOuyU&feature=youtu.be
Unity Package is here: https://github.com/JShull/MRTKExamples

@StarWardo
Copy link

StarWardo commented Jun 3, 2019

@JShull
Copy link

JShull commented Jun 3, 2019

@JShull Thanks, could you add this answer on SO ? https://stackoverflow.com/questions/56393003/how-to-place-a-prefab-and-then-remove-it-via-voice-when-looked-at

Went ahead and added it. Hope it helped!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants