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(); 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(); 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() == null) {//如果当前没有碰撞体 Vector3 _Center = Vector3.zero;//初始化一个中心点 MeshRenderer[] MR = go.GetComponentsInChildren();//获取所有子物体的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 //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(); if (go.GetComponent()) { 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("\n\t{0}\n\t{1}\n\t{2}\n\t{3}\n\t{4}\n\t{5}\n", 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(); 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(); 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 }