Files
MinFt/Client/Assets/Editor/CopySelectedPath.cs
2026-04-27 12:07:32 +08:00

85 lines
3.6 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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);
}
}
}