我使用 MainCamera`ray 来碰撞 GameObject,但是如果 GameObject 是 Image,我无法碰撞 Iamge,我该怎么办?

I use MainCamera`ray to get the GameObject collided,but if the GameObject is Image,I can not collide the Iamge,What can I do?

if (Physics.Raycast(ray, out hit))
    {
        if (hit.collider.name.StartsWith("Image"))
        {//can not get image
            Debug.Log("OK");
            image = GameObject.Find(hit.collider.name).gameObject;
            Debug.Log ("image.name:"+image.name);
        }
    }

获得碰撞的代码。

如果光线到Image,我无法得到碰撞事件。

如何获取图像碰撞

如果图像是 UI Image. You can use Event System's raycast。 或者,如果按图像,您的意思是附加了精灵渲染器的 2D 对象。你需要有一个 Box collider 连接到它。

顺便说一句 image = GameObject.Find(hit.collider.name).gameObject; 是 wrong.you 可以像这样得到 gameObject : image = hit.collider.gameObject;