- Home /
Custom inspector. if bool is true then show variable.
I need editor script to drag GameObject and InputField in inspector if and only if bool is true. If bool is false then dont show InputField and GameObject field in inspector. If true show that field in inspector.
public bool StartTemp;
if(StartTemp)
{
public InputField iField; // if StartTemp is true then only show in inspector.
public GameObject Template; // if StartTemp is true then only show in inspector.
}
If StartTemp is true then show InputField and GameObject field in inspector.
GUILayout.BeginHorizontal();
GUILayout.Label("Team", GUILayout.Width(122));
_gmCtrl.teamMatch = EditorGUILayout.Toggle(_gmCtrl.teamMatch);
GUILayout.EndHorizontal();
if (_gmCtrl.teamMatch)
{
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Label("Friend", GUILayout.Width(122));
_gmCtrl.noOfFriend = EditorGUILayout.IntField(_gmCtrl.noOfFriend);
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Label("Enemy", GUILayout.Width(122));
_gmCtrl.noOfEnemy = EditorGUILayout.IntField(_gmCtrl.noOfEnemy);
GUILayout.EndHorizontal();
}
Just like this.
Bool false Bool true
What is the question? Your code syntax is wrong, can you qualify your question for us please.
Yes I know that it cant be possible.
public bool StartTemp;
if(StartTemp)
{
public InputField iField;
public GameObject Template;
}
But I need editor script to drag GameObject and InputField in inspector if and only if bool is true. If bool is false then dont show that two field in inspector. @Landern
Answer by ElijahShadbolt · Dec 14, 2016 at 08:50 PM
RandomScript.cs
using UnityEngine;
using UnityEngine.UI;
#if UNITY_EDITOR
using UnityEditor;
#endif
public class RandomScript : MonoBehaviour
{
[HideInInspector] // HideInInspector makes sure the default inspector won't show these fields.
public bool StartTemp;
[HideInInspector]
public InputField iField;
[HideInInspector]
public GameObject Template;
// ... Update(), Awake(), etc
}
#if UNITY_EDITOR
[CustomEditor(typeof(RandomScript))]
public class RandomScript_Editor : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector(); // for other non-HideInInspector fields
RandomScript script = (RandomScript)target;
// draw checkbox for the bool
script.StartTemp = EditorGUILayout.Toggle("Start Temp", script.StartTemp);
if (script.StartTemp) // if bool is true, show other fields
{
script.iField = EditorGUILayout.ObjectField("I Field", script.iField, typeof(InputField), true) as InputField;
script.Template = EditorGUILayout.ObjectField("Template", script.Template, typeof(GameObject), true) as GameObject;
}
}
}
#endif
Cresspresso - Why hide the initiating bool (StartTemp) in the main Class (lines 10 & 11) and then force the show in the Editor Script (lines 32 & 33)?
It works with the initiating bool shown in the default CS Class (pulling the "[HideInInspector]" from line 10) and commenting out line 33.
Am I missing something?
(And I realize I'm 5-years late to this party AND YOUR answer is still perfect! (in Unity 2019.2.7f2)
I'm just trying to understand if it is a preference (explicitly hiding & showing stuff) or a Unity requirement that became moot along the way...
It is nothing special, one could rewrite it as
public bool StartTemp; /* without [HideInInspector] */
...
DrawDefaultInspector();
RandomScript script = (RandomScript)target;
/* without EditorGUILayout.Toggle */
if (script.StartTemp)
{
...
}
However, to make it easier to group the toggle field with the hidden fields, I chose to draw it after all the other non-HideInInspector fields (not shown in the example), in case any fields are added when the script is modified by someone else.
Thoses variables that are displayed reset to null when launching the game :/ Do you know why and if I can change this ?
I hop you still use this account lol :p
EditorUtility.SetDirty or Undo.RecordObject probably. Use one of these methods to tell the Editor's SerializedObject that you changed some of its properties. Avoid doing it every frame of OnInspectorGUI, instead only do it when the object returned from EditorGUILayout.ObjectField is not equal to the old value.
Answer by NoseKills · Dec 14, 2016 at 04:40 PM
Just put the drawing of those fields inside the "if"
_gmCtrl.teamMatch = EditorGUILayout.Toggle(_gmCtrl.teamMatch);
if (_gmCtrl.teamMatch) {
var gameObject = EditorGUILayout.ObjectField(source, typeof(GameObject), true);
)
Thanks @Nose$$anonymous$$ills . I will try this. Do you know about InputField?