- Home /
Custom inspector editor - how to put new editor fields in a specific place
Hi everyone :)
I'm trying to get new editor fields when a bool is checked in the inspector. While I did manage to do that, I don't know how to make it show right below the boolean you had to check and not at the bottom of the inspector.
Here is my code:
override public void OnInspectorGUI()
{
base.OnInspectorGUI ();
var myScript = target as CardAsset;
if (myScript.KillEffect) {
Debug.Log ("KillEffect is true");
EditorGUILayout.LabelField ("TEST");
}
}
Any help is really appreciated :)
You are saying it's showing at the bottom because there are more inspector variables from your other script?
Yes. base.OnInspectorGUI (); is where all others variables are co$$anonymous$$g (from my ScriptableObject). I would like to put the new label between existing vars and i don't know how
In this case, your custom editor is going to have to show those other variables, ins$$anonymous$$d of doing a DrawDefaultInspector(); Its more work.. I can show you an example if you wish.
I don't think you can insert a single field into an existing inspector GUI. You only have access to its base.OnInspectorGUI() as you've seen, which just draws the entire default inspector block.
I think you will have to make a custom inspector for that type (which it seems like you're doing already) and just create those other fields yourself.
Answer by Vega4Life · Dec 27, 2018 at 03:08 PM
For fun I made an example on how to make what you want. Two scripts. To see how it works, copy these scripts and put the Example script on a gameObject. Then toggle the boolean.
using UnityEngine;
// Nothing special here, just a script with some exposed inspector variables
public class Example : MonoBehaviour
{
[SerializeField] bool boolVariable;
[SerializeField] float floatVariable;
[SerializeField] int intVariable;
}
Here is the custom editor for the above script that will put a label underneath the bool property if true
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(Example))]
public class CustomExample : Editor
{
SerializedProperty boolProperty;
SerializedProperty floatProperty;
SerializedProperty intProperty;
void OnEnable()
{
boolProperty = serializedObject.FindProperty("boolVariable");
floatProperty = serializedObject.FindProperty("floatVariable");
intProperty = serializedObject.FindProperty("intVariable");
}
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUILayout.Space();
boolProperty.boolValue = EditorGUILayout.Toggle(boolProperty.displayName, boolProperty.boolValue);
if (boolProperty.boolValue)
{
OnBoolPropertyTrue();
}
floatProperty.floatValue = EditorGUILayout.FloatField(floatProperty.displayName, floatProperty.floatValue);
intProperty.intValue = EditorGUILayout.IntField(intProperty.displayName, intProperty.intValue);
serializedObject.ApplyModifiedProperties();
}
void OnBoolPropertyTrue()
{
EditorGUILayout.LabelField("I AM BELOW THE BOOL PROPERTY NOW");
}
}
I hope this helps!
Hey Vega4Life. $$anonymous$$ind telling me something? Why do you use serializedObject.Update(); with the lower case? If I use the upper case like this SerializedObject.Update(); I get errors. (I was trying to use the upper case because the official documentation uses the upper case)
Your answer
Follow this Question
Related Questions
How to Hide/Show List or Array in the inspector based on a variable? 0 Answers
Inspector Overlapping Text Label at a Position 1 Answer
Trying to create Custom Inspector Labels and I get erros 0 Answers
How can i get SerializedProperty from UnityEvent which in List. Sorry for my Eng. 2 Answers
Custom Inspector "Multi-object editing not supported" 4 Answers