复选框在混合现实工具包 V2 中无法正常工作

Checkbox not working as intended in Mixed reality toolkit V2

复选框目前在新工具包中不可用,因为您无法将函数绑定到状态。

GitHub 上有一个开发提交,但它还不能用,所以我需要一个脚本来解决,而不更改工具包。

应该能够轻松设置起始状态,并在状态更改时调用函数 - 目前您做不到。

找到了无需更改工具包即可工作的解决方案,并且应该适用于较新的版本。

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

[RequireComponent(typeof(Interactable))]
public class CheckBoxInteractableSwitch : MonoBehaviour
{

    public bool startChecked = true;

    public UnityEvent OnCheck;
    public UnityEvent OnUncheck;

    private Interactable interactable;
    private int state = 1;

    void Start()
    {
        interactable = GetComponent<Interactable>();

        if (OnCheck == null)
            OnCheck = new UnityEvent();
        if (OnUncheck == null)
            OnUncheck = new UnityEvent();

        OnCheck.AddListener(Checked);
        OnUncheck.AddListener(UnChecked);

        //works with 2 dimensions only
        if (startChecked)
        {
            if (interactable.GetDimensionIndex() == 0) interactable.IncreaseDimension();
        }
        else
        {
            if (interactable.GetDimensionIndex() == 1) interactable.IncreaseDimension();
        }

    }

    void Update()
    {
        if (interactable == null) return;

        //state check
        if (state != interactable.GetDimensionIndex())
        {
            state = interactable.GetDimensionIndex();
            if (state == 0) OnUncheck.Invoke();
            if(state == 1) OnCheck.Invoke();
        }
    }

    private void Checked()
    {

    }

    private void UnChecked()
    {

    }
}

仅适用于复选框(2 维),您可以设置复选框的默认状态,并且可以在状态更改时订阅状态。