2025-11-05 21:34:21 +08:00
|
|
|
|
using System.Collections.Generic;
|
2025-11-08 14:03:17 +08:00
|
|
|
|
using System.Linq;
|
2025-11-05 21:34:21 +08:00
|
|
|
|
using UnityEngine;
|
2025-11-08 14:03:17 +08:00
|
|
|
|
using UnityEngine.SceneManagement;
|
2025-11-05 21:34:21 +08:00
|
|
|
|
|
|
|
|
|
|
namespace UIFrame.Utilities
|
|
|
|
|
|
{
|
|
|
|
|
|
public class GameObjectTool
|
|
|
|
|
|
{
|
|
|
|
|
|
/// <summary>
|
2025-11-08 14:03:17 +08:00
|
|
|
|
/// 根据Unity对象路径查找场景中的GameObject,包括隐藏(非激活)对象。
|
|
|
|
|
|
/// 路径示例:"RootObject/ChildObject/GrandchildObject"
|
2025-11-05 21:34:21 +08:00
|
|
|
|
/// </summary>
|
2025-11-08 14:03:17 +08:00
|
|
|
|
/// <param name="path">要查找的GameObject的层级路径。</param>
|
2025-11-05 21:34:21 +08:00
|
|
|
|
/// <returns>找到的GameObject,如果未找到则返回null。</returns>
|
2025-11-08 14:03:17 +08:00
|
|
|
|
public static GameObject FindObjectByPath(string path)
|
2025-11-05 21:34:21 +08:00
|
|
|
|
{
|
2025-11-08 14:03:17 +08:00
|
|
|
|
if (string.IsNullOrWhiteSpace(path))
|
2025-11-05 21:34:21 +08:00
|
|
|
|
{
|
2025-11-08 14:03:17 +08:00
|
|
|
|
Debug.LogWarning("FindObjectByPath: Provided path is null, empty, or whitespace. Returning null.");
|
|
|
|
|
|
return null;
|
2025-11-05 21:34:21 +08:00
|
|
|
|
}
|
2025-11-08 14:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
string[] pathParts = path.Split(new char[] { '/' }, System.StringSplitOptions.RemoveEmptyEntries);
|
|
|
|
|
|
// 如果路径分割后没有有效部分(例如:"/" 或空字符串),则直接返回null
|
|
|
|
|
|
if (pathParts.Length == 0)
|
2025-11-05 21:34:21 +08:00
|
|
|
|
{
|
2025-11-08 14:03:17 +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
|
|
|
|
{
|
2025-11-08 14:03:17 +08:00
|
|
|
|
currentObject = rootObj;
|
|
|
|
|
|
break;
|
2025-11-05 21:34:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
2025-11-08 14:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
// 如果根对象未找到,则路径无效
|
|
|
|
|
|
if (currentObject == null)
|
2025-11-05 21:34:21 +08:00
|
|
|
|
{
|
2025-11-08 14:03:17 +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
|
|
|
|
}
|
2025-11-08 14:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
for (int i = 1; i < pathParts.Length; i++)
|
2025-11-05 21:34:21 +08:00
|
|
|
|
{
|
2025-11-08 14:03:17 +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
|
|
|
|
}
|
2025-11-08 14:03:17 +08:00
|
|
|
|
|
|
|
|
|
|
return currentObject;
|
2025-11-05 21:34:21 +08:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|