使用光线投射来查看画布按钮是否被点击

本文关键字:按钮 是否 布按钮 光线投射 | 更新日期: 2023-09-27 18:16:35

我使用这个按钮:

Button button = GameObject.FindGameObjectWithTag("MainCanvas").GetComponentInChildren<Button>();

然后我做:

void Update()
{
    Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
    RaycastHit hit;
    if (Physics.Raycast(ray, hit))
    {       
        if (button.collider.Raycast(mainCamera.ScreenPointToRay(Input.mousePosition), hit))
        { 
            //Code to run when button clicked
        }
    }   
}

然而,我一直得到错误。知道我哪里做错了吗?

错误:

Argument 1: Cannot convert from UnityEngine.Ray to UnityEngine.Vector3 Argument 1: Cannot convert from UnityEngine.RaycastHit to UnityEngine.Vector3 Component.collider is obsolete. Property: collider has been depricated Component does not contain a definition for Raycast and no extension method 'Raycast' accepting a first argument type of 'Component' could be found

使用光线投射来查看画布按钮是否被点击

这是因为没有Button.collider这样的字段。编译器不知道您要访问哪个字段。它还表示该字段已被弃用所以你可以选择