zSpace实现点击任意键(笔或者键盘)响应

2023-08-24 14:08:40 浏览数 (2)

有些时候要出现“点击任意键进入”,既要有响应键盘,又要响应笔上的按钮 挂载脚本

代码语言:javascript复制
using UnityEngine;
using System.Collections;
using zSpace.Core;
public class AnyKeyPassMgr : MonoBehaviour {
    ZCore m_zCore;

    public delegate void VoidDelegate(GameObject go);
    public VoidDelegate onClick;

    static public AnyKeyPassMgr Get(GameObject go)
    {
        AnyKeyPassMgr listener = go.GetComponent<AnyKeyPassMgr>();
        if (listener == null) listener = go.AddComponent<AnyKeyPassMgr>();
        return listener;

    }
        // Use this for initialization
    void Start () {
        m_zCore = GameObject.FindObjectOfType<ZCore>();
        m_zCore.TargetButtonPress  = HandleButtonPress;
    }
	
    void OnDestroy()
    {
        m_zCore.TargetButtonPress -= HandleButtonPress;
    }
	// Update is called once per frame
	void Update () {
        if (Input.anyKeyDown)
        {
            onClick(gameObject);
        }
    }

    private void HandleButtonPress(ZCore sender, ZCore.TrackerButtonEventInfo info)
    {
        if (info.TargetType == ZCore.TargetType.Primary)
        {
            //ZCore.Pose pose = m_zCore.GetTargetPose(ZCore.TargetType.Primary, ZCore.CoordinateSpace.World);
            onClick(gameObject);
        }
    }
}

使用委托的形式,如下使用

代码语言:javascript复制
AnyKeyPassMgr.Get(gameObject).onClick = OnAnyKey;

    void OnAnyKey(GameObject obj)
    {
        //处理响应
    }

0 人点赞