0

我有大约 10 个按钮,每个按钮都有一个通过脚本添加的唯一值。我想知道使用 MRTK 手与按钮交互时单击了哪个按钮。

我尝试将 updateField 方法添加到使用默认 EventSystem 获取当前选定对象的按钮。但是,当我调试时,该值为空。

public void UpdateField()
{
      int dayNumb = EventSystem.current.currentSelectedGameObject.GetComponent<DayButton>().CurrentNumber();
}

NullReferenceException:对象引用未设置为对象的实例 UpdateInputField.UpdateField ()(在 Assets/Scripts/UpdateInputField.cs:35) UnityEngine.Events.InvokableCall.Invoke ()(在 C:/buildslave/unity/build/Runtime /Export/UnityEvent.cs:166) UnityEngine.Events.UnityEvent.Invoke () (在 C:/buildslave/unity/build/Runtime/Export/UnityEvent_0.cs:58) Microsoft.MixedReality.Toolkit.UI.InteractableOnPressReceiver.OnUpdate (Microsoft.MixedReality.Toolkit.UI.InteractableStates 状态,Microsoft.MixedReality.Toolkit.UI.Interactable 源)(在 Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Events/InteractableOnPressReceiver.cs:71)Microsoft.MixedReality .Toolkit.UI.Interactable.InternalUpdate ()(位于 Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Interactable.cs:525)微软。MixedReality.Toolkit.UI.Interactable.Update () (在 Assets/MixedRealityToolkit.SDK/Features/UX/Interactable/Scripts/Interactable.cs:506)

4

3 回答 3

2

关于异常

EventSystem.current.currentSelectedGameObject.GetComponent<DayButton>().CurrentNumber();

提供了很多可能性。基本上每个之前的参考都.可以通过null。最有可能的情况是,currentSelectedGameObject当前根本没有选择任何对象,或者GetComponent<DayButton>()选择的对象根本没有这样的组件。


我对您的课程一无所知,对 MRTK 知之甚少,但也许您可以使用事件系统模式,例如

public class DayButton : WhateverType
{
    // public static event everyone can add listeners to 
    // in order to wait for any DayButton click
    public static event Action<DayButton> OnButtonClicked;

    // Just assuming for now this is the method called on click
    public void OnClick()
    {
        // Fire the static event and inform every listener
        // that this button was clicked by passing yourself in as parameter
        OnButtonClicked?.Invoke(this);
    }
}

然后你可以让一个(或不同的)监听器类等待事件并检查按下了哪个按钮

public class SomeListener : MonoBehaviour
{
    privtae void Awake()
    {
        // Add a listener to the global event
        DayButton.OnButtonClicked += HandleOnButtonClicked;
    }

    private void OnDestroy()
    {
        // Make sure to always remove listeners once not needed anymore
        // otherwise you get NullReferenceExceptions!
        DayButton.OnButtonClicked -= HandleOnButtonClicked;
    }

    // By passing the according DayButton reference in you have access
    // to all its public fields and methods
    private void HandleOnButtonClicked(DayButton button)
    {
        Debug.Log("The Button with the name {button.name} and number {button.CurrentNumber} was clicked!", this);
    }
}
于 2019-09-23T14:21:27.737 回答
2

以下代码将打印场景中单击的每个按钮的名称。请注意,您可以在从代码创建按钮时添加这些侦听器,而不是在启动时添加事件侦听器。

using Microsoft.MixedReality.Toolkit.UI;
using UnityEngine;

/// <summary>
/// On startup, adds event handlers for every Interactable aka button
/// in the scene that logs the name of the button pressed.
/// You can similarly add such a listener when adding the buttons dynamically
/// </summary>
public class PrintWhichButtonPressed : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        var allInteractables = GameObject.FindObjectsOfType<Interactable>();
        foreach (var i in allInteractables)
        {
            i.OnClick.AddListener(() => Debug.Log(Time.time + ": " + i.gameObject.name + " was clicked"));
        }
    }
}
于 2019-09-24T01:26:58.647 回答
1

解决了!!我在 start 方法的每个按钮上添加了一个监听器。然后调用该函数来获取这些对象的组件。

这是我是怎么做到的............

using UnityEngine.UI;
using UnityEngine.EventSystems;
using UnityEditor;
using Microsoft.MixedReality.Toolkit.UI;

public class UpdateInputField : MonoBehaviour {
    public InputField mainInputField;
    private Calendar calendar;
    private GameObject dateCanvas;
    public int dayNumb;

    void Start(){
        var allInteractables = GameObject.FindObjectsOfType<Interactable>();
        foreach (var i in allInteractables)
        {
            i.OnClick.AddListener(() => UpdateField(i));
        }
    }

    public void UpdateField(Microsoft.MixedReality.Toolkit.UI.Interactable index)
    {
        dayNumb = GameObject.Find(index.gameObject.name).GetComponent<DayButton>().CurrentNumber();
        calendar = GameObject.FindObjectOfType<Calendar>();
        mainInputField.text = calendar.ReturnDate(dayNumb);     
    }
}
于 2019-09-24T11:56:38.447 回答