复制代码
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> |
|