UGUI自動判斷UI層次


問題描述:

       在開發中,經常會遇到需要判斷UI層次的問題,比如imagetop 在 imageCenter上層 ,imageCenter 在 imageBottom上層,注意這裏的層次關係指的是實際的UI顯示層次

與UI在Hierachy中的層級無關,此時就要判斷出這種層次關係來;

  


解決思路:

public RectTransform rect;
public Canvas ca;
public Vector2 v2;
void OnGUI(){
if(GUILayout.Button("dddfffff"))
{
  v2= RectTransformUtility.WorldToScreenPoint (ca.worldCamera, rect.position);




PointerEventData eventDataCurrentPosition = new PointerEventData(EventSystem.current);
eventDataCurrentPosition.position = v2;


List<RaycastResult> results = new List<RaycastResult>();
EventSystem.current.RaycastAll(eventDataCurrentPosition, results);


for (int i = 0; i < results.Count; i++) {
Debug.Log (results[i].gameObject.name);
}


}
}

發佈了10 篇原創文章 · 獲贊 96 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章