Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# RaycastHit与canvas的问题?_C#_Unity3d - Fatal编程技术网

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?如果是,您只是做错了,请阅读。