microsoft / MixedRealityToolkit-Unity

This repository is for the legacy Mixed Reality Toolkit (MRTK) v2. For the latest version of the MRTK please visit https://github.com/MixedRealityToolkit/MixedRealityToolkit-Unity
https://aka.ms/mrtkdocs
MIT License
6k stars 2.12k forks source link

Dynamically adding NearInteractionTouchableVolume component works in Editor but not in HoloLens2. #9143

Closed TakuyaKinoshita closed 3 years ago

TakuyaKinoshita commented 3 years ago

Describe the bug

I wanted to register hand events dynamically when I used HoloLens2. I implemented the following reference and it works in UnityEditor, but not when deployed to HoloLens2.

First, I created the code to change the color of the material using the Sphere object. Added an event to dynamically set the color for Sphere objects.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
// Mixed Reality Toolkit
using Microsoft.MixedReality.Toolkit.UI;
using Microsoft.MixedReality.Toolkit.Input;

public class DynamicHandEvent : MonoBehaviour
{
    [SerializeField]
    private GameObject sphere;
    [SerializeField]
    private Material sphereMa;

    private void Start()
    {
        NearInteractionTouchableVolume comp = sphere.AddComponent<NearInteractionTouchableVolume>();
        comp.EventsToReceive = TouchableEventType.Touch;
        TouchHandler touch = sphere.AddComponent<TouchHandler>();
        touch.OnTouchStarted.AddListener((e) => {
            sphereMa.color = GetColor("#ee8695");
        });
        touch.OnTouchCompleted.AddListener((e) => {
            sphereMa.color = GetColor("#ECEFF1");
        });
    }
    private Color GetColor(string sColor)
    {
        Color color = default(Color);
        ColorUtility.TryParseHtmlString(sColor, out color);
        return color;
    }
}

Articles referred to https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/Input/HowToAddNearInteractivity.html#near-interaction-script-examples

To reproduce

Steps to reproduce the behavior:

  1. Create Sphere object
  2. Set the position of the Sphere object to (0, 0, 1)
  3. Change the size of the Sphere object to (0.1, 0.1, 0.1)
  4. Create a material with the MixedRealityToolkit / Statndard Shader selected.
  5. Set the created material to the MeshRenderer of the Sphere object.
  6. Create a game object from named HandEvent.
  7. Attach the DynamicHandEvent.cs component to the HandEvent object.
  8. In the inspector of the HandEvent object, set the Sphere object and the material created in step 4.

Expected behavior

When touching a Sphere object, the color of the object will be changed to the specified color.

Screenshots

In Editor ezgif com-gif-maker (1)

Your setup (please complete the following information)

Target platform (please complete the following information)

Additional context

Add any other context about the problem here.

TakuyaKinoshita commented 3 years ago

I think it might be related to the #8507 issue.

keveleigh commented 3 years ago

Looks to me like the underlying issue is that we only cache the collider in OnValidate, which is only called in the editor:

https://github.com/microsoft/MixedRealityToolkit-Unity/blob/95659a6cbb568f63c7b762cc68d4cfb0483898b2/Assets/MRTK/Services/InputSystem/NearInteractionTouchableVolume.cs#L29-L34

We should either lazy cache (in the property getter, if the backing field is null) or additionally cache in Awake or Start if the backing field is null at that point.

david-c-kline commented 3 years ago

@CDiaz-MS do we have an ETA on this? Can @keveleigh or I help?

CDiaz-MS commented 3 years ago

Yes, help would be appreciated on this one.