需求
- 如果希望點(diǎn)擊一個(gè)物體但是希望隔離UI,當(dāng)點(diǎn)擊到UI時(shí)不響應(yīng)物體的事件,那么一般的做法都是使用Unity自帶的api IsPointerOverGameObject來判斷是否點(diǎn)擊到UI,代碼如下
if (Input.GetMouseButtonDown(0))
{
IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
if (IsOverGameObject)
{
Debug.Log("Click the UI");
return;
}
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
{
if (null != hit.collider)
{
Debug.LogError(hit.collider.name);
}
}
}
解析
- 但是這個(gè)代碼實(shí)際上是有問題的,比如當(dāng)離開unity編輯器窗口,然后再次點(diǎn)擊游戲內(nèi)對應(yīng)物體,會發(fā)現(xiàn)可以打印出點(diǎn)擊物體的名字,之后再點(diǎn)擊則是被IsPointerOverGameObject隔離了。但是我們需要的是無論什么時(shí)候都要被IsPointerOverGameObject隔離,順著IsPointerOverGameObject查看源碼,會發(fā)現(xiàn)如下代碼
public override bool IsPointerOverGameObject(int pointerId)
{
var lastPointer = GetLastPointerEventData(pointerId);
if (lastPointer != null)
return lastPointer.pointerEnter != null;
return false;
}
關(guān)鍵在于lastPointer.pointerEnter != null這句代碼,如果在自己的UI上掛腳本并實(shí)現(xiàn)以下代碼
public void OnPointerEnter(PointerEventData eventData)
{
Debug.Log("OnPointerEnter");
}
會發(fā)現(xiàn)在上面有問題的代碼下,從其他地方重新點(diǎn)擊到unity游戲窗口,并點(diǎn)擊物體,物體名字的log在OnPointerEnter log的前面,所以驗(yàn)證了unity內(nèi)部判斷是否點(diǎn)到ui實(shí)際上是判斷是否OnPointerEnter了UI,但是遺憾的是這個(gè)順序是不固定的。所以一旦是不固定的順序,那么IsPointerOverGameObject在某些情況下會失靈。
解決
- 一個(gè)簡單的解決方案是在GetMouseButtonUp時(shí)再次檢測一遍,這次確保unity檢測到了進(jìn)入U(xiǎn)I,代碼如下
void Update()
{
if (Input.GetMouseButtonDown(0))
{
IsOverGameObject = EventSystem.current.IsPointerOverGameObject();
if (IsOverGameObject)
{
Debug.Log("Click the UI GetMouseButtonDown");
return;
}
}
if (Input.GetMouseButtonUp(0))
{
IsOverGameObject = EventSystem.current.IsPointerOverGameObject() || IsOverGameObject;
if (IsOverGameObject)
{
Debug.Log("Click the UI GetMouseButtonUp");
return;
}
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
if (Physics.Raycast(ray.origin, ray.direction, out RaycastHit hit, float.MaxValue))
{
if (null != hit.collider)
{
Debug.LogError(hit.collider.name);
}
}
}
}
再次運(yùn)行游戲就發(fā)現(xiàn)第一次隔離UI是被GetMouseButtonUp捕捉到了