首页 > 代码库 > Unity Editor自定义检视面板
Unity Editor自定义检视面板
学习了下自定义检视面板的使用,效果图如下:
先是定义一个脚本文件,我们来修饰它的检视面板:
[HelpURL("http://www.baidu.com")]public class Atr : MonoBehaviour{ public int id; public string Name; [Multiline(5)] public string BackStory; public float health; public float damage; public float weaponDamagel, weaponDamage2; public string shoeName; public int shoeSize; public string shoeType; [Space(100)] [Range(-2,5)] public int time; void Start () { health = 50; }}
然后在根目录的Editor文件夹下定义一个用来修饰上面脚本检视面板的类文件:
using UnityEngine;using System.Collections;using UnityEditor;[CustomEditor(typeof(Atr))]//需要继承自editor,并且引入UnityEditor程序集public class LearnInspector : Editor{ private Atr atr; private bool showWeapons; void OnEnable() { //获取当前自定义的Inspector对象 atr = (Atr) target; } //执行该函数来自定义检视面板 public override void OnInspectorGUI() { //不写默认是垂直布局 EditorGUILayout.BeginVertical(); //空格 EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Base Info"); atr.id = EditorGUILayout.IntField("Atr ID", atr.id); atr.Name = EditorGUILayout.TextField("Atr Name", atr.Name); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Back Story"); atr.BackStory = EditorGUILayout.TextArea(atr.BackStory, GUILayout.MinHeight(100)); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); atr.health = EditorGUILayout.Slider("Health", atr.health, 0, 100); if (atr.health < 20) { GUI.color = Color.red; } else if (atr.health>80) { GUI.color = Color.green; } else { GUI.color = Color.grey; } Rect progressRect = GUILayoutUtility.GetRect(50, 50); EditorGUI.ProgressBar(progressRect,atr.health/100.0f,"Health"); GUI.color = Color.white; EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); atr.damage = EditorGUILayout.Slider("Damage", atr.damage, 0, 20); if(atr.damage<10) { EditorGUILayout.HelpBox("伤害过低",MessageType.Error); } else if (atr.damage > 15) { EditorGUILayout.HelpBox("伤害过高",MessageType.Warning); } else { EditorGUILayout.HelpBox("伤害适中",MessageType.Info); } EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Shoe"); EditorGUILayout.BeginHorizontal(); EditorGUILayout.LabelField("Name", GUILayout.MaxWidth(50)); atr.shoeName = EditorGUILayout.TextField(atr.shoeName); EditorGUILayout.LabelField("Size", GUILayout.MaxWidth(50)); atr.shoeSize = EditorGUILayout.IntField(atr.shoeSize); EditorGUILayout.LabelField("Type", GUILayout.MaxWidth(50)); atr.shoeType = EditorGUILayout.TextField(atr.shoeType); EditorGUILayout.EndHorizontal(); EditorGUILayout.EndVertical(); }}//绘制字段用到的方法//EditorGUILayout.LabelField()标签字段//EditorGUILayout.IntField() 整数字段//EditorGUILayout.FloatField() 浮点数字段//EditorGUILayout.TextField() 文本字段//EditorGUILayout.Vector2Field() 二维向量字段//EditorGUILayout.Vector3Field() 三维向量字段//EditorGUILayout.Vector4Field() 四维向量字段
可以看出该修饰类和效果图对应的关系。我们可以方便的定义检视面板来协助游戏的开发调试,让它直观的显示出帮助消息。
更多的信息可以查看帮助文档:http://www.ceeger.com/Script/Editor/Editor.html
Unity Editor自定义检视面板
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。