归海一啸
管理员
管理员
  • 社区居民
  • 最爱沙发
  • 忠实会员
  • 喜欢达人
  • 原创写手
阅读:3014回复:0

(Unity3d)Vuforia开发高阶二-虚拟按钮(第三部分)

楼主#
更多 发布于:2015-06-25 22:42
第三部分:
既然虚拟按钮的事件可以被检测到,那么可以像正常的按钮一样,执行长按短按,或者双击之类的操作。这里以判断虚拟按钮长按还是短按为例。

主要的实现原理是通过判断触发持续的时间长短。
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);
        }
    }
}

AR学院(www.arvrschool.com),从这里感触未来!

欢迎分享

游客

返回顶部