[Unity C#_Menu Window Development Series_Inspector Component UnityEditor Development]

GUI series operations

    • 1. Enumeration menu implementation
      • File 1: Assets/MyScript/Test1.cs
        • code show as below:
      • File 2: Assets/MyScript/Editor/Test1Editor.cs
        • code show as below:
      • have a test
        • Create a new scene and create an Empty node to test the enumerated components.
        • Drag file 1: Assets/MyScript/Test1.cs to the Game Object’s Inspector panel.
        • Implemented a simple enumeration menu:
    • 2.Window menu implementation
      • Window menu implementation 1 – display window:
        • File: Assets/MyScript/Test2Window.cs
          • code show as below:
        • have a test
          • After saving the file, there is a “Test 2/ShowWindow” menu option on the left side of the window.
            • Open the “Test 2/ShowWindow” window, as follows:
      • Window menu implementation 2 – pop-up type:
        • File: Assets/MyScript/Test3Window.cs
          • code show as below:
        • have a test
          • Open the “Test2/Test3Window” window, as follows:
      • Window menu implementation 3 – floating tool window:
        • File: Assets/MyScript/Test4Window.cs
          • code show as below:
          • have a test
            • Open the “Test 2/Test4Window” window, as follows:
    • 3.Window text and color
      • File: Assets/MyScript/Test6Window.cs
        • code show as below:
        • have a test
          • Open the “Test2/Test6Window” window, as follows:
          • Window text and color keywords: TextField, TextArea, PasswordField and ColorField.
    • 4.Window window label field
      • File: Assets/MyScript/Test7Window.cs
        • code show as below:
        • have a test
          • Open the “Test 2/Test7Window” window, as follows:
          • Window label field keywords: LabelField(“Text input box”); and Space(20);
    • 5.Window slider
      • File: Assets/MyScript/Test8Window.cs
        • code show as below:
        • have a test
          • Open the “Test 2/Test8Window” window, as follows:
          • Window label field keywords: Slider, IntSlider and EditorGUILayout.MinMaxSlider(ref this.mMinFloat, ref this.mMaxFloat, 0, 100);
    • 6.Window three-dimensional and four-dimensional array
      • File: Assets/MyScript/Test9Window.cs
        • code show as below:
        • have a test
          • Open the “Test2/Test9Window” window, as follows:
          • Window label field keywords: Vector4Field, RectField and BoundsField
    • 7.Window label/layer and object selection
      • File: Assets/MyScript/Test10Window.cs
        • code show as below:
        • have a test
          • Open the “Test2/Test10Window” window, as follows:
    • 8.Window implements Bool and folding box
      • File: Assets/MyScript/Test11Window.cs
        • code show as below:
        • have a test
          • Open the “Test2/Test11Window” window, as follows:
          • Bool and folding box implementation structure:
    • 9.Window implements sliding bar and disables gray option
      • File: Assets/MyScript/Test12Window.cs
        • code show as below:
        • have a test
          • Open the “Test2/Test12Window” window, as follows:
          • The slide bar on the right side of the window implements the structure
          • Whether to disable the gray implementation structure

1. Enumeration menu implementation

File 1: Assets/MyScript/Test1.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Test1 : MonoBehaviour
{<!-- -->
    public Enum4 mEnum;
    public int mInt;
    public float mFloat;
    public string mStr;
    public Color mColor;
    // Start is called before the first frame update
    void Start()
    {<!-- -->
        
    }

    // Update is called once per frame
    void Update()
    {<!-- -->
        
    }
}

public enum Enum4
{<!-- -->
    None,
    IntVal,
    FloatVal,
    StrVal,
    ColorVal
}

File 2: Assets/MyScript/Editor/Test1Editor.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(Test1),true)]
public class Test4Editor : Editor
{<!-- -->
    public SerializedObject mObj;
    public SerializedProperty mEnum;
    public SerializedProperty mInt;
    public SerializedProperty mFloat;
    public SerializedProperty mStr;
    public SerializedProperty mColor;
    public void OnEnable()
    {<!-- -->
        this.mObj = new SerializedObject(target);
        this.mEnum = this.mObj.FindProperty("mEnum");
        this.mInt = this.mObj.FindProperty("mInt");
        this.mFloat = this.mObj.FindProperty("mFloat");
        this.mStr = this.mObj.FindProperty("mStr");
        this.mColor = this.mObj.FindProperty("mColor");
    }
    public override void OnInspectorGUI()
    {<!-- -->
        this.mObj.Update();
        EditorGUILayout.PropertyField(this.mEnum);
        switch (this.mEnum.enumValueIndex)
        {<!-- -->
            case 1:
                EditorGUILayout.PropertyField(this.mInt);
                break;
            case 2:
                EditorGUILayout.PropertyField(this.mFloat);
                break;
            case 3:
                EditorGUILayout.PropertyField(this.mStr);
                break;
            case 4:
                EditorGUILayout.PropertyField(this.mColor);
                break;
        }
        this.mObj.ApplyModifiedProperties();
    }
}

Test it

Create a new scene and create an Empty node to test the enumerated components

