在二维空间中:
我有一个精灵和一个脚本组件被分配给它
我在Start方法中有这样一个语句:
它不会像预期的那样检测到鼠标!
请解释原因。
public class EvtPub : MonoBehaviour
{
RectTransform rt;
Rect onScreenRect;
void Start()
{
rt = GetComponent<RectTransform>();
// having next statement here, the code will Not work!
onScreenRect = rt.rect;
}
void Update()
{
// having next statement here, the code Will work!
// onScreenRect = rt.rect;
onScreenRect.Set(onScreenRect.x + transform.position.x,
onScreenRect.y + transform.position.y, onScreenRect.width, onScreenRect.height);
if (onScreenRect.Contains(Input.mousePosition))
{
Debug.Log("Mouse over detected!");
}
}
}