工具类~复制路径

using UnityEditor;
using UnityEngine;

public class LKPosAndEditorEditor : EditorWindow
{
    [MenuItem("Tools/LKP")]
    static void ShowWindow()
    {
        GetWindow(typeof(LKPosAndEditorEditor));
    }

    void OnGUI()
    {
        #region 位置/路径
        GUI.color = Color.red;
        if (GUILayout.Button("打印位置", GUILayout.Width(100), GUILayout.Height(20)))
            PrintGameObjectPos();
        GUI.color = Color.yellow;
        if (GUILayout.Button("复制位置", GUILayout.Width(100), GUILayout.Height(20)))
            CopyGameObjectPos();
        if (GUILayout.Button("复制全局位置", GUILayout.Width(100), GUILayout.Height(20)))
            CopyGameObjectGloalPos();
        GUI.color = Color.red;
        if (GUILayout.Button("打印路径", GUILayout.Width(100), GUILayout.Height(20)))
            PrintGameObjectPath();
        GUI.color = Color.yellow;
        if (GUILayout.Button("复制路径", GUILayout.Width(100), GUILayout.Height(20)))
            CopyGameObjectPath();
        #endregion

    }

    #region 位置/路径
    public static void PrintGameObjectPos()
    {
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("You must select Obj first!");
            return;
        }

        GameObject go = obj as GameObject;
        Debug.Log("Name: "+go.transform.name+
            "\n    pos: "+"new Vector3("+go.transform.localPosition.x+"f,"+go.transform.localPosition.y+"f,"+go.transform.localPosition.z+"f)"+
            "    rot: " + "new Vector3(" + go.transform.localEulerAngles.x + "f," + go.transform.localEulerAngles.y + "f," + go.transform.localEulerAngles.z + "f)" +
            "    scale: "+"new Vector3("+go.transform.localScale.x +"f,"+go.transform.localScale.y+"f,"+go.transform.localScale.z+"f)");
    }

    public static void PrintGameObjectPath()
    {
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("You must select Obj first!");
            return;
        }

        string result = AssetDatabase.GetAssetPath(obj);
        if (string.IsNullOrEmpty(result))   //如果不是资源则在场景中查找
        {
            Transform selectChild = Selection.activeTransform;
            if (selectChild != null)
            {
                result = selectChild.name;
                while (selectChild.parent != null)
                {
                    selectChild = selectChild.parent;
                    result = string.Format("{0}/{1}", selectChild.name, result);
                }
            }
        }

        Debug.Log("Path :    "+result);
    }


    public static void CopyGameObjectPos()
    {
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("You must select Obj first!");
            return;
        }

        GameObject go = obj as GameObject;
        string pr = "new Vector3(" + go.transform.localPosition.x + "f," + go.transform.localPosition.y + "f," + go.transform.localPosition.z + "f)"
           + ", new Vector3(" + go.transform.localEulerAngles.x + "f," + go.transform.localEulerAngles.y + "f," + go.transform.localEulerAngles.z + "f)";

        Copy(pr);
    }
    public static void CopyGameObjectGloalPos()
    {
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("You must select Obj first!");
            return;
        }

        GameObject go = obj as GameObject;
        string pr = "new Vector3(" + go.transform.position.x + "f," + go.transform.position.y + "f," + go.transform.position.z + "f)"
           + ", new Vector3(" + go.transform.eulerAngles.x + "f," + go.transform.eulerAngles.y + "f," + go.transform.eulerAngles.z + "f)";

        Copy(pr);
    }

    public static void CopyGameObjectPath()
    {
        UnityEngine.Object obj = Selection.activeObject;
        if (obj == null)
        {
            Debug.LogError("You must select Obj first!");
            return;
        }

        string result = AssetDatabase.GetAssetPath(obj);
        if (string.IsNullOrEmpty(result))   //如果不是资源则在场景中查找
        {
            Transform selectChild = Selection.activeTransform;
            if (selectChild != null)
            {
                result = selectChild.name;
                while (selectChild.parent != null)
                {
                    selectChild = selectChild.parent;
                    result = string.Format("{0}/{1}", selectChild.name, result);
                }
            }
        }

        Copy(result);
    }
    #endregion

    public static void Copy(string format, params object[] args)
    {
        string result = string.Format(format, args);
        TextEditor editor = new TextEditor();
        editor.content = new GUIContent(result);
        editor.OnFocus();
        editor.Copy();
    }
}
                              

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值