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();
}
}