最终效果:
using System; using System.Collections; using System.Collections.Generic; using UnityEditor; using UnityEngine; [CustomEditor(typeof(BaseAttribute))] //需要重写的类 public class MyTouchJoystick : Editor { BaseAttribute baseAttribute; bool isShowBackSetting; bool isShowForwardSetting; void OnEnable() { //获取当前编辑自定义的Inspector的对象 baseAttribute = (BaseAttribute)target; } //自定义检视面板 public override void OnInspectorGUI() { //设置整个布局为垂直布局 EditorGUILayout.BeginVertical(); //空行 EditorGUILayout.Space(); EditorGUILayout.Space(); EditorGUILayout.Space(); //绘制摇杆的背景属性 isShowBackSetting = EditorGUILayout.Foldout(isShowBackSetting,"BackSprite:"); if (isShowBackSetting) { //绘制背景 baseAttribute.BackSprite = (Sprite)EditorGUILayout.ObjectField("Texture:", baseAttribute.BackSprite, typeof(Sprite), true); //水平绘制大小属性面板 EditorGUILayout.LabelField("Size:"); EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); //绘制标签 EditorGUILayout.LabelField("Width:", GUILayout.MaxWidth(50)); //绘制可以输入的float类型 baseAttribute.BackSpriteWidth = EditorGUILayout.FloatField(baseAttribute.BackSpriteWidth); EditorGUILayout.LabelField("Height:", GUILayout.MaxWidth(50)); baseAttribute.BackSpriteHeight = EditorGUILayout.FloatField(baseAttribute.BackSpriteHeight); EditorGUILayout.EndHorizontal(); } isShowForwardSetting = EditorGUILayout.Foldout(isShowForwardSetting, "ForwardSprite:"); if(isShowForwardSetting) { //绘制前景 baseAttribute.ForwardSprite = (Sprite)EditorGUILayout.ObjectField("Texture:", baseAttribute.ForwardSprite, typeof(Sprite), true); //水平绘制大小属性面板 EditorGUILayout.LabelField("Size:"); EditorGUILayout.BeginHorizontal(); EditorGUILayout.Space(); EditorGUILayout.LabelField("Width:", GUILayout.MaxWidth(50)); baseAttribute.ForwardSpriteWidth = EditorGUILayout.FloatField(baseAttribute.BackSpriteWidth); EditorGUILayout.LabelField("Height:", GUILayout.MaxWidth(50)); baseAttribute.ForwardSpriteHeight = EditorGUILayout.FloatField(baseAttribute.BackSpriteHeight); EditorGUILayout.EndHorizontal(); } } }
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; [ExecuteInEditMode] //不需要运行游戏即可控制图片切换 public class BaseAttribute : MonoBehaviour { //背景属性 public Sprite BackSprite; public float BackSpriteWidth=100; public float BackSpriteHeight=100; //前景属性 public Sprite ForwardSprite; public float ForwardSpriteWidth; public float ForwardSpriteHeight; void Update() { SettingBackImage(); } void SettingBackImage() { this.GetComponent<Image>().sprite = BackSprite; } }
1.Vertical-垂直布局
默认的界面布局就是垂直布局,但是为了节目效果,我们还是把它写上比较好,设置元素为垂直布局需使用这对兄弟来声明:
EditorGUILayout.BeginVertical();
EditorGUILayout.EndVertical();
2.Horizontal-水平布局
设置元素为水平布局需使用这对兄弟来声明:
EditorGUILayout.BeginHorizontal();
EditorGUILayout.EndHorizontal();
3.空行
使用 EditorGUILayout.Space() 可在两个元素之间空出一行。
4.绘制各种类型字段
绘制字段用到以下几个方法:
EditorGUILayout.LabelField()标签字段
EditorGUILayout.IntField() 整数字段
EditorGUILayout.FloatField() 浮点数字段
EditorGUILayout.TextField() 文本字段
EditorGUILayout.Vector2Field() 二维向量字段
EditorGUILayout.Vector3Field() 三维向量字段
EditorGUILayout.Vector4Field() 四维向量字段
EditorGUILayout.ColorField() 颜色字段
5.滑块、进度条
1/ 滑块:EditorGUILayout.Slider()
制作一个滑动条用户可以拖动来改变值,在最小和最大值之间
2/ 进度条:EditorGUI.ProgressBar()
6.帮助框
帮助框:EditorGUILayout.HelpBox()
来源:http://blog.csdn.net/qq_33337811/article/details/62227146