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

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

绘制描述文本区域


最终效果
最终效果
  • 2
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值