- 解決目的: 避免手動拼寫節點路徑的時候,出現路徑錯誤導致獲取不到節點的情況。
- 解決效果: 添加如下腳本之后,將自動復制路徑到剪貼板中,在代碼中通過 ctrl+v 粘貼路徑
- 代碼如下:
public class CustomMenuItems{[MenuItem("GameObject/自定義功能/獲取GameObject路徑", false, 0)]static void GetGameObjectPath(MenuCommand menuCommand){GameObject selectedObject = menuCommand.context as GameObject;if (selectedObject != null){string path = GetFullPath(selectedObject.transform);Debug.Log($"GameObject路徑: {path}");// 復制到剪貼板EditorGUIUtility.systemCopyBuffer = path;}}// 獲取 GameObject 的完整層級路徑static string GetFullPath(Transform transform){string path = transform.name;while (transform.parent != null){transform = transform.parent;// 如果父節點名稱是 "Panel",就停止遍歷if (transform.name == "Panel"){path = transform.name + "/" + path;break;}path = transform.name + "/" + path;}return path;}}
效果如下圖所示: