Add an example of how to instantiate Interactable at runtime
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!"));
}
}
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
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.
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.
Interactable has an ISpeechInputHandler built in, so it can listen for "Remove" if it is setup in the Speed Input profile.
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
@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
@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!