【Unity C#_菜单Window开发系列_Inspector Component UnityEditor开发】

简介: 【Unity C#_菜单Window开发系列_Inspector Component UnityEditor开发】

1.枚举菜单实现


文件1:Assets/MyScript/Test1.cs


代码如下:

uusing System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test1 : MonoBehaviour
{
    public Enum4 mEnum;
    public int mInt;
    public float mFloat;
    public string mStr;
    public Color mColor;
    // Start is called before the first frame update
    void Start()
    {
        
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

public enum Enum4
{
    None,
    IntVal,
    FloatVal,
    StrVal,
    ColorVal
}

文件2:Assets/MyScript/Editor/Test1Editor.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test1),true)]
public class Test4Editor : Editor
{
    public SerializedObject mObj;
    public SerializedProperty mEnum;
    public SerializedProperty mInt;
    public SerializedProperty mFloat;
    public SerializedProperty mStr;
    public SerializedProperty mColor;
    public void OnEnable()
    {
        this.mObj = new SerializedObject(target);
        this.mEnum = this.mObj.FindProperty("mEnum");
        this.mInt = this.mObj.FindProperty("mInt");
        this.mFloat = this.mObj.FindProperty("mFloat");
        this.mStr = this.mObj.FindProperty("mStr");
        this.mColor = this.mObj.FindProperty("mColor");
    }
    public override void OnInspectorGUI()
    {
        this.mObj.Update();
        EditorGUILayout.PropertyField(this.mEnum);
        switch (this.mEnum.enumValueIndex)
        {
            case 1:
                EditorGUILayout.PropertyField(this.mInt);
                break;
            case 2:
                EditorGUILayout.PropertyField(this.mFloat);
                break;
            case 3:
                EditorGUILayout.PropertyField(this.mStr);
                break;
            case 4:
                EditorGUILayout.PropertyField(this.mColor);
                break;
        }
        this.mObj.ApplyModifiedProperties();
    }
}

测试一下


新建一个场景,新建一个Empty 节点,用来测试枚举组件

将文件1:Assets/MyScript/Test1.cs拖到Game Object的Inspector面板上。

实现了一个简单的枚举菜单:


2.Window窗口菜单实现


窗口菜单实现1——显示窗口:


文件:Assets/MyScript/Test2Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test2Window : EditorWindow
{
    [MenuItem("测试2/ShowWindow")]
    public static void ShowWindow()
    {
        Test2Window.CreateInstance<Test2Window>().Show();
    }
}

测试一下


保存文件后,在窗口左边有"测试2/ShowWindow"菜单选项


如下:

打开"测试2/ShowWindow"窗口,如下:

窗口菜单实现2——弹出类型:


文件:Assets/MyScript/Test3Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test3Window : EditorWindow
{
    [MenuItem("测试2/Test3Window")]
    public static void ShowWindow()
    {
        Test3Window.CreateInstance<Test3Window>().ShowUtility();
    }
}

测试一下


打开"测试2/Test3Window"窗口,如下:

窗口菜单实现3——浮动工具窗口:


文件:Assets/MyScript/Test4Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test4Window : EditorWindow
{
    [MenuItem("测试2/Test4Window")]
    public static void ShowWindow()
    {
        Test4Window.CreateInstance<Test4Window>().ShowPopup();
    }

    public void OnGUI()
    {
        if(GUILayout.Button("关闭"))
        {
            this.Close();
        }
    }
}

测试一下


打开"测试2/Test4Window"窗口,如下:


3.Window窗口文本与颜色


