分享

(Unity3d)Vuforia开发基础四-模型选定与控制

 雪柳花明 2016-12-08
public class RayPick : MonoBehaviour {
       
    public Material[] mats;
    public Texture[] texs;
    int i = 0;
       
      
    // Use this for initialization
    void Start () {
          
    }
           
    // Update is called once per frame
    void Update () {
        if(Input.GetMouseButton(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);//从摄像机发出到点击坐标的射线
            RaycastHit hitInfo;
            if(Input.touchCount==1&&Input.GetTouch(0).phase==TouchPhase.Began){
            if(Physics.Raycast(ray,out hitInfo))
            {
                Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
                GameObject gameObj = hitInfo.collider.gameObject;
                //gameObj.transform.parent.renderer.material = mats<i>;//设置父组件的材质
                if(Input.GetTouch(0).tapCount==2){
                    gameObj.renderer.material.mainTexture = texs[i];
                }
                //gameObj.renderer.material = mats<i>;
                //gameObj.transform.parent.gameObject.SetActive(true);
                // other gameobjects set false
                //checkIfPicked(gameObj.transform.parent.gameObject);
                Debug.Log("---->"+gameObj.transform.parent.gameObject.name);
                i++;
                if(i>=mats.Length-1){
                    i=0;
                }
                Debug.Log("click object name is " + gameObj.name);
                if(gameObj.tag == "boot")//当射线碰撞目标为boot类型的物品 ,执行拾取操作
                {
                    Debug.Log("pick up!");
                }
            }
            }
        }
    }
}

单击、双击、长按等:if(Physics.Raycast(ray,out hitInfo)) 
这个接下来触发的事件就是单击和长按。

if(Input.touchCount == 1){
                Touch touch = Input.GetTouch(0);
                if(touch.phase == TouchPhase.Began){
                    newTouch = true;
                    touchTime = Time.time;
                }else if(touch.phase == TouchPhase.Stationary){
                    if(newTouch==true&&Time.time-touchTime>1){
                        Debug.Log("longpress!!!");
                        newTouch = false;
                        longPressDetected = true;
                        //touched_model_name = "long press!!!!!!!";
                    }else if(newTouch==false&&longPressDetected== false){
                        newTouch = true;
                        touchTime = Time.time;
                        Debug.Log("TouchTime = "+touchTime);
                    }
                }else{
                    newTouch = false;
                    longPressDetected = false;
                    Debug.Log("setting newTouch false");
                }
            }
双击:if(Input.GetTouch(0).tapCount==2) 
判断单击的次数,如果为2,就是代表双击。
if(Physics.Raycast(ray,out hitInfo))
            {
                Debug.DrawLine(ray.origin,hitInfo.point);//划出射线,只有在scene视图中才能看到
                GameObject gameObj = hitInfo.collider.gameObject;
                //gameObj.transform.parent.renderer.material = mats<i>;//设置父组件的材质
                if(Input.GetTouch(0).tapCount==2){
                    gameObj.renderer.material.mainTexture = texs[i];
                }
}</i></i>


    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多