C# RaycastHit与canvas的问题?
只有当命中的游戏对象不在画布中时,此脚本才会在控制台上显示消息。在画布内的按钮上释放鼠标按钮时,脚本不会调试任何内容。我怎样才能解决这个问题C# RaycastHit与canvas的问题?,c#,unity3d,C#,Unity3d,只有当命中的游戏对象不在画布中时,此脚本才会在控制台上显示消息。在画布内的按钮上释放鼠标按钮时,脚本不会调试任何内容。我怎样才能解决这个问题 RaycastHit hit; void Update () { if(Input.GetMouseButtonUp(0)) { Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition); //RayHit hit; if(Ph
RaycastHit hit;
void Update ()
{
if(Input.GetMouseButtonUp(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
//RayHit hit;
if(Physics.Raycast(ray, out hit))
{
// do what you want
Debug.Log(hit.collider.gameObject.tag);
}
}
}
您可以这样使用来获取单击的UI对象
if (Input.GetMouseButtonDown(0))
{
Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (EventSystem.current.IsPointerOverGameObject())
{
Debug.Log(EventSystem.current.currentSelectedGameObject.GetComponent<Text>().name);
}
if(Input.GetMouseButtonDown(0))
{
Ray-Ray=Camera.main.screenpointoray(输入.鼠标位置);
if(EventSystem.current.IsPointerOverGameObject())
{
Log(EventSystem.current.currentSelectedGameObject.GetComponent().name);
}
EventSystem.current.currentSelectedGameObject.GetComponent().name
将返回单击的对象和
EventSystem.current.IsPointerOverGameObject()
将检查UI对象是否单击。您是指画布还是UI?如果是,您只是做错了,请阅读。