Drag file 1: Assets/MyScript/Test1.cs onto the Game Object’s Inspector panel.

Implemented a simple enumeration menu:

2.Window menu implementation

Window menu implementation 1 – display window:

File: Assets/MyScript/Test2Window.cs
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test2Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/ShowWindow")]
    public static void ShowWindow()
    {<!-- -->
        Test2Window.CreateInstance<Test2Window>().Show();
    }
}

Test it
After saving the file, there is a “Test 2/ShowWindow” menu option on the left side of the window

as follows:

Open the “Test 2/ShowWindow” window, as follows:

Window menu implementation 2-pop-up type:

File: Assets/MyScript/Test3Window.cs
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test3Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test3Window")]
    public static void ShowWindow()
    {<!-- -->
        Test3Window.CreateInstance<Test3Window>().ShowUtility();
    }
}

Test it
Open the “Test 2/Test3Window” window, as follows:

Window menu implementation 3 – floating tool window:

File: Assets/MyScript/Test4Window.cs
The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test4Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test4Window")]
    public static void ShowWindow()
    {<!-- -->
        Test4Window.CreateInstance<Test4Window>().ShowPopup();
    }

    public void OnGUI()
    {<!-- -->
        if(GUILayout.Button("Close"))
        {<!-- -->
            this.Close();
        }
    }
}

Test it
Open the “Test 2/Test4Window” window, as follows:

3.Window text and color

File: Assets/MyScript/Test6Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test6Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test6Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test6Window>().Show();
    }
    public string mText = "Default text";
    public Color mColor = Color.white;
    public void OnGUI()
    {<!-- -->
        if (GUILayout.Button("Close"))
        {<!-- -->
            this.Close();
        }
        this.mText = EditorGUILayout.TextField(this.mText);
        this.mText = EditorGUILayout.TextArea(this.mText);
        this.mText = EditorGUILayout.PasswordField(this.mText);

        this.mColor = EditorGUILayout.ColorField(this.mColor);
//Keywords behind EditorGUILayout: TextField, TextArea, PasswordField and ColorField.

    }
}

Test it
Open the “Test 2/Test6Window” window, as follows:

Window text and color keywords: TextField, TextArea, PasswordField and ColorField.

4.Window label field

File: Assets/MyScript/Test7Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test7Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test7Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test7Window>().Show();
    }
    public string mText = "Default text";
    public Color mColor = Color.white;
    public void OnGUI()
    {<!-- -->
        EditorGUILayout.LabelField("Text input box");
        this.mText = EditorGUILayout.TextField(this.mText);
        EditorGUILayout.Space(20);
        this.mText = EditorGUILayout.TextArea(this.mText);
        EditorGUILayout.SelectableLabel("Password input box");
        this.mText = EditorGUILayout.PasswordField(this.mText);

        this.mColor = EditorGUILayout.ColorField(this.mColor);
    }
}

Test it
Open the “Test 2/Test7Window” window, as follows:

Window label field keywords: LabelField(“Text input box”); and Space(20);

5.Window slider

File: Assets/MyScript/Test8Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test8Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test8Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test8Window>().Show();
    }
    public int mInt;
    public float mFloat;

    public float mMinFloat;
    public float mMaxFloat;
    public void OnGUI()
    {<!-- -->
        EditorGUILayout.LabelField("Floating point value slider 0-100");
        this.mFloat = EditorGUILayout.Slider(this.mFloat, 0, 100);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("Integer value slider 0-100");
        this.mInt = EditorGUILayout.IntSlider(this.mInt, 0, 100);

        EditorGUILayout.Space(30);
        EditorGUILayout.LabelField("Minimum and maximum value slider");
        this.mMinFloat = EditorGUILayout.Slider(this.mMinFloat, 0, 100);
        this.mMaxFloat = EditorGUILayout.Slider(this.mMaxFloat, 0, 100);
        EditorGUILayout.MinMaxSlider(ref this.mMinFloat, ref this.mMaxFloat, 0, 100);
    }
}

Test it
Open the “Test 2/Test8Window” window, as follows:

Window label field keywords: Slider, IntSlider and EditorGUILayout.MinMaxSlider(ref this.mMinFloat, ref this.mMaxFloat, 0, 100);

6.Window three-dimensional and four-dimensional array

File: Assets/MyScript/Test9Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test9Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test9Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test9Window>().Show();
    }
    public Vector2 mPos2;
    public Vector3 mPos3;
    public Vector4 mPos4;
    public Rect mRect;
    public Bounds mBounds;


    public void OnGUI()
    {<!-- -->
        this.mPos2 = EditorGUILayout.Vector2Field("two-dimensional value",this.mPos2);
        this.mPos3 = EditorGUILayout.Vector3Field("Three-dimensional value", this.mPos3);
        this.mPos4 = EditorGUILayout.Vector4Field("Four-dimensional value", this.mPos4);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("Matrix");
        this.mRect = EditorGUILayout.RectField(this.mRect);
        EditorGUILayout.Space(20);
        EditorGUILayout.LabelField("spacing");
        this.mBounds = EditorGUILayout.BoundsField(this.mBounds);
    }
}
Test it
Open the “Test 2/Test9Window” window, as follows:

