using UnityEditor; using UnityEngine; public class CopySelectedPath { [MenuItem("Tools/Screenshot &#S")] static void Screenshot() { string path = EditorUtility.SaveFilePanel("Save Screenshot", "", "screenshot", "png"); if (!string.IsNullOrEmpty(path)) { ScreenCapture.CaptureScreenshot(path); } } [MenuItem("GameObject/Copy Selected Object Path to Clipboard")] static void CopySelectedObjectPrefabPathToClipboard() { GameObject selectedObject = Selection.activeGameObject; if (selectedObject != null) { //获得选中节点在当前场景中的路径 string prefabPath = GetTransformPath(selectedObject.transform); prefabPath = prefabPath.Substring(prefabPath.IndexOf('/') + 1); EditorGUIUtility.systemCopyBuffer = prefabPath; Debug.Log("已将当前选中对象在Prefab中的路径复制到剪切板中:" + prefabPath); } else { Debug.Log("没有选中任何对象!"); } } public static string GetTransformPath(Transform transform, MonoBehaviour monoBehaviour = null) { if (transform.parent == null || transform.parent.name.Contains("Canvas") || (monoBehaviour != null && transform.GetComponent(monoBehaviour.GetType()) != null)) { return transform.name; } else { return GetTransformPath(transform.parent, monoBehaviour) + "/" + transform.name; } } [MenuItem("CONTEXT/MonoBehaviour/Get All Component")] static void GetAllGameObjects(MenuCommand command) { MonoBehaviour monoBehaviour = command.context as MonoBehaviour; if (monoBehaviour != null) { SerializedObject serializedObject = new SerializedObject(monoBehaviour); SerializedProperty property = serializedObject.GetIterator(); string str = ""; while (property.NextVisible(true)) { if (property.propertyType == SerializedPropertyType.ObjectReference && property.objectReferenceValue != null) { string type = property.type.Substring(property.type.IndexOf("$") + 1); Component gameObject = property.objectReferenceValue as Component; if (gameObject != null) { string prefabPath = GetTransformPath(gameObject.transform, monoBehaviour); prefabPath = prefabPath.Substring(prefabPath.IndexOf('/') + 1); Debug.Log("属性 " + property.name + " 对应的 Game Object:" + prefabPath); str += $" {property.name}= transform.Find(\"{prefabPath}\").GetComponent<{type}();\n"; } else { GameObject gameObject1 = property.objectReferenceValue as GameObject; if (gameObject1 != null) { string prefabPath = GetTransformPath(gameObject1.transform, monoBehaviour); prefabPath = prefabPath.Substring(prefabPath.IndexOf('/') + 1); Debug.Log("属性 " + property.name + " 对应的 Game Object:" + prefabPath); str += $" {property.name}= transform.Find(\"{prefabPath}\").gameObject;\n"; } } } } EditorGUIUtility.systemCopyBuffer = str; Debug.Log("已将当前选中对象在Prefab中的路径复制到剪切板中:\n" + str); } } }