目录
1.给需要点击点物体加collider
2.层级面板加EventSystem
3. 相机加Physics Raycaster
4.物体单独响应点击事件
5.控制脚本实现各物体的点击事件
6.点击ui时屏蔽 物体点击事件
1.给需要点击点物体加collider
2.层级面板加EventSystem
3. 相机加Physics Raycaster
2d 3d按需添加
4.物体单独响应点击事件
新建单独脚本挂在响应点击的物体上
public class ClickObj : MonoBehaviour, IPointerClickHandler
{public void OnPointerClick(PointerEventData event){Debug.LogError("我被点了");}
}
5.控制脚本实现各物体的点击事件
private void Update(){if (subCamera[0].activeSelf){if (Input.GetMouseButtonDown(0)){ray = Camera.main.ScreenPointToRay(Input.mousePosition);if (Physics.Raycast(ray, out hit)){obj = hit.collider.gameObject;//根据物体名 进行下一步操作switch (obj.name){case "add_dp": Debug.LogError("aaa");break;case "add_ck":break;case "add_jxb":break;case "add_lt":break;case "add_cm":break;case "add_kqf":break;}}}}else if (subCamera[1].activeSelf){if (Input.GetMouseButtonDown(0)){ray = Camera.main.ScreenPointToRay(Input.mousePosition);if (Physics.Raycast(ray, out hit)){obj = hit.collider.gameObject;//根据物体tag 进行下一步操作switch (obj.tag){case "aaa":Debug.LogError("aaa");break;case "bbb":Debug.LogError("bbb");break;}}}}}
6.点击ui时屏蔽 物体点击事件
if (Input.touchCount == 1){if (EventSystem.current.IsPointerOverGameObject(Input.GetTouch(0).fingerId)) { return; }}else{if (EventSystem.current.IsPointerOverGameObject()) { return; }}