Window label field keywords: Vector4Field, RectField and BoundsField

7.Window label/layer and object selection

File: Assets/MyScript/Test10Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test10Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test10Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test10Window>().Show();
    }
    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

    public void OnGUI()
    {<!-- -->
        EditorGUILayout.LabelField("Tag");
        this.mStr = EditorGUILayout.TagField(this.mStr);
        EditorGUILayout.Space(170);
        EditorGUILayout.LabelField("Layer");
        this.mInt = EditorGUILayout.LayerField(this.mInt);
        EditorGUILayout.Space(150);
        EditorGUILayout.LabelField("Camera");
        this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Transform");
        this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Texture");
        this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
        EditorGUILayout.Space();
        EditorGUILayout.LabelField("Object_scene and resource are both optional");
        this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
    }
}

Test it
Open the “Test 2/Test10Window” window, as follows:

8.Window implements Bool and folding box

File: Assets/MyScript/Test11Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test11Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test11Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test11Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

    public void OnGUI()
    {<!-- -->
        this.mBool1 = EditorGUILayout.Toggle("Whether to turn on", this.mBool1);
        if (this.mBool1)
        {<!-- -->
            EditorGUILayout.LabelField("Tag");
            this.mStr = EditorGUILayout.TagField(this.mStr);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Layer");
            this.mInt = EditorGUILayout.LayerField(this.mInt);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Camera");
            this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2, "Whether to fold");
        if (this.mBool2)
        {<!-- -->
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Transform");
            this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Texture");
            this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Object_scene and resource are both optional");
            this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
        }
    }
}

Test it
Open the “Test 2/Test11Window” window, as follows:

Please add image description

Bool and folding box implementation structure:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test11Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test11Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test11Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
...
    public void OnGUI()
    {<!-- -->
        this.mBool1 = EditorGUILayout.Toggle("Whether to turn on", this.mBool1);
        if (this.mBool1)
        {<!-- -->
...
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2, "Whether to fold");
        if (this.mBool2)
        {<!-- -->
...
        }
    }
}

9.Window implements sliding bar and disables gray option

File: Assets/MyScript/Test12Window.cs

The code is as follows:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test12Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test12Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test12Window>().Show();
    }
    public bool mBool1;
    public bool mBool2;
    public bool mBool3;

    public string mStr;
    public int mInt;
    public Object mObj1;
    public Object mObj2;
    public Object mObj3;
    public Object mObj4;

public Vector2 mPos;
    public void OnGUI()
    {<!-- -->
        this.mPos = EditorGUILayout.BeginScrollView(this.mPos);
        this.mBool1 = EditorGUILayout.Toggle("Whether to turn on", this.mBool1);
        if (this.mBool1)
        {<!-- -->
            EditorGUILayout.LabelField("Tag");
            this.mStr = EditorGUILayout.TagField(this.mStr);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Layer");
            this.mInt = EditorGUILayout.LayerField(this.mInt);
            EditorGUILayout.Space(20);
            EditorGUILayout.LabelField("Camera");
            this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        }

        this.mBool2 = EditorGUILayout.Foldout(this.mBool2, "Whether to fold");
        if (this.mBool2)
        {<!-- -->
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Transform");
            this.mObj2 = EditorGUILayout.ObjectField(this.mObj2, typeof(Transform));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Texture");
            this.mObj3 = EditorGUILayout.ObjectField(this.mObj3, typeof(Texture));
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Object_scene and resource are both optional");
            this.mObj4 = EditorGUILayout.ObjectField(this.mObj4, typeof(Object));
        }

        this.mBool3 = EditorGUILayout.BeginToggleGroup("Whether to disable graying out", this.mBool3);
        EditorGUILayout.LabelField("Tag");
        this.mStr = EditorGUILayout.TagField(this.mStr);
        EditorGUILayout.LabelField("Layer");
        this.mInt = EditorGUILayout.LayerField(this.mInt);
        EditorGUILayout.LabelField("Camera");
        this.mObj1 = EditorGUILayout.ObjectField(this.mObj1, typeof(Camera));
        EditorGUILayout.EndToggleGroup();
        EditorGUILayout.EndScrollView();
    }
}

Test it
Open the “Test 2/Test12Window” window, as follows:

Please add image description

The structure of the slide bar on the right side of the window
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;

public class Test12Window : EditorWindow
{<!-- -->
    [MenuItem("Test2/Test12Window")]
    public static void ShowWindow()
    {<!-- -->
        EditorWindow.GetWindow<Test12Window>().Show();
    }
    public Object mObj4;

public Vector2 mPos;
    public void OnGUI()
    {<!-- -->
        this.mPos = EditorGUILayout.BeginScrollView(this.mPos);//The slide bar on the right side of the window starts
...//The menu contained in the middle
        EditorGUILayout.EndScrollView();//End of the slide bar on the right side of the window

    }
}

Whether to disable the gray implementation structure
 this.mBool3 = EditorGUILayout.BeginToggleGroup("Whether to disable graying out", this.mBool3);
        ...
        EditorGUILayout.EndToggleGroup();
        ...