https://www.xuanyusong.com/archives/category/unity/unity3deditor

1

  1. using UnityEngine;
  2.  
  3. public class Test : MonoBehaviour {
  4. public Rect rect;
  5. public Texture texture;
  6. }
  7.  
  8. using UnityEngine;
  9. using UnityEditor;
  10.  
  11. [CustomEditor(typeof(Test))]
  12. public class MyEditor: Editor {
  13. public override void OnInspectorGUI() {
  14. Test test = (Test)target;
  15. test.rect = EditorGUILayout.RectField("窗口坐标", test.rect);
  16. test.texture = EditorGUILayout.ObjectField("增加一个贴图", test.texture, typeof(Texture), true) as Texture;
  17. }
  18. }

2

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class MyEditor: EditorWindow {
  5.  
  6. private string text;
  7. private Texture texture;
  8.  
  9. [MenuItem("GameObject/window")]
  10. public static void AddWindow() {
  11. Rect rect = , , , );
  12. MyEditor window = (MyEditor)EditorWindow.GetWindowWithRect(typeof(MyEditor), rect, true, "window one");
  13. window.Show();
  14. }
  15.  
  16. public void OnGUI() {
  17. text = EditorGUILayout.TextField("输入文字:", text);
  18.  
  19. ))) {
  20. ShowNotification(new GUIContent("This is a Notification"));
  21. }
  22.  
  23. ))) {
  24. this.RemoveNotification();
  25. }
  26.  
  27. EditorGUILayout.LabelField("鼠标在窗口的位置", Event.current.mousePosition.ToString());
  28.  
  29. texture = EditorGUILayout.ObjectField("添加贴图", texture, typeof(Texture), true) as Texture;
  30.  
  31. ))) {
  32. Close();
  33. }
  34. }
  35.  
  36. public void OnFocus() {
  37. Debug.Log("当窗口获得焦点时调用一次");
  38. }
  39.  
  40. public void OnLostFocus() {
  41. Debug.Log("当窗口丢失焦点时调用一次");
  42. }
  43.  
  44. public void OnHierarchyChange() {
  45. Debug.Log("当Hierarchy视图中的任何对象发生改变时调用一次");
  46. }
  47.  
  48. public void OnProjectChange() {
  49. Debug.Log("当Project视图中的资源发生改变时调用一次");
  50. }
  51.  
  52. public void OnInspectorUpdate() {
  53. // Debug.Log("窗口面板的更新");
  54. // 这里开启窗口的重绘,不然窗口信息不会刷新
  55. this.Repaint();
  56. }
  57.  
  58. public void OnSelectionChange() {
  59. foreach (Transform t in Selection.transforms) {
  60. Debug.Log("OnSelectionChange: " + t.name);
  61. }
  62. }
  63.  
  64. public void OnDestroy() {
  65. Debug.Log("当窗口关闭时调用");
  66. }
  67. }

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. [CustomEditor(typeof(Test))]
  5. public class MyEditor: Editor {
  6.  
  7. public void OnSceneGUI() {
  8. Test test = (Test)target;
  9.  
  10. Handles.Label(test.transform.position + Vector3.up * , test.transform.name + " : " + test.transform.position.ToString());
  11. Handles.BeginGUI();
  12.  
  13. GUILayout.BeginArea(, , , ));
  14.  
  15. if (GUILayout.Button("这是一个按钮!")) {
  16. Debug.Log("test");
  17. }
  18.  
  19. GUILayout.Label("我在编辑Scene视图");
  20.  
  21. GUILayout.EndArea();
  22.  
  23. Handles.EndGUI();
  24. }
  25. }
  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class MyEditor: Editor {
  5.  
  6. [DrawGizmo(GizmoType.InSelectionHierarchy | GizmoType.NotInSelectionHierarchy)]
  7. public static void DrawGameObjectName(Transform transform, GizmoType gizmoType) {
  8. Handles.Label(transform.position, transform.gameObject.name);
  9. }
  10. }

  1. using UnityEngine;
  2. using UnityEditor;
  3.  
  4. public class MyEditor: Editor {
  5.  
  6. [MenuItem("MyMenu/Do Test")]
  7. public static void Test() {
  8. Transform parent = Selection.activeGameObject.transform;
  9. Vector3 postion = parent.position;
  10. Quaternion rotation = parent.rotation;
  11. Vector3 scale = parent.localScale;
  12. parent.position = Vector3.zero;
  13. parent.rotation = Quaternion.Euler(Vector3.zero);
  14. parent.localScale = Vector3.one;
  15.  
  16. Collider[] colliders = parent.GetComponentsInChildren<Collider>();
  17. foreach (Collider child in colliders) {
  18. DestroyImmediate(child);
  19. }
  20. Vector3 center = Vector3.zero;
  21. Renderer[] renders = parent.GetComponentsInChildren<Renderer>();
  22. foreach (Renderer child in renders) {
  23. center += child.bounds.center;
  24. }
  25. center /= parent.transform.childCount;
  26. Bounds bounds = new Bounds(center, Vector3.zero);
  27. foreach (Renderer child in renders) {
  28. bounds.Encapsulate(child.bounds);
  29. }
  30. BoxCollider boxCollider = parent.gameObject.AddComponent<BoxCollider>();
  31. boxCollider.center = bounds.center - parent.position;
  32. boxCollider.size = bounds.size;
  33.  
  34. parent.position = postion;
  35. parent.rotation = rotation;
  36. parent.localScale = scale;
  37. }
  38.  
  39. }

