如何在多维数据集中动态创建 OnPointerDown MixedReality PointerEventData?

How to create OnPointerDownMixedRealityPointerEventData dynamycally in a cube?

我正在使用 MRTK 2.3.0 版本,当 "event pointer down" 创建一个新的游戏对象立方体时,我创建了一个拖动的立方体,但新的立方体没有事件。我想分配一个事件来移动它。如何通过代码分配事件?

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

    public class TouchableCube : MonoBehaviour, IMixedRealityPointerHandler
    {
        Color colorBlue = Color.blue;
        Renderer rend;

        public void OnPointerClicked(MixedRealityPointerEventData eventData)
        {
            rend.material.color = colorBlue;
        }


        public void OnPointerDown(MixedRealityPointerEventData eventData)
        {
            rend.material.color = Color.red;
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = new Vector3(0,0,2);

            cube.SetActive(true);


        }

        public void OnPointerDragged(MixedRealityPointerEventData eventData)
        {
            rend.material.color = Color.yellow;
            Vector3 v = new Vector3(
                               eventData.Pointer.Position.x,
                               eventData.Pointer.Position.y, 
                               eventData.Pointer.Position.z);
            rend.transform.position = v;   
        }

您可以添加 PointerHandler 到您的收件人并添加听众。下面是如何添加一个改变立方体颜色的侦听器,来自您的示例代码:

        public void OnPointerDown(MixedRealityPointerEventData eventData)
        {
            rend.material.color = Color.red;
            GameObject cube = GameObject.CreatePrimitive(PrimitiveType.Cube);
            cube.transform.position = new Vector3(0,0,2);
            var pointerHandler = target.AddComponent<PointerHandler>();
            pointerHandler.OnPointerDown.AddListener((e) => material.color = Color.green);
            pointerHandler.OnPointerUp.AddListener((e) => material.color = Color.magenta);
            cube.SetActive(true);

        }

有关更多示例,请参阅 How to add near interactivity and Pointers