当前位置:网站首页>Unity3D——自定义类的Inspector面板的修改
Unity3D——自定义类的Inspector面板的修改
2022-08-11 08:04:00 【索利亚噶通】
Unity3D——自定义类的Inspector面板的修改
- 预期目标: 对自定义类的Inspector面板进行修改,达到如下效果
- 步骤
(1)创建自己的脚本(例如Help.cs),置于Assets / Scripts目录(非必须)下
(2)创建与其对应的脚本(HelpInspector.cs), 置于 Assets / Editor 目录(没有可自行创建)下
(3)在SudentInspector.cs中
- 引用UnityEditor命名空间
- 添加特性[CutomEditor(typeof(Help))]
- 继承Editor类
- 重写OnInspectorGUI类
注意:这里重写OnInspectorGUI方法时,编译器自带调用父类中的OnInspectorGUI,这里如果不想要Help类中的不需要的public变量出现在Inspector面板上,就将base.OnInspectorGUI()注释掉
代码
// SudentInspector.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Help))]
public class HelpInspector : Editor
{
public override void OnInspectorGUI()
{
Help myHelp = (Help)target; // target就是Help Inspector的实例
GUILayout.BeginVertical();
GUILayout.Label("Help Here");
GUILayout.Label("Version: 1.0.0");
GUILayout.EndVertical();
GUILayout.BeginHorizontal();
GUILayout.Label("Test Input", GUILayout.Width(60));
myHelp.helpId = EditorGUILayout.IntField( myHelp.helpId,GUILayout.Width(150));
GUILayout.EndHorizontal();
GUILayout.BeginVertical();
myHelp.helpName = EditorGUILayout.TextField(myHelp.helpName, GUILayout.Width(150));
GUILayout.EndVertical();
}
}
// Help.cs
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class Help : MonoBehaviour
{
public bool helpHere;
public int helpId;
public string helpName;
}
功能
- 注意
- 这里的target就是指Help创建出来的Help实例(面板),如果想要控制Help中的变量,不能再创建新的实例,因为我们是去操控面板上这个已经创建出来的实例,也就是target
边栏推荐
- Redis source code-String: Redis String command, Redis String storage principle, three encoding types of Redis string, Redis String SDS source code analysis, Redis String application scenarios
- 如何通过 IDEA 数据库管理工具连接 TDengine?
- Swagger简单使用
- JUC并发编程
- 【LeetCode】Summary of linked list problems
- matplotlib
- 【43. 字符串相乘】
- 机器学习(二)线性回归
- 《剑指offer》题解——week3(持续更新)
- Pico neo3 Unity Packaging Settings
猜你喜欢
随机推荐
1061 True or False (15 points)
Find the latest staff salary and the last staff salary changes
Break pad source code compilation--refer to the summary of the big blogger
1096 big beautiful numbers (15 points)
项目2-年收入判断
项目1-PM2.5预测
2022 China Soft Drink Market Insights
如何仅更改 QGroupBox 标题的字体?
Kaldi语音识别工具编译问题记录(踩坑记录)
The growth path of a 40W test engineer with an annual salary, which stage are you in?
1036 跟奥巴马一起编程 (15 分)
初级软件测试工程师笔试试题,你知道答案吗?
pyqt5实现仪表盘
流式结构化数据计算语言的进化与新选择
Hibernate 的 Session 缓存相关操作
RestTemplate工具类
C Primer Plus(6) 中文版 第1章 初识C语言 1.6 语言标准
分布式锁-Redission - 缓存一致性解决
C语言-结构体
借问变量何处存,牧童笑称用指针,Go lang1.18入门精炼教程,由白丁入鸿儒,go lang类型指针(Pointer)的使用EP05