5

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections.Generic;
  5.  
  6. [CustomEditor(typeof(UnityEditor.DefaultAsset))]
  7. public class FolderInspector : Editor {
  8.  
  9. Data data;
  10. Data selectData;
  11.  
  12. void OnEnable() {
  13. if (Directory.Exists(AssetDatabase.GetAssetPath(target))) {
  14. data = new Data();
  15. LoadFiles(data, AssetDatabase.GetAssetPath(Selection.activeObject));
  16. }
  17.  
  18. }
  19. public override void OnInspectorGUI() {
  20.  
  21. if (Directory.Exists(AssetDatabase.GetAssetPath(target))) {
  22. GUI.enabled = true;
  23. EditorGUIUtility.SetIconSize(Vector2.one * );
  24. DrawData(data);
  25. }
  26.  
  27. }
  28.  
  29. ) {
  30. GUIContent content = GetGUIContent(currentPath);
  31.  
  32. if (content != null) {
  33. data.indent = indent;
  34. data.content = content;
  35. data.assetPath = currentPath;
  36.  
  37. }
  38.  
  39. foreach (var path in Directory.GetFiles(currentPath)) {
  40. content = GetGUIContent(path);
  41. if (content != null) {
  42. Data child = new Data();
  43. child.indent = indent + ;
  44. child.content = content;
  45. child.assetPath = path;
  46. data.childs.Add(child);
  47. }
  48. }
  49.  
  50. foreach (var path in Directory.GetDirectories(currentPath)) {
  51. Data childDir = new Data();
  52. data.childs.Add(childDir);
  53. LoadFiles(childDir, path, indent + );
  54. }
  55. }
  56.  
  57. void DrawData(Data data) {
  58. if (data.content != null) {
  59. EditorGUI.indentLevel = data.indent;
  60. DrawGUIData(data);
  61.  
  62. }
  63. ; i < data.childs.Count; i++) {
  64. Data child = data.childs[i];
  65. if (child.content != null) {
  66. EditorGUI.indentLevel = child.indent;
  67. )
  68. DrawData(child);
  69. else
  70. DrawGUIData(child);
  71. }
  72. }
  73. }
  74.  
  75. void DrawGUIData(Data data) {
  76. GUIStyle style = "Label";
  77. Rect rt = GUILayoutUtility.GetRect(data.content, style);
  78. if (data.isSelected) {
  79. EditorGUI.DrawRect(rt, Color.gray);
  80. }
  81.  
  82. rt.x += ( * EditorGUI.indentLevel);
  83. if (GUI.Button(rt, data.content, style)) {
  84. if (selectData != null) {
  85. selectData.isSelected = false;
  86. }
  87. data.isSelected = true;
  88. selectData = data;
  89. Debug.Log(data.assetPath);
  90. }
  91. }
  92.  
  93. GUIContent GetGUIContent(string path) {
  94. Object asset = AssetDatabase.LoadAssetAtPath(path, typeof(Object));
  95. if (asset) {
  96. return new GUIContent(asset.name, AssetDatabase.GetCachedIcon(path));
  97. }
  98. return null;
  99. }
  100.  
  101. class Data {
  102. public bool isSelected = false;
  103. ;
  104. public GUIContent content;
  105. public string assetPath;
  106. public List<Data> childs = new List<Data>();
  107. }
  108. }

