- Home /
drawdefaultinspector except of one variable??
Hello,
So say I have a script that makes a custom inspector for anotherone. The target script has many variables and depending on the enum value I select I want to hide 2 of them. In other words when my enum is set to A I want all the variables of my script to appear in the inspector, but when it is set to B I want to all the variables to appear except of 2.
Do I have to write one line of code for each variable at my custom inspector script or is there a way to say drawDefaultInspector except of those 2 variables?
Answer by FortisVenaliter · May 19, 2016 at 07:04 PM
As far as I know, yeah, you have to rewrite the whole inspector if you want to change the functionality of even one variable. Luckily, all of the predefined fields already have functions to display the same way they do normally.
Answer by Nition · Oct 18, 2019 at 12:06 AM
Instead of using DrawDefaultInspector(), use:
DrawPropertiesExcluding(serializedObject, "myProperty", "myOtherProperty);
Right and you don't have to rewrite the whole inspector even when you want to insert some GUI in between. You can simply iterate over the SerializedProperties:
// untested.
serializedObject.Update();
var p = serializedObject.GetIterator();
do
{
if (p.name != "NameYouWantToOmit")
EditorGUILayout.PropertyField(p);
if(p.name == "someProperty")
{
// Add extra GUI after "someProperty"
}
}
while (p.NextVisible(true));
serializedObject.Apply$$anonymous$$odifiedProperties();
Nice trick! I'd never thought of doing it that way.
Some guy explains it really well here with an example: https://forum.unity.com/threads/hiding-inherited-public-variable-in-the-inspector.161828/#post-4689677
I just needed to copy the code relevant for me.
using UnityEditor;
[CustomEditor(typeof(Laser))]
public class LaserEditor : Editor
{
public override void OnInspectorGUI()
{
serializedObject.Update();
EditorGUI.BeginChangeCheck();
DrawPropertiesExcluding(serializedObject, "firingCycleInterval");
if (EditorGUI.EndChangeCheck())
serializedObject.Apply$$anonymous$$odifiedProperties();
}
}
This is an excellent way to draw an editor but exclude (and then replace) a few custom attributes. This answer needs to get out there more!!
Your answer
Follow this Question
Related Questions
What is the efficient way of showing variables depend on enum in custom inspector? 2 Answers
EditorGUILayout.EnumPopup giving me errors 1 Answer
Button in EditorGUI / ScriptableWizard? 1 Answer
Applying WordWrap to an existing Editor Text Field 0 Answers
Weird action in Unity editor when pressing the "E" key? 1 Answer