Home > front end >  How to assign programmatically an Interactable Event in MRTK
How to assign programmatically an Interactable Event in MRTK

Time:11-09

I am creating a map in Unity3D to be displayed in Hololens 2. I am using MRTK to code interactable functionality.

Since the map contains a lot of independent countries, whose respond onClick may vary with time, I decided to add the required components programmatically. For example:

public Transform worlmap;
public Microsoft.MixedReality.Toolkit.UI.Theme mTheme;
public Microsoft.MixedReality.Toolkit.UI.States mStates;
List<Microsoft.MixedReality.Toolkit.UI.Theme> themes;


    void Start()
{
    themes = new List<Microsoft.MixedReality.Toolkit.UI.Theme>();
    if (mTheme)
    {
        themes.Add(mTheme);
    }

    // Looping through all the countries in worldMap
    foreach (Transform country in worldMap)
    {
        // Adding the 4 components which make a GameObject interactable in Hololens
        country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>();
        country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.UI.PressableButton>();
        country.gameObject.AddComponent<Microsoft.MixedReality.Toolkit.Input.NearInteractionTouchable>();
        country.gameObject.AddComponent<MeshCollider>();

        // Assigning State
        country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().States = mStates;
        
        // Assigning the Profiles (Will definine the colors based on the State)
        Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem mProfile = new Microsoft.MixedReality.Toolkit.UI.InteractableProfileItem();
        mProfile.Themes = themes;
        mProfile.Target = country.gameObject;
        country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().Profiles.Add(mProfile);
        
        // Assigning Events
        // TODO
    }
}

The part below is working, but I am not able to assign Events to the different countries in the same loop.

I can define these events in the Editor, for example:

enter image description here

But since it is a repetitive activity which may slightly change over time, I was trying to achieve the same programmatically inside the loop of the script above.

This is what I tried, but it is not working:

// Creating an Event
Microsoft.MixedReality.Toolkit.UI.InteractableEvent mEvent = new Microsoft.MixedReality.Toolkit.UI.InteractableEvent();

// Assigning the method to be executed during onClick
mEvent.Event.AddListener(country.gameObject.GetComponent<CountrySelected>().printName)

// Assigning the Event to Interactable 
country.gameObject.GetComponent<Microsoft.MixedReality.Toolkit.UI.Interactable>().InteractableEvents.Add(mEvent);

What is the correct way of adding programmatically Events to an Interactable in MRTK?

CodePudding user response:

To assign OnClick Event for the Interactable component at the runtime, please try the following code:

    this.GetComponent<Interactable>().OnClick.AddListener(MyFun1);

The Interactable component profile in the Unity Editor may not display the new method at the runtime, but it does work in the scene.

  • Related