Unity编辑器扩展 Chapter3--Create Custom Inspector
一、Create Custom Inspector
重绘inspector面板一方面是我们的挂在脚本的窗口变得友好,另一方面可以让其变得更强大,比如添加一些有效性验证。
二、重要说明
1.EditorUtility.SetDirty(object target):该方法可以表明target对象为dirty((⊙﹏⊙)b,怎么翻译合适?)的,unity会将有dirty标记的已经改变的对象在磁盘中存储,可以用来对一些修改数据的强制刷新,这里用来立即绘制GUI(我们对GUI的修改不会立即生效,只有鼠标在Scene上时才会强制重绘界面元素)。
2.对于类似GUI.Style等静态属性的修改,在修改前需要备份,使用后在设回,避免修改后的对其他无需使用修改后属性的干扰。
3.布局中的GUILayout.BeginVertical等需成对出现,有始有终。
4.该类型的脚本都需放在Editor文件夹下,unity会自动调用。
三、Code
1 using UnityEngine; 2 using System.Collections; 3 using UnityEditor; 4 5 namespace RunAndJump.LeveCreator 6 { 7 //[CanEditMultipleObjects] //希望多个挂在该类的对象可以在inspector面板选定多个一同修改的话 8 //需要将该标签添加上,由于level类一个场景下只有一个,所以无需添加该标记 9 [CustomEditor(typeof(Level))] 10 public class LevelInspector : Editor 11 { 12 private Level _myTarget; 13 private int _newTotalColumns; 14 private int _newTotalRows; 15 16 private void OnEnable() 17 { 18 Debug.Log("OnEnable was called..."); 19 _myTarget = (Level)target; 20 InitLevel(); 21 ResetResizeValues(); 22 } 23 24 private void OnDisable() 25 { 26 Debug.Log("OnDisable was called..."); 27 } 28 29 private void OnDestory() 30 { 31 Debug.Log("OnDestroy was called..."); 32 } 33 34 public override void OnInspectorGUI() 35 { 36 EditorGUILayout.LabelField("The GUI of this inspector was modified."); 37 EditorGUILayout.LabelField("The current level time is:"+_myTarget.TotalTime); 38 39 // DrawDefaultInspector();//使用unity默认的绘制方式来显示Inspector面板 40 DrawDateGUI(); 41 DrawLevelSizeGUI(); 42 43 //当使用绘制面板方法并且GUI改变时刷新GUI页面 44 if(GUI.changed) 45 EditorUtility.SetDirty(_myTarget); 46 } 47 48 private void DrawLevelSizeGUI() 49 { 50 EditorGUILayout.LabelField("Size",EditorStyles.boldLabel); 51 EditorGUILayout.BeginHorizontal("box"); 52 EditorGUILayout.BeginVertical(); 53 _newTotalColumns = EditorGUILayout.IntField("Columns", Mathf.Max(0, _newTotalColumns)); 54 _newTotalRows = EditorGUILayout.IntField("Rows", Mathf.Max(0, _newTotalRows)); 55 EditorGUILayout.EndVertical(); 56 EditorGUILayout.BeginVertical(); 57 58 bool oldEnable = GUI.enabled; 59 GUI.enabled = (_newTotalColumns != _myTarget.TotalColumns || _newTotalRows != _myTarget.TotalRows); 60 bool buttonResize = GUILayout.Button("Resize", GUILayout.Height(2*EditorGUIUtility.singleLineHeight)); 61 if (buttonResize) 62 { 63 if (EditorUtility.DisplayDialog( 64 "Level Creator", 65 "Are you sure you want to resize the leve?\nThis action cannot be undone", 66 "yes", 67 "no" 68 )) 69 { 70 ResizeLeve(); 71 } 72 73 } 74 75 bool buttonReset = GUILayout.Button("Reset"); 76 if (buttonReset) 77 { 78 ResetResizeValues(); 79 } 80 GUI.enabled = oldEnable; 81 82 EditorGUILayout.EndVertical(); 83 EditorGUILayout.EndHorizontal(); 84 } 85 86 private void InitLevel() 87 { 88 if (_myTarget.Pieces == null || _myTarget.Pieces.Length == 0) 89 { 90 Debug.Log("Initializing thie Pieces array..."); 91 _myTarget.Pieces=new LevelPiece[_myTarget.TotalColumns*_myTarget.TotalRows]; 92 } 93 } 94 95 private void ResetResizeValues() 96 { 97 _newTotalColumns = _myTarget.TotalColumns; 98 _newTotalRows = _myTarget.TotalRows; 99 } 100 101 private void ResizeLeve() 102 { 103 LevelPiece[] newPieces=new LevelPiece[_newTotalColumns*_newTotalRows]; 104 for (int col = 0; col < _myTarget.TotalColumns; ++col) 105 { 106 for (int row = 0; row < _myTarget.TotalRows; ++row) 107 { 108 if (col < _newTotalColumns && row < _newTotalRows) 109 { 110 newPieces[col + row*_newTotalColumns] = _myTarget.Pieces[col + row*_myTarget.TotalColumns]; 111 } 112 else 113 { 114 LevelPiece piece = _myTarget.Pieces[col+row*_myTarget.TotalColumns]; 115 if (piece != null) 116 { 117 //在editor模式下我们必须使用DestroyImmediate 118 Object.DestroyImmediate(piece.gameObject); 119 } 120 } 121 } 122 } 123 _myTarget.Pieces = newPieces; 124 _myTarget.TotalColumns = _newTotalColumns; 125 _myTarget.TotalRows = _newTotalRows; 126 } 127 128 private void DrawDateGUI() 129 { 130 EditorGUILayout.LabelField("Data",EditorStyles.boldLabel); 131 EditorGUILayout.BeginVertical("box"); 132 _myTarget.TotalTime = EditorGUILayout.IntField("Total Time", Mathf.Max(0, _myTarget.TotalTime)); 133 _myTarget.Gravity = EditorGUILayout.FloatField("Gravity", _myTarget.Gravity); 134 _myTarget.Bgm = (AudioClip) EditorGUILayout.ObjectField("Bgm", _myTarget.Bgm, typeof (AudioClip), false); 135 _myTarget.Background = 136 (Sprite) EditorGUILayout.ObjectField("Background", _myTarget.Background, typeof (Sprite), false); 137 EditorGUILayout.EndVertical(); 138 } 139 } 140 }
四、效果
前: 后:
点赞鼓励下,(づ ̄3 ̄)づ╭❤~
作者:世纪末的魔术师
出处:https://www.cnblogs.com/Firepad-magic/
Unity最受欢迎插件推荐:点击查看
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。