在这次的项目中需要配置一些参数,在配置部分内容的时候需要复制和黏贴部分物体的世界坐标,但是有些物体是其他物体的子物体,无法直接的看到他们的世界坐标,所以我自己写了一个小工具用来实现这个功能,类似的扩展原理是一样的,抛砖引玉。
效果
直接上代码
using UnityEngine;
using System.Collections;
using UnityEditor;
using System.Reflection;
using System.Text;
/// <summary>
/// 扩展Transform,添加复制坐标和旋转的按钮
/// </summary>
[CustomEditor(typeof(Transform))]
public class MyTools : Editor {
private Editor editor;
private Transform transform;
void OnEnable()
{
editor = CreateEditor(target, Assembly.GetAssembly(typeof(Editor)).GetType("UnityEditor.TransformInspector", true));
}
public override void OnInspectorGUI()
{
transform = target as Transform;
editor.OnInspectorGUI();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("复制坐标(world)")) {
TextEditor textEd = new TextEditor();
StringBuilder str = new StringBuilder();
str.Append(transform.position.x + ",");
str.Append(transform.position.y + ",");
str.Append(transform.position.z);
textEd.text = str.ToString();
textEd.OnFocus();
textEd.Copy();
}
if (GUILayout.Button("复制旋转(world)"))
{
TextEditor textEd = new TextEditor();
StringBuilder str = new StringBuilder();
str.Append(transform.rotation.eulerAngles.x + ",");
str.Append(transform.rotation.eulerAngles.y + ",");
str.Append(transform.rotation.eulerAngles.z);
textEd.text = str.ToString();
textEd.OnFocus();
textEd.Copy();
}
if (GUILayout.Button("复制两者(world)"))
{
TextEditor textEd = new TextEditor();
StringBuilder str = new StringBuilder();
str.Append(transform.position.x + ",");
str.Append(transform.position.y + ",");
str.Append(transform.position.z + "|");
str.Append(transform.rotation.eulerAngles.x + ",");
str.Append(transform.rotation.eulerAngles.y + ",");
str.Append(transform.rotation.eulerAngles.z);
textEd.text = str.ToString();
textEd.OnFocus();
textEd.Copy();
}
EditorGUILayout.EndHorizontal();
EditorGUILayout.BeginHorizontal();
if (GUILayout.Button("黏贴坐标(world)"))
{
Vector3 vec = changeVec3Str(GUIUtility.systemCopyBuffer);
if (vec.Equals(Vector3.zero))
return;
transform.position = vec;
}
if (GUILayout.Button("黏贴旋转(world)"))
{
Vector3 qua = changeVec3Str(GUIUtility.systemCopyBuffer);
if (qua.Equals(Vector3.zero))
return;
transform.rotation = Quaternion.Euler(qua);
}
if (GUILayout.Button("黏贴两者(world)"))
{
string[] bothStr = GUIUtility.systemCopyBuffer.Split('|');
if (bothStr.Length == 2) {
string posStr = bothStr[0];
Vector3 pos = changeVec3Str(posStr);
if (!pos.Equals(Vector3.zero))
{
transform.position = pos;
}
string quaStr = bothStr[1];
Vector3 qua = changeVec3Str(quaStr);
if (!qua.Equals(Vector3.zero))
{
transform.rotation = Quaternion.Euler(qua);
}
}
}
EditorGUILayout.EndHorizontal();
}
//1,1,1
Vector3 changeVec3Str(string str) {
str = str.Replace("(", "");
str = str.Replace(")", "");
str = str.Replace(" ", "");
string[] strs = str.Split(',');
if (strs.Length != 3)
{
return Vector3.zero;
}
else {
float x = float.Parse(strs[0].ToString());
float y = float.Parse(strs[1].ToString());
float z = float.Parse(strs[2].ToString());
return new Vector3(x, y, z);
}
}
1,1,1,1
//Quaternion changeQuaStr(string str) {
// str = str.Replace("(", "");
// str = str.Replace(")", "");
// str = str.Replace(" ", "");
// string[] strs = str.Split(',');
// if (strs.Length < 4)
// {
// return Quaternion.identity;
// }
// else
// {
// float x = float.Parse(strs[0].ToString());
// float y = float.Parse(strs[1].ToString());
// float z = float.Parse(strs[2].ToString());
// float w = float.Parse(strs[3].ToString());
// return new Quaternion(x, y, z, w);
// }
//}
}