2024-12-14 18:27:59 +08:00

277 lines
10 KiB
C#
Raw Permalink 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 UnityEngine;
using System.Collections;
using UnityEditor;
using System.Collections.Generic;
[InitializeOnLoad]
[CanEditMultipleObjects]
public class GetInfo : UnityEditor.Editor
{
//[UnityEditor.MenuItem("GameObject/自定义菜单", false, 0)]
////////////////////////////
//[UnityEditor.MenuItem("GameObject/自定义菜单/拷贝带后缀", false, 0)]
//static void CopySuffix()
//{
// isCopySuffix = !isCopySuffix;
//}
//[UnityEditor.MenuItem( "GameObject/自定义菜单/拷贝不带后缀", false, 0)]
//static void CopyNoSuffix()
//{
// isCopySuffix = !isCopySuffix;
//}
//将路径复制到剪切板
[UnityEditor.MenuItem("GameObject/自定义菜单/获得路径(Ctrl+q) _%_q", false, 0)]
static void getGameObjetPath()
{
Transform trans = Selection.activeTransform;
//Debug.Log(getGameObjectPath2(trans));
string content = GetGameObjectPath(trans);
GUIUtility.systemCopyBuffer = content;
//Clipboard.SetText(GetGameObjectPath(trans));
}
static string GetGameObjectPath(Transform selTrans)
{
string Path = selTrans.name;
while (null != selTrans.parent)
{
Path = selTrans.parent.name + "/" + Path;
selTrans = selTrans.parent;
}
return Path;
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得世界坐标(Ctrl+w) _%_w", false, 1)]
static void GetPosition()
{
Transform trans = Selection.activeTransform;
if (trans)
{
Vector3 position = trans.position;
GUIUtility.systemCopyBuffer = V3toStr(position, -1);
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得相对坐标", false, 2)]
static void GetLocalPosition()
{
Transform trans = Selection.activeTransform;
if (trans)
{
Vector3 position = trans.localPosition;
GUIUtility.systemCopyBuffer = V3toStr(position, -1);
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得ForWard", false, 3)]
static void GetForward()
{
Transform trans = Selection.activeTransform;
if (trans)
{
Vector3 forword = trans.forward;
GUIUtility.systemCopyBuffer = V3toStr(forword, -1);
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得欧拉角(Ctrl+e) _%_e", false, 4)]
static void GetEuler()
{
Transform trans = Selection.activeTransform;
if (trans)
{
Quaternion quaternion = trans.rotation;
string result = V3toStr(quaternion.eulerAngles,-1);
GUIUtility.systemCopyBuffer = result;
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得BoxColliderSize", false, 5)]
static void GetBoxColliderSize()
{
Transform trans = Selection.activeTransform;
if (trans)
{
BoxCollider boxcollider = trans.GetComponent<BoxCollider>();
if (boxcollider)
{
Vector3 vBoxSize = boxcollider.size;
string result = V3toStr(vBoxSize,-1);
GUIUtility.systemCopyBuffer = result;
}
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得BoxColliderCenter", false, 6)]
static void GetBoxColliderCenter()
{
Transform trans = Selection.activeTransform;
if (trans)
{
BoxCollider boxcollider = trans.GetComponent<BoxCollider>();
if (boxcollider)
{
Vector3 vBoxCenter = boxcollider.center;
string result = V3toStr(vBoxCenter,-1);
GUIUtility.systemCopyBuffer = result;
}
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得物体名字", false, 7)]
static void GetGameObjectName()
{
Transform trans = Selection.activeTransform;
if (trans)
{
GUIUtility.systemCopyBuffer = trans.name;
}
}
[UnityEditor.MenuItem("GameObject/自定义菜单/获得物体缩放", false, 8)]
static void GetGameObjectScale()
{
Transform trans = Selection.activeTransform;
if (trans)
{
Vector3 vector3 = Vector3.zero;
vector3 = trans.localScale;
string result = V3toStr(vector3,-1);
GUIUtility.systemCopyBuffer = result;
}
}
static string V3toStr(Vector3 vector, int xiaoshu =-1)
{
string xs = "";
for(int i=0;i<xiaoshu;i++)
{
xs += "#";
}
string s1 = "{0:" + xs + "}";
string s2 = "{1:" + xs + "}";
string s3 = "{2:" + xs + "}";
if (xiaoshu==-1)
{
return vector.x.ToString() + "," + vector.y.ToString() + "," + vector.z.ToString();
}
string st = s1 + "," + s2 + "," + s3;
return string.Format(st, vector.x, vector.y, vector.z);
}
#region
[UnityEditor.MenuItem("GameObject/自定义功能/获得Device信息", false, 4)]
static void GetDevice()
{
Transform trans = Selection.activeTransform;
GUIUtility.systemCopyBuffer = GetDeviceInfo(trans,1);
}
[UnityEditor.MenuItem("GameObject/自定义功能/获得子物体Device信息", false, 4)]
static void GetDevicechild()
{
Transform trans = Selection.activeTransform;
int j = 0;
string result = "";
for (int i = 0; i < trans.childCount; i++)
{
Transform tran = trans.GetChild(i);
if (tran.parent == trans)
{
j++;
result += GetDeviceInfo(tran, j) + "\n";
}
}
GUIUtility.systemCopyBuffer = result;
}
static string GetDeviceInfo(Transform trans, int id)
{
if (trans)
{
Vector3 oldEulerAngles = trans.eulerAngles;
trans.eulerAngles = Vector3.zero;
GameObject go = trans.gameObject;//转换为gameobject
string colliderSize = "0,0,0";
string colliderCenter = "0,0,0";
if (go.GetComponent<Collider>() == null)
{//如果当前没有碰撞体
Vector3 _Center = Vector3.zero;//初始化一个中心点
MeshRenderer[] MR = go.GetComponentsInChildren<MeshRenderer>();//获取所有子物体的meshrenderer
foreach (MeshRenderer item in MR)
{//遍历
_Center += item.bounds.center;//先将所有子对象的渲染中心点拿到
}
_Center /= MR.Length;//除以子对象的数量 获得父物体的真正中心位置
Bounds bounds = new Bounds(_Center, Vector3.zero);//初始化一个bounds 中心点设为计算出的父物体中心点
foreach (MeshRenderer child in MR)
{//遍历
bounds.Encapsulate(child.bounds); //将所有子物体的buonds包含入此bounds
}
//BoxCollider BC = go.gameObject.AddComponent<BoxCollider>();//添加boxcollider
//BC.center = (bounds.center - trans.position) / go.transform.localScale.x;// 需要除以父物体缩放
Vector3 center = (bounds.center - trans.position) / go.transform.localScale.x;
//BC.size = bounds.size / go.transform.localScale.x;// 需要除以父物体缩放
Vector3 size = bounds.size / go.transform.localScale.x;
colliderSize = string.Format("{0},{1},{2}", size.x, size.y, size.z);
colliderCenter = string.Format("{0},{1},{2}", center.x, center.y, center.z);
trans.eulerAngles = oldEulerAngles;
}
else
{
BoxCollider box = go.GetComponent<BoxCollider>();
if (go.GetComponent<BoxCollider>())
{
Vector3 center = box.center;
Vector3 size = box.size;
colliderSize = string.Format("{0},{1},{2}", size.x, size.y, size.z);
colliderCenter = string.Format("{0},{1},{2}", center.x, center.y, center.z);
}
}
string sId = id.ToString();
string tip = "提示";
string path = GetGameObjectPath(trans);
string result = string.Format("<Device>\n\t<ID>{0}</ID>\n\t<Path>{1}</Path>\n\t<Tip>{2}</Tip>\n\t<ColliderSize>{3}</ColliderSize>\n\t<ColliderCenter>{4}</ColliderCenter>\n\t<TriggerDistance>{5}</TriggerDistance>\n</Device>",
sId, path, tip, colliderSize, colliderCenter, "10");
return result;
}
return "";
}
#endregion
#region UI信息
// [UnityEditor.MenuItem("GameObject/UI信息/UI信息", false, 0)]
static void GetUISize()
{
Transform trans = Selection.activeTransform;
if (trans)
{
RectTransform rectTrans = trans.GetComponent<RectTransform>();
if (rectTrans)
{
Debug.Log("屏幕:" + UnityEngine.Screen.width + "," + UnityEngine.Screen.height);
Debug.Log("UI的大小:" + rectTrans.rect.width + "," + rectTrans.rect.height);
Debug.Log("UI的位置:" + rectTrans.position);
Debug.Log("UI的相对位置:" + rectTrans.localPosition);
Debug.Log("UI的sizeDelta:" + rectTrans.sizeDelta);
Debug.Log("UI真实大小:");
GameObject obj = GameObject.Find("UGUI");
if (obj)
{
Canvas canvas = obj.GetComponent<Canvas>();
if (canvas)
{
Debug.Log("UI除以缩放因子的大小" + rectTrans.rect.width / canvas.scaleFactor + "," + rectTrans.rect.height / canvas.scaleFactor);
Debug.Log(string.Format("UI在屏幕上的大小{0},{1}", rectTrans.rect.width * canvas.scaleFactor, rectTrans.rect.height * canvas.scaleFactor));
}
}
}
}
}
#endregion
}