6

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System;
  5.  
  6. public class MyEditor {
  7.  
  8. [InitializeOnLoadMethod]
  9. static void Start() {
  10. Action OnEvent = delegate {
  11. Event e = Event.current;
  12.  
  13. switch (e.type) {
  14. // case EventType.mouseDown:
  15. // Debug.Log ("mousedown");
  16. // e.Use ();
  17. // break;
  18. // case EventType.mouseUp:
  19. // Debug.Log ("mouseup");
  20. // e.Use ();
  21. // break;
  22. // case EventType.MouseMove:
  23. // Debug.Log ("move");
  24. // e.Use ();
  25. // break;
  26. case EventType.DragPerform:
  27. Debug.Log("DragPerform");
  28. e.Use();
  29. break;
  30. case EventType.DragUpdated:
  31. Debug.Log("DragUpdated");
  32. e.Use();
  33. break;
  34. case EventType.DragExited:
  35. Debug.Log("DragExited");
  36. e.Use();
  37. break;
  38. }
  39. };
  40.  
  41. EditorApplication.hierarchyWindowItemOnGUI = delegate (int instanceID, Rect selectionRect) {
  42.  
  43. OnEvent();
  44.  
  45. };
  46.  
  47. EditorApplication.projectWindowItemOnGUI = delegate (string guid, Rect selectionRect) {
  48.  
  49. OnEvent();
  50. };
  51.  
  52. }
  53. }

7

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4.  
  5. public class MyHierarchyMenu {
  6. [MenuItem("Window/Test/yusong")]
  7. static void Test() {
  8. }
  9.  
  10. [MenuItem("Window/Test/momo")]
  11. static void Test1() {
  12. }
  13. [MenuItem("Window/Test/雨松/MOMO")]
  14. static void Test2() {
  15. }
  16.  
  17. [InitializeOnLoadMethod]
  18. static void StartInitializeOnLoadMethod() {
  19. EditorApplication.hierarchyWindowItemOnGUI += OnHierarchyGUI;
  20. }
  21.  
  22. static void OnHierarchyGUI(int instanceID, Rect selectionRect) {
  23. if (Event.current != null && selectionRect.Contains(Event.current.mousePosition)
  24. && Event.current.button == && Event.current.type <= EventType.MouseUp) {
  25. GameObject selectedGameObject = EditorUtility.InstanceIDToObject(instanceID) as GameObject;
  26. //这里可以判断selectedGameObject的条件
  27. if (selectedGameObject.name == "SceneRoot") {
  28. Vector2 mousePosition = Event.current.mousePosition;
  29.  
  30. EditorUtility.DisplayPopupMenu(, ), "Window/Test", null);
  31. Event.current.Use();
  32. }
  33. }
  34. }
  35.  
  36. }

  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.Collections;
  4. using System.Reflection;
  5. using UnityEngine.Profiling;
  6.  
  7. public class MyHierarchyMenu {
  8. [MenuItem("1/1")]
  9. public static void menu() {
  10. Texture target = Selection.activeObject as Texture;
  11. var type = System.Reflection.Assembly.Load("UnityEditor.dll").GetType("UnityEditor.TextureUtil");
  12.  
  13. //var type = Types.GetType("UnityEditor.TextureUtil", "UnityEditor.dll");
  14. MethodInfo methodInfo = type.GetMethod("GetStorageMemorySize", BindingFlags.Static | BindingFlags.Instance | BindingFlags.Public);
  15.  
  16. Debug.Log("内存占用:" + EditorUtility.FormatBytes(Profiler.GetRuntimeMemorySizeLong(Selection.activeObject)));
  17. Debug.Log("硬盘占用:" + EditorUtility.FormatBytes((int)methodInfo.Invoke(null, new object[] { target })));
  18. }
  19.  
  20. }

