今天同事让我,帮他写一个自己定义编辑器Inspector属性面板显示效果,我想到的是Unity的编辑器类,它里面有好多也可以自定义效果的方法,其实主要用到的就是EditorGUI的东西,下面我将一些常见的方法贴出来,大家做为参考吧。
这个是普通的类:文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
using UnityEngine; using UnityEngine.UI; using System; public class CustomClass : MonoBehaviour { [HideInInspector] public GameObject go; [HideInInspector] public Point point; [HideInInspector] public bool on; [HideInInspector] public bool off; [HideInInspector] public Transform trans; [HideInInspector] public Vector3 position; [HideInInspector] public int index; [HideInInspector] public float flo; [HideInInspector] public Image image; // Use this for initialization void Start() { } // Update is called once per frame void Update() { } } [Serializable] public struct Point { public int x; public int y; public override string ToString() { return "(" + x + "," + y + ")"; } public Point(int x, int y) { this.x = x; this.y = y; } } |
这个是Editor类:文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
using UnityEngine; using UnityEngine.UI; using System.Collections; using UnityEditor; [CustomEditor(typeof(CustomClass))] public class CustomClassEditor : Editor { CustomClass _target; // Use this for initialization void OnEnable() { _target = (CustomClass)target; } // Update is called once per frame public override void OnInspectorGUI() { DrawDefaultInspector(); //GUI.backgroundColor = Color.red;//背景颜色 GUI.color = Color.green;//整体颜色 //GUI.contentColor = Color.yellow;//字体颜色 EditorGUILayout.BeginHorizontal(); EditorGUILayout.PrefixLabel("Point"); GUILayout.Label("X"); _target.point.x = EditorGUILayout.IntField(_target.point.x); GUILayout.Label("Y"); _target.point.y = EditorGUILayout.IntField(_target.point.y); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.on = EditorGUILayout.Toggle("On",_target.on); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.off = EditorGUILayout.Toggle("Off",_target.off); EditorGUILayout.EndHorizontal(); if (_target.on) { EditorGUILayout.BeginHorizontal(); _target.go = (GameObject)EditorGUILayout.ObjectField("Go", _target.go, typeof(GameObject), true); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.trans = (Transform)EditorGUILayout.ObjectField("Trans", _target.trans, typeof(GameObject), true); EditorGUILayout.EndHorizontal(); } EditorGUILayout.BeginHorizontal(); _target.position = EditorGUILayout.Vector3Field("Position", _target.position); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.index = EditorGUILayout.IntField("Index", _target.index); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.flo = EditorGUILayout.FloatField("Flo", _target.flo); EditorGUILayout.EndHorizontal(); EditorGUILayout.BeginHorizontal(); _target.image = (Image)EditorGUILayout.ObjectField("Go", _target.image, typeof(Image), true); EditorGUILayout.EndHorizontal(); if (GUI.changed) { EditorUtility.SetDirty(_target); } } } |
只要将Editor类放到任意目录下的Editor文件夹下,这个类就会生效。如下图:文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
最终在Inspector属性面板显示的效果如下图:文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
大家有什么不懂的,就留言吧。希望呢给大家带来帮助。文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
文章源自大腿Plus-https://www.zhaoshijun.com/archives/204
我的微信
微信扫一扫
shijun_z
我的QQ
QQ扫一扫
846207670
1F
很实用,感谢博主分享!!!