Unity Editor 基础篇(三):自定义窗口

源地址:http://gad.qq.com/article/detail/32207
终极目标

利用学到的东西制作自己的工具(自定义的窗口、Inspector、菜单、插件等等)。


准备工作

在之前的项目中,找到 Editor 文件夹,然后创建一个新的 C# 脚本,命名为“MyFirstWindow”,然后双击打开脚本,添加如下代码:

  1. using System.Collections;  
  2. using System.IO;  
  3. using UnityEditor;  
  4. using UnityEditor.SceneManagement;  
  5. using UnityEngine;  
  6. ///   
  7. ///   
  8. ///   
  9. public class MyFirstWindow : EditorWindow {  
  10.     string bugReporterName = "";  
  11.     string description = "";  
  12.     GameObject buggyGameobject;  
  13.     MyFirstWindow()  
  14.     {  
  15.         this.titleContent = new GUIContent("Bug Reporter");  
  16.     }  
  17.   
  18.     [MenuItem("Tool/Bug Reporter")]  
  19.     static void ShowWindow()  
  20.     {  
  21.         EditorWindow.GetWindow(typeof(MyFirstWindow));  
  22.     }  
  23.   
  24.     void OnGUI()  
  25.     {  
  26.         EditorGUILayout.BeginVertical();  
  27.   
  28.         GUILayout.Space(10);  
  29.         GUI.skin.label.fontSize = 24;  
  30.         GUI.skin.label.alignment = TextAnchor.MiddleCenter;  
  31.         GUILayout.Label("Bug Reporter");  
  32.   
  33.         GUILayout.Space(10);  
  34.         bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);  
  35.   
  36.   
  37.         GUILayout.Space(10);  
  38.         GUI.skin.label.fontSize = 12;  
  39.         GUI.skin.label.alignment = TextAnchor.UpperLeft;  
  40.         GUILayout.Label("Currently Scene:" EditorSceneManager.GetActiveScene().name);  
  41.   
  42.         GUILayout.Space(10);  
  43.         GUILayout.Label("Time:" System.DateTime.Now);  
  44.   
  45.         GUILayout.Space(10);  
  46.         buggyGameobject = (GameObject)EditorGUILayout.ObjectField("Buggy Gameobject", buggyGameobject,typeof(GameObject),true);  
  47.   
  48.         GUILayout.Space(10);  
  49.         GUILayout.BeginHorizontal();  
  50.         GUILayout.Label("Description",GUILayout.MaxWidth(80));  
  51.         description = EditorGUILayout.TextArea(description,GUILayout.MaxHeight(50));  
  52.         GUILayout.EndHorizontal();  
  53.   
  54.   
  55.         EditorGUILayout.Space();  
  56.         if(GUILayout.Button("Save Bug"))  
  57.         {  
  58.             SaveBug();  
  59.         }  
  60.         if(GUILayout.Button("Save Bug With Screenshot"))  
  61.         {  
  62.             SaveBugWithScreenshot();  
  63.         }  
  64.   
  65.         GUILayout.EndVertical();  
  66.     }  
  67.     void SaveBug()  
  68.     {  
  69.         Directory.CreateDirectory("Assets/BugRepots/" bugReporterName);  
  70.         StreamWriter sw = new StreamWriter("Assets/BugRepots/" bugReporterName "/"  bugReporterName ".txt");  
  71.         sw.WriteLine(bugReporterName);  
  72.         sw.WriteLine(System.DateTime.Now.ToString());  
  73.         sw.WriteLine(EditorSceneManager.GetActiveScene().name);  
  74.         sw.WriteLine(description);  
  75.         sw.Close();  
  76.   
  77.     }  
  78.     void SaveBugWithScreenshot()  
  79.     {  
  80.         Directory.CreateDirectory("Assets/BugRepots/" bugReporterName);  
  81.         StreamWriter sw = new StreamWriter("Assets/BugRepots/" bugReporterName "/"  bugReporterName ".txt");  
  82.         sw.WriteLine(bugReporterName);  
  83.         sw.WriteLine(System.DateTime.Now.ToString());  
  84.         sw.WriteLine(EditorSceneManager.GetActiveScene().name);  
  85.         sw.WriteLine(description);  
  86.         sw.Close();  
  87.         Application.CaptureScreenshot("Assets / BugRepots / " bugReporterName " / "  bugReporterName "ScreenShot" ".png");  
  88.     }  
  89. }  

代码分析 


属性



首先声明了三个变量:


    1.bugReporterName 用于储存记录Bug人的名字

    2.description 用于描述Bug信息

    3.buggyGameObject 用于储存 Bug 对象


设置窗口的名字



添加菜单栏选项 - 打开窗口({MenuItem()]下的方法必須為靜太)

绘制窗口


    绘制窗口元素需要在 OnGUI() 函数里面设计,接下来我们一一分解。



步骤:

    1.GUILayout.Space(10),这个有说过,让两个元素之间空十个像素之间的距离

    2.GUI.skin.label.fontSize 、GUI.skin.label.alignment 用于设置标题的字体大小和对齐格式;

显示当前正在编辑的场景

EditorSceneManager.GetActiveScen().name,其实就是返回当前编辑的场景信息(也就是返回 Scene 类型参数),然后利用 name 属性获取场景的名字,命名空間:using UnityEditor.SceneManagement;

绘制对象槽

  1.第一个参数用于设置卡槽的标题名字

    2.第二个参数用于设置字段显示的物体

    3.第三个参数用于设置显示的类型

    4.第四个参数用于设置是否允许指定场景中的物件


绘制描述文本区域




最终效果


最终效果

猜你喜欢

转载自blog.csdn.net/yongshuangzhao/article/details/80224608