Unity编辑器扩展学习 转载的更多相关文章

  1. unity编辑器扩展学习

    扩展编辑器实际上就是在unity菜单栏中添加一些按钮,可以一键执行一些重复性的工作. 一.添加按钮 1.简单使用MenuItem特性 using UnityEngine; using UnityEdi ...

  2. Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据

    Unity编辑器扩展 Chapter7--使用ScriptableObject持久化存储数据 unity unity Editor ScirptableObject  Unity编辑器扩展 Chapt ...

  3. Unity编辑器扩展chapter1

    Unity编辑器扩展chapter1 unity通过提供EditorScript API 的方式为我们提供了方便强大的编辑器扩展途径.学好这一部分可以使我们学会编写一些工具来提高效率,甚至可以自制一些 ...

  4. Unity 编辑器扩展 场景视图内控制对象

    http://blog.csdn.net/akof1314/article/details/38129031 假设有一个敌人生成器类,其中有个属性range用来表示敌人生成的范围区域大小,那么可以用O ...

  5. unity 编辑器扩展简单入门

    unity 编辑器扩展简单入门 通过使用编辑器扩展,我们可以对一些机械的操作实现自动化,而不用使用额外的环境,将工具与开发环境融为一体:并且,编辑器扩展也提供GUI库,来实现可视化操作:编辑器扩展甚至 ...

  6. Unity编辑器扩展Texture显示选择框

    学习NGUI插件的时候,突然间有一个问题为什么它这些属性可以通过弹出窗口来选中呢? 而我自己写的组件只能使用手动拖放的方式=.=. Unity开发了组件Inspector视图扩展API,如果我们要写插 ...

  7. Unity 编辑器扩展

    自定义检视面板的使用: 先是定义一个脚本文件,我们来修饰它的检视面板: [HelpURL("http://www.baidu.com")] public class Atr : M ...

  8. Unity3d编辑器扩展学习笔记

    编辑器扩展 1.添加菜单栏:把特性应用于静态方法 参数1:菜单名的空格后面是定义快捷键(单符号得用"_"开头,组合键%=Ctrl,#=Shift,&=Alt) 参数2:通过 ...

  9. Unity编辑器扩展

    在开发中有可能需要自己开发编辑器工具,在Unity中界面扩展常见两种情况,拿某插件为例: 1,自建窗口扩展 2,脚本Inspector显示扩展 不管使用那种样式,都需要经常用到两个类EditorGUI ...

随机推荐

  1. 我说精通字符串,面试官竟然问我 Java 中的 String 有没有长度限制?

    String 是 Java 中很重要的一个数据类型,除了基本数据类型以外,String 是被使用的最广泛的了,但是,关于 String,其实还是有很多东西容易被忽略的. 就如本文我们要讨论的问题:Ja ...

  2. 15-scrapy-redis两种形式分布式爬虫

    什么叫做分布式爬虫? 分布式爬虫,就是多台机器共用一个scrapy—redis程序高效爬取数据, 为啥要用分布式爬虫? 其一:因为多台机器上部署的scrapy会各自拥有各自的调度器,这样就使得多台机器 ...

  3. 公益:开放一台Nacos服务端给各位Spring Cloud爱好者

    之前开放过一台公益Eureka Server给大家,以方便大家在阅读我博客中教程时候做实验.由于目前在连载Spring Cloud Alibaba,所以对应的也部署了一台Nacos,并且也开放出来,给 ...

  4. log4net的配置及使用

    网上查了有很多种写法和配置,结果百度出来都是几种方法混合写法,拷在一起结果还不能正常运行.因此把自己做成功的代码写上来做个备份. 运行环境:log4net 2.03版本,.net 4.5 大体步骤为: ...

  5. Z从壹开始前后端分离【 .NET Core2.0/3.0 +Vue2.0 】框架之四 || Swagger的使用 3.2

    本文梯子 本文3.0版本文章 前言 一.swagger的一般用法 0.设置swagger页面为首页——开发环境 1.设置默认直接首页访问 —— 生产环境 2.为接口添加注释 3.对 Model 也添加 ...

  6. Python【day 14-4】sorted filter map+递归文件夹+二分法查找

    def func(x): #普通函数 return x*x ret1 = func(10) #匿名函数 f = lambda x:x*x # 匿名函数写法: 匿名函数名=lambda 参数:返回值 ' ...

  7. GO基础之函数

    一.Go语言函数的格式 函数构成了代码执行的逻辑结构,在Go语言中,函数的基本组成为:关键字 func.函数名.参数列表.返回值.函数体和返回语句,每一个程序都包含很多的函数,函数是基本的代码块. 函 ...

  8. RunLoop的知识小记

    RunLoop字面上的意思是,运行循环: 其基本作用:保持程序的持续运行: 处理App中的各种事件(比如:触摸事件.定时器事件.Selector事件) 节省CPU资源,提高程序性能:该做事时做事,该休 ...

  9. java annotation使用介绍

    还望支持个人博客站:http://www.enjoytoday.cn 介绍 Annotation的中文名字叫注解,开始与JDK 1.5,为了增强xml元数据和代码的耦合性的产物.注解本身并没有业务逻辑 ...

  10. Android JS打开原生应用

    设置App通过网页JS,唤醒打开本地应用. 在AndroidManifest 中,在应用启动页配置下,添加android:exported="true",设置category 添加 ...