Files
DuckovMods/UIFrame/Utilities/GameObjectTool.cs

76 lines
3.1 KiB
C#
Raw Permalink Normal View History

2025-11-05 21:34:21 +08:00
using System.Collections.Generic;
using System.Linq;
2025-11-05 21:34:21 +08:00
using UnityEngine;
using UnityEngine.SceneManagement;
2025-11-05 21:34:21 +08:00
namespace UIFrame.Utilities
{
public class GameObjectTool
{
/// <summary>
/// 根据Unity对象路径查找场景中的GameObject包括隐藏非激活对象。
/// 路径示例:"RootObject/ChildObject/GrandchildObject"
2025-11-05 21:34:21 +08:00
/// </summary>
/// <param name="path">要查找的GameObject的层级路径。</param>
2025-11-05 21:34:21 +08:00
/// <returns>找到的GameObject如果未找到则返回null。</returns>
public static GameObject FindObjectByPath(string path)
2025-11-05 21:34:21 +08:00
{
if (string.IsNullOrWhiteSpace(path))
2025-11-05 21:34:21 +08:00
{
Debug.LogWarning("FindObjectByPath: Provided path is null, empty, or whitespace. Returning null.");
return null;
2025-11-05 21:34:21 +08:00
}
string[] pathParts = path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
// 如果路径分割后没有有效部分(例如:"/" 或空字符串则直接返回null
if (pathParts.Length == 0)
2025-11-05 21:34:21 +08:00
{
Debug.LogWarning(
$"FindObjectByPath: Path '{path}' resulted in no valid segments after splitting. Returning null.");
return null;
}
GameObject currentObject = null;
// GetRootGameObjects() 会返回场景中所有根级别的GameObject无论它们是否激活。
Scene activeScene = SceneManager.GetActiveScene();
GameObject[] rootGameObjects = activeScene.GetRootGameObjects();
foreach (GameObject rootObj in rootGameObjects)
{
if (rootObj.name == pathParts[0])
2025-11-05 21:34:21 +08:00
{
currentObject = rootObj;
break;
2025-11-05 21:34:21 +08:00
}
}
// 如果根对象未找到,则路径无效
if (currentObject == null)
2025-11-05 21:34:21 +08:00
{
Debug.LogWarning($"FindObjectByPath: Root object '{pathParts[0]}' not found in the active scene. Returning null.");
return null;
2025-11-05 21:34:21 +08:00
}
for (int i = 1; i < pathParts.Length; i++)
2025-11-05 21:34:21 +08:00
{
if (currentObject == null)
{
Debug.LogError(
$"FindObjectByPath: Unexpected null currentObject while traversing path segment '{pathParts[i]}'. This indicates an internal logic error.");
return null;
}
// transform.Find() 能够查找包括非激活状态的子对象
Transform childTransform = currentObject.transform.Find(pathParts[i]);
if (childTransform == null)
{
Debug.LogWarning($"FindObjectByPath: Child object '{pathParts[i]}' not found under '{currentObject.name}'. Path invalid. Returning null.");
return null;
}
currentObject = childTransform.gameObject;
2025-11-05 21:34:21 +08:00
}
return currentObject;
2025-11-05 21:34:21 +08:00
}
}
}