使用Raycast查看是否单击了画布按钮

时间:2015-10-08 12:51:22

标签: c# unity3d unity5

我使用此按钮获取按钮:

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

1 个答案:

答案 0 :(得分:1)

因为没有Button.collider这样的字段。编译器不知道您尝试访问哪个字段。它也说该字段已被弃用,所以你去了