Unity 快速定位UI
Unity 快速定位UI
問題由來
-
當(dāng)項目UI層級特別多的時候
-
想找快速定位UI的位置非常麻煩
使用方式
-
運行狀態(tài)下
-
鼠標(biāo)移動到指定UI位置
-
快捷鍵Ctrl+F
源碼
using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
public class QuickPositioningUITool : Editor
{
[MenuItem("ZQFramwork/快速定位UI %f", false, 0)]
public static void QuickPositioning()
{
if (Application.isPlaying == false)
{
return;
}
//使焦點移動到Game視圖
Type gameViewType = typeof(Editor).Assembly.GetType("UnityEditor.GameView");
EditorWindow window = EditorWindow.GetWindow(gameViewType);
window.Focus();
PointerEventData pointerEventData = new PointerEventData(EventSystem.current)
{
position = Input.mousePosition
};
List<RaycastResult> raycastResults = new List<RaycastResult>();
//獲取鼠標(biāo)位置所有碰撞對象
EventSystem.current.RaycastAll(pointerEventData, raycastResults);
if (raycastResults.Count > 0)
{
//選擇第一個對象
Selection.activeGameObject = raycastResults[0].gameObject;
EditorGUIUtility.PingObject(raycastResults[0].gameObject);
}
}
}

浙公網(wǎng)安備 33010602011771號