触摸按钮

VR 里用手指去按的按钮。用一个带碰撞体的物体挂上 AeloriaInteractableShim,脚本订阅 ButtonDown 即可。

准备

  1. 建一个按钮物体(比如一块小面板),加上 Collider(要能被手指碰到);
  2. 给它加 Aeloria Interactable Shim 组件;
  3. 把脚本挂上去,把 shim 和目标物体拖进字段。

脚本

using UnityEngine;
using Cilbox;
using Aeloria.Shims;

[Cilboxable]
public class TouchButton : MonoBehaviour
{
    public AeloriaInteractableShim buttonShim;
    public GameObject lamp;

    void Start()
    {
        buttonShim.ButtonDown += OnPress;
    }

    void OnDestroy()
    {
        if (buttonShim != null) buttonShim.ButtonDown -= OnPress;
    }

    void OnPress()
    {
        lamp.SetActive(!lamp.activeSelf);
    }
}

需要区分”按下”和”松开”时,再订阅 ButtonUp;两个都是无参数的回调。