- Home /
Create custom menu script in inspector, like 'Canvas Scaler (Script)'
I want to create a script in which the user can select his mode, and then according to this will appreciate different subconfigurations, such as the script Canvas Scaler.
Thanks
Answer by SirPaddow · Aug 01, 2019 at 09:57 AM
Dropdowns like this are the result of enums. If you want to show/hide other parameters depending on the value selected, you have to create a custom editor
https://docs.unity3d.com/Manual/editor-CustomEditors.html
Here's an exemple MonoBehaviour:
using UnityEngine;
public class MyClass : MonoBehaviour
{
public enum MyEnum
{
First, Second
}
public MyEnum myEnumValue;
public bool relatedToFirstOnly;
public int relatedToSecondOnly;
}
And a custom editor:
using UnityEditor;
[CustomEditor(typeof(MyClass))]
public class MyClassCustomEditor : Editor
{
public override void OnInspectorGUI()
{
MyClass myClassItem = this.target as MyClass;
EditorGUILayout.PropertyField(this.serializedObject.FindProperty("myEnumValue"));
if (myClassItem.myEnumValue == MyClass.MyEnum.First)
{
EditorGUILayout.PropertyField(this.serializedObject.FindProperty("relatedToFirstOnly"));
}
else if (myClassItem.myEnumValue == MyClass.MyEnum.Second)
{
EditorGUILayout.PropertyField(this.serializedObject.FindProperty("relatedToSecondOnly"));
}
serializedObject.ApplyModifiedProperties();
}
}
It works, perfect. Thank you.
Is there a way to make some variables always show up automatically?
You mean, shown no matter which option is selected? If yes, all you need to do is to move the "EditorGUILayout.PropertyField" line outside the condition.
Your answer
Follow this Question
Related Questions
Multiple Cars not working 1 Answer
Custom Inspector, duplicates variables ? 1 Answer
Distribute terrain in zones 3 Answers
How can I create a list of preset class/objects to be used in the editor 1 Answer
How would I go about creating a custom Unity Event in a Custom Unity Editor/Inspector? 0 Answers