前言
项目的 UI 界面越来越复杂,有必要写个脚本快速查找;
一、直接上代码
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
/// <summary>
/// 脚本挂到任意Canvas上生效
/// </summary>
public class QuickFindUI : MonoBehaviour
{
public GameObject m_obj;
private int cnt = 0;
private void Update()
{
// 滚轮点击
if (Input.GetMouseButtonDown(2))
GetOverUI(m_obj);
}
public void GetOverUI(GameObject obj)
{
PointerEventData pointerEventData = new(EventSystem.current);
pointerEventData.position = Input.mousePosition;
List<RaycastResult> results = new();
obj.GetComponent<GraphicRaycaster>().Raycast(pointerEventData, results);
//自动选中Hierarchy面板对象
EditorGUIUtility.PingObject(results[cnt >= results.Count ? cnt = 0 : cnt++].gameObject);
}
}
二、总结
理论上也可以用射线检测的方法,有空有兴趣可以试试。