Add touch listener to 3D object in unity(Unity中给3D物体增添按钮点击功能)

Add touch listener to 3D object in unity(Unity中给3D物体添加按钮点击功能)

1.将下面该段代码赋给摄像机

void Update () {
       
        RaycastHit hit = new RaycastHit();
        for (int i = 0; i < Input.touchCount; ++i) {
            if (Input.GetTouch(i).phase.Equals(TouchPhase.Began)) {
            // Construct a ray from the current touch coordinates
            Ray ray = Camera.main.ScreenPointToRay(Input.GetTouch(i).position);
            if (Physics.Raycast(ray, out hit)) {
                hit.transform.gameObject.SendMessage("OnMouseDown");
              }
           }
       }
    }

2.为要被点击的3D物体创建一个C#脚本,并添加OnMouseDown函数,以使上段代码进行触发执行;

void OnMouseDown(){
		DoSomething();
	}
	
       This script can be attached to any GameObjects that you want to use as buttons.

转自http://blog.****.net/tanmengwen/article/details/7882170


1楼leihengxin6小时前
顶一下。