文件:Assets/MyScript/Test6Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test6Window : EditorWindow
{
    [MenuItem("测试2/Test6Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test6Window>().Show();
    }
    public string mText = "默认文本";
    public Color mColor = Color.white;
    public void OnGUI()
    {
        if (GUILayout.Button("关闭"))
        {
            this.Close();
        }
        this.mText = EditorGUILayout.TextField(this.mText);
        this.mText = EditorGUILayout.TextArea(this.mText);
        this.mText = EditorGUILayout.PasswordField(this.mText);

        this.mColor = EditorGUILayout.ColorField(this.mColor);
//EditorGUILayout 后面的关键字:TextField、TextArea、PasswordField和ColorField。

    }
}

测试一下


打开"测试2/Test6Window"窗口,如下:

窗口文本与颜色关键字:TextField、TextArea、PasswordField和ColorField。


4.Window窗口标签字段


文件:Assets/MyScript/Test7Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test7Window : EditorWindow
{
    [MenuItem("测试2/Test7Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test7Window>().Show();
    }
    public string mText = "默认文本";
    public Color mColor = Color.white;
    public void OnGUI()
    {
        EditorGUILayout.LabelField("文本输入框");
        this.mText = EditorGUILayout.TextField(this.mText);
        EditorGUILayout.Space(20);
        this.mText = EditorGUILayout.TextArea(this.mText);
        EditorGUILayout.SelectableLabel("密码输入框");
        this.mText = EditorGUILayout.PasswordField(this.mText);

        this.mColor = EditorGUILayout.ColorField(this.mColor);
    }
}

测试一下


打开"测试2/Test7Window"窗口,如下:

窗口标签字段关键字:LabelField(“文本输入框”);和Space(20);


5.Window窗口滑动条


文件:Assets/MyScript/Test8Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test8Window : EditorWindow
{
    [MenuItem("测试2/Test8Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test8Window>().Show();
    }
    public int mInt;
    public float mFloat;

    public float mMinFloat;
    public float mMaxFloat;
    public void OnGUI()
    {
        EditorGUILayout.LabelField("浮点值滑动条0-100");
        this.mFloat = EditorGUILayout.Slider(this.mFloat, 0, 100);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("整数值滑动条0-100");
        this.mInt = EditorGUILayout.IntSlider(this.mInt, 0, 100);

        EditorGUILayout.Space(30);
        EditorGUILayout.LabelField("最小值和最大值滑动条");
        this.mMinFloat = EditorGUILayout.Slider(this.mMinFloat, 0, 100);
        this.mMaxFloat = EditorGUILayout.Slider(this.mMaxFloat, 0, 100);
        EditorGUILayout.MinMaxSlider(ref this.mMinFloat, ref this.mMaxFloat, 0, 100);
    }
}

测试一下


打开"测试2/Test8Window"窗口,如下:

窗口标签字段关键字:Slider、IntSlider和EditorGUILayout.MinMaxSlider(ref this.mMinFloat, ref this.mMaxFloat, 0, 100);


6.Window三维四维数组


文件:Assets/MyScript/Test9Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test9Window : EditorWindow
{
    [MenuItem("测试2/Test9Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test9Window>().Show();
    }
    public Vector2 mPos2;
    public Vector3 mPos3;
    public Vector4 mPos4;
    public Rect mRect;
    public Bounds mBounds;


    public void OnGUI()
    {
        this.mPos2 = EditorGUILayout.Vector2Field("二维数值",this.mPos2);
        this.mPos3 = EditorGUILayout.Vector3Field("三维数值",this.mPos3);
        this.mPos4 = EditorGUILayout.Vector4Field("四维数值",this.mPos4);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("矩阵");
        this.mRect = EditorGUILayout.RectField(this.mRect);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("间距");
        this.mBounds = EditorGUILayout.BoundsField(this.mBounds);
    }
}

测试一下


打开"测试2/Test9Window"窗口,如下:

窗口标签字段关键字:Vector4Field、RectField和BoundsField


7.Window标签/层和对象选择


文件:Assets/MyScript/Test10Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test10Window : EditorWindow
{
    [MenuItem("测试2/Test10Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test10Window>().Show();
    }
    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

    public void OnGUI()
    {
        EditorGUILayout.LabelField("Tag");
        this.mStr = EditorGUILayout.TagField(this.mStr);
        EditorGUILayout.Space(170);
        EditorGUILayout.LabelField("Layer");
        this.mInt = EditorGUILayout.LayerField(this.mInt);
        EditorGUILayout.Space(150);
        EditorGUILayout.LabelField("Camera");
        this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Transform");
        this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Texture");
        this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Object_场景和资源的都可选");
        this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
    }
}

测试一下


打开"测试2/Test10Window"窗口,如下:


8.Window实现Bool和折叠框


文件:Assets/MyScript/Test11Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test11Window : EditorWindow
{
    [MenuItem("测试2/Test11Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test11Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

    public void OnGUI()
    {
        this.mBool1 = EditorGUILayout.Toggle("是否开启", this.mBool1);
        if (this.mBool1)
        {
            EditorGUILayout.LabelField("Tag");
            this.mStr = EditorGUILayout.TagField(this.mStr);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Layer");
            this.mInt = EditorGUILayout.LayerField(this.mInt);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Camera");
            this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2 , "是否折叠");
        if (this.mBool2)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Transform");
            this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Texture");
            this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Object_场景和资源的都可选");
            this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
        }
    }
}

测试一下


打开"测试2/Test11Window"窗口,如下:

Bool和折叠框实现结构:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test11Window : EditorWindow
{
    [MenuItem("测试2/Test11Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test11Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
...
    public void OnGUI()
    {
        this.mBool1 = EditorGUILayout.Toggle("是否开启", this.mBool1);
        if (this.mBool1)
        {
...
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2 , "是否折叠");
        if (this.mBool2)
        {
...
        }
    }
}


9.Window实现滑动条和禁用置灰选项


文件:Assets/MyScript/Test12Window.cs


代码如下:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test12Window : EditorWindow
{
    [MenuItem("测试2/Test12Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test12Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
    public bool mBool3;

    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

public Vector2 mPos;
    public void OnGUI()
    {
        this.mPos = EditorGUILayout.BeginScrollView(this.mPos);
        this.mBool1 = EditorGUILayout.Toggle("是否开启", this.mBool1);
        if (this.mBool1)
        {
            EditorGUILayout.LabelField("Tag");
            this.mStr = EditorGUILayout.TagField(this.mStr);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Layer");
            this.mInt = EditorGUILayout.LayerField(this.mInt);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Camera");
            this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2, "是否折叠");
        if (this.mBool2)
        {
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Transform");
            this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Texture");
            this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Object_场景和资源的都可选");
            this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
        }

        this.mBool3 = EditorGUILayout.BeginToggleGroup("是否禁用置灰", this.mBool3);
        EditorGUILayout.LabelField("Tag");
        this.mStr = EditorGUILayout.TagField(this.mStr);
        EditorGUILayout.LabelField("Layer");
        this.mInt = EditorGUILayout.LayerField(this.mInt);
        EditorGUILayout.LabelField("Camera");
        this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        EditorGUILayout.EndToggleGroup();
        EditorGUILayout.EndScrollView();
    }
}

测试一下


打开"测试2/Test12Window"窗口,如下:

窗口右侧滑动条实现结构
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test12Window : EditorWindow
{
    [MenuItem("测试2/Test12Window")]
    public static void ShowWindow()
    {
        EditorWindow.GetWindow<Test12Window>().Show();
    }
    public Object mObj4;

public Vector2 mPos;
    public void OnGUI()
    {
        this.mPos = EditorGUILayout.BeginScrollView(this.mPos);//窗口右侧滑动条开始
...//中间包含的菜单
        EditorGUILayout.EndScrollView();//窗口右侧滑动条结束

    }
}

是否禁用置灰实现结构
        this.mBool3 = EditorGUILayout.BeginToggleGroup("是否禁用置灰", this.mBool3);
        ...
        EditorGUILayout.EndToggleGroup();
        ...
相关文章
|
4天前
|
前端开发 JavaScript 安全
C#一分钟浅谈:Blazor WebAssembly 开发
Blazor WebAssembly 是一个客户端框架,允许开发者使用C#和Razor语法构建Web应用。本文介绍了Blazor WebAssembly的基本概念、常见问题及解决方案,包括路由配置、数据绑定、异步操作、状态管理和性能优化等方面的内容,并分享了一些易错点及如何避免的方法。希望这些内容能帮助你在Blazor WebAssembly开发中少走弯路,提高开发效率。
69 51
|
2天前
|
开发框架 缓存 .NET
C# 一分钟浅谈:Blazor Server 端开发
Blazor Server 是基于 ASP.NET Core 的框架,允许使用 C# 和 Razor 语法构建交互式 Web 应用。本文介绍 Blazor Server 的基本概念、快速入门、常见问题及解决方案,帮助开发者快速上手。涵盖创建应用、基本组件、数据绑定、状态管理、跨组件通信、错误处理和性能优化等内容。
7 1
|
3天前
|
缓存 C# 开发者
C# 一分钟浅谈:Blazor Server 端开发
本文介绍了 Blazor Server,一种基于 .NET 的 Web 开发模型,允许使用 C# 和 Razor 语法构建交互式 Web 应用。文章从基础概念、创建应用、常见问题及解决方案、易错点及避免方法等方面详细讲解,帮助开发者快速上手并提高开发效率。
16 2
|
12天前
|
测试技术 Go C#
C#一分钟浅谈:ReSharper 插件增强开发效率
【10月更文挑战第25天】ReSharper 是 JetBrains 开发的一款 Visual Studio 插件,旨在提高 .NET 开发者的生产力。它通过代码分析、重构、导航等功能,帮助开发者避免常见错误,提升代码质量和开发效率。本文将通过具体代码案例,详细介绍 ReSharper 的常见功能及其应用。
28 1
|
18天前
|
C# Python
使用wxpython开发跨平台桌面应用,对wxpython控件实现类似C#扩展函数处理的探究
【10月更文挑战第30天】使用 `wxPython` 开发跨平台桌面应用时,可以通过创建辅助类来模拟 C# 扩展函数的功能。具体步骤包括:1. 创建辅助类 `WxWidgetHelpers`;2. 在该类中定义静态方法,如 `set_button_color`;3. 在应用中调用这些方法。这种方法提高了代码的可读性和可维护性,无需修改 `wxPython` 库即可为控件添加自定义功能。但需要注意显式调用方法和避免命名冲突。
|
25天前
|
JSON C# 开发者
C#语言新特性深度剖析:提升你的.NET开发效率
【10月更文挑战第15天】C#语言凭借其强大的功能和易用性深受开发者喜爱。随着.NET平台的演进,C#不断引入新特性,如C# 7.0的模式匹配和C# 8.0的异步流,显著提升了开发效率和代码可维护性。本文将深入探讨这些新特性,助力开发者在.NET开发中更高效地利用它们。
30 1
|
1月前
|
开发框架 NoSQL MongoDB
C#/.NET/.NET Core开发实战教程集合
C#/.NET/.NET Core开发实战教程集合
|
2月前
|
物联网 C# C语言
物联网开发中C、C++和C#哪个更好用
在物联网(IoT)开发中,C、C++和C#各有优缺点,适用场景不同。C语言性能高、资源占用低,适合内存和计算能力有限的嵌入式系统,但开发复杂度高,易出错。C++支持面向对象编程,性能优秀,适用于复杂应用,但学习曲线陡峭,编译时间长。C#易于学习,与.NET框架结合紧密,适合快速开发Windows应用,但性能略低,平台支持有限。选择语言需根据具体项目需求、复杂性和团队技术栈综合考虑。
|
1月前
|
XML 存储 安全
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
C#开发的程序如何良好的防止反编译被破解?ConfuserEx .NET混淆工具使用介绍
49 0
|
2月前
|
图形学 C++ C#
Unity插件开发全攻略:从零起步教你用C++扩展游戏功能,解锁Unity新玩法的详细步骤与实战技巧大公开
【8月更文挑战第31天】Unity 是一款功能强大的游戏开发引擎,支持多平台发布并拥有丰富的插件生态系统。本文介绍 Unity 插件开发基础,帮助读者从零开始编写自定义插件以扩展其功能。插件通常用 C++ 编写,通过 Mono C# 运行时调用,需在不同平台上编译。文中详细讲解了开发环境搭建、简单插件编写及在 Unity 中调用的方法,包括创建 C# 封装脚本和处理跨平台问题,助力开发者提升游戏开发效率。
196 0