(Unity3d)Vuforia开发高阶二-虚拟按钮

来源:互联网 发布:分布式数据库 hadoop 编辑:程序博客网 时间:2024/06/06 02:37
第三部分: 
既然虚拟按钮的事件可以被检测到,那么可以像正常的按钮一样,执行长按短按,或者双击之类的操作。这里以判断虚拟按钮长按还是短按为例。 
 
主要的实现原理是通过判断触发持续的时间长短。 

复制代码
using UnityEngine;using System.Collections;using Vuforia; public class MyButtonControl : MonoBehaviour,IVirtualButtonEventHandler {    public GameObject bowling;    private GameObject clone;     private float intervalTime = 0;    private bool isPressed = false;     // 判断是长按还是短按    private bool isShortPress = false;    private bool isLongPress = false;     // Use this for initialization    void Start () {        VirtualButtonBehaviour[] vbs = GetComponentsInChildren<VirtualButtonBehaviour> ();        for (int i =0; i<vbs.Length; i++) {            vbs[i].RegisterEventHandler(this);        }    }         // Update is called once per frame    void Update () {        Debug.Log ("---->"+gameObject.transform.rotation);        if (isPressed) {            intervalTime += Time.deltaTime;        }    }     // 检测长按和短按    public void OnButtonPressed (VirtualButtonAbstractBehaviour vb){        switch (vb.VirtualButtonName) {        case "fire":            Debug.Log("----------> Fire");            //bowling.transform.rigidbody.AddForce (Vector3.forward*20000);            //clone = (GameObject)Instantiate(bowling,bowling.transform.position,gameObject.transform.rotation);            //clone.transform.rigidbody.AddForce (-Vector3.forward*20000);            //clone.transform.rigidbody.velocity = new Vector3(0,0,10);            bowling.transform.rigidbody.velocity = new Vector3(0,0,1000);            isPressed = true;            intervalTime = 0;            break;        default:            break;        }    }     public void OnButtonReleased (VirtualButtonAbstractBehaviour vb){        isPressed = false;        if (intervalTime <= 1) {            Debug.Log("-----> short Pressed!"+intervalTime);        }  else {            Debug.Log("-----> long Pressed!"+intervalTime);        }    }}

0 0