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

来源:互联网 发布:淘宝钻石展位好吗 编辑:程序博客网 时间:2024/05/17 06:19

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

终极目标

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

准备工作

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


using System.Collections;using System.IO;using UnityEditor;using UnityEditor.SceneManagement;using UnityEngine;/// <summary>/// /// </summary>public class MyFirstWindow : EditorWindow {    string bugReporterName = "";    string description = "";    GameObject buggyGameobject;    MyFirstWindow()    {        this.titleContent = new GUIContent("Bug Reporter");    }    [MenuItem("Tool/Bug Reporter")]    static void ShowWindow()    {        EditorWindow.GetWindow(typeof(MyFirstWindow));    }    void OnGUI()    {        EditorGUILayout.BeginVertical();        GUILayout.Space(10);        GUI.skin.label.fontSize = 24;        GUI.skin.label.alignment = TextAnchor.MiddleCenter;        GUILayout.Label("Bug Reporter");        GUILayout.Space(10);        bugReporterName = EditorGUILayout.TextField("Bug Name", bugReporterName);        GUILayout.Space(10);        GUI.skin.label.fontSize = 12;        GUI.skin.label.alignment = TextAnchor.UpperLeft;        GUILayout.Label("Currently Scene:"+EditorSceneManager.GetActiveScene().name);        GUILayout.Space(10);        GUILayout.Label("Time:"+System.DateTime.Now);        GUILayout.Space(10);        buggyGameobject = (GameObject)EditorGUILayout.ObjectField("Buggy Gameobject", buggyGameobject,typeof(GameObject),true);        GUILayout.Space(10);        GUILayout.BeginHorizontal();        GUILayout.Label("Description",GUILayout.MaxWidth(80));        description = EditorGUILayout.TextArea(description,GUILayout.MaxHeight(50));        GUILayout.EndHorizontal();        EditorGUILayout.Space();        if(GUILayout.Button("Save Bug"))        {            SaveBug();        }        if(GUILayout.Button("Save Bug With Screenshot"))        {            SaveBugWithScreenshot();        }        GUILayout.EndVertical();    }    void SaveBug()    {        Directory.CreateDirectory("Assets/BugRepots/"+bugReporterName);        StreamWriter sw = new StreamWriter("Assets/BugRepots/"+bugReporterName+"/"+ bugReporterName+".txt");        sw.WriteLine(bugReporterName);        sw.WriteLine(System.DateTime.Now.ToString());        sw.WriteLine(EditorSceneManager.GetActiveScene().name);        sw.WriteLine(description);        sw.Close();    }    void SaveBugWithScreenshot()    {        Directory.CreateDirectory("Assets/BugRepots/"+bugReporterName);        StreamWriter sw = new StreamWriter("Assets/BugRepots/"+bugReporterName+"/"+ bugReporterName+".txt");        sw.WriteLine(bugReporterName);        sw.WriteLine(System.DateTime.Now.ToString());        sw.WriteLine(EditorSceneManager.GetActiveScene().name);        sw.WriteLine(description);        sw.Close();        Application.CaptureScreenshot("Assets / BugRepots / "+bugReporterName+" / "+ bugReporterName+"ScreenShot"+".png");    }}

代码分析 


属性



首先声明了三个变量:


    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.第四个参数用于设置是否允许指定场景中的物件


绘制描述文本区域




最终效果


最终效果

阅读全文
1 0
原创粉丝点击