How to use Unity editor script (inherited from Editor) (gradually improved)

This script is only used in the editor to better control the script (modify the specified script GUI panel, better control the target script, etc.). It is mainly displayed in Unity's script panel and inherits the Editor's script placement. Where (Assets/Editor/TileEditor.cs)

  1. The use of [CustomEditor(typeof(TileObject))] and tileObject = (TileObject)target

a.[CustomEditor(typeof(TileObject))] is placed in front of the Editor script class to specify a script. Here is the TileObject script. private void OnEnable(){tileObject = (TileObject)target} is used for initialization.

2. Commonly used functions below public override void OnInspectorGUI(){} are defined here.

a.GUILayout.Label("Tile Editor"); Define a Tile Editor label under the specified script panel (as shown in the figure)
b.editMode = EditorGUILayout.Toggle("Edit",editMode);Toggle switches between two states false true;
c. tileObject.debug = EditorGUILayout.Toggle("Debug",tileObject.debug);相同
d.string[] editDataStr = { "Dead", "Road", "Guard" };
tileObject.dataID = GUILayout.Toolbar(tileObject.dataID,editDataStr); as shown in the figure
e.EditorGUILayout.Separator(); draw a dividing line for beauty
if (GUILayout.Button("Reset")) {
tileObject.Reset();
}No need to say more about the Reset button, you know it
f.DrawDefaultInspector(); displays other properties of the target script in unity instead of having to redraw all the content on the script panel when using OnInspectorGUI. This is very important.

Guess you like

Origin blog.csdn.net/HeDanTou_/article/details/129352557