- Home /
Custom Inspector, How to create textfield on button click?
Im trying to make is so when I click a button in my inspector, a text field appears. It seems like a really simple thing to do. But for some reason the text field doenst appear.
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Create key", GUILayout.Width(100)))
{
some = EditorGUILayout.TextField(some);
}
}
This is what I am trying to get to work. if I put the code above the button code it works. Im new with working with custom inspectors, so any help is appriciated.
Answer by Exigo95 · May 31, 2019 at 11:30 AM
The OnIspectorGUI callback is essentially called every frame, thus you have to make sure everything you want to see all the time is also called everytime. Additionally, the Button does not automatically serve as some kind of toggle. Currently, you press the button and the TextField line is called once. You have to save the state of having the button clicked once.
private bool _PressedButton;
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Create key", GUILayout.Width(100)))
{
_PressedButton != _PressedButton;
}
if(_PressedButton)
{
some = EditorGUILayout.TextField(some);
}
}
This should work, although untested. In this example you change the state of the private member _PressedButton every time you click the button. Once the variable is true, the code execution calls the TextField line.
ahh thanks. I knew I was missing something obvious. Thanks dude
You're welcome. You could accept that answer, so somebody else directly knows if the answer is good without reading the comments ;)
Your answer
Follow this Question
Related Questions
Using a CustomEditor in the inspector and also seeing normal inspector fields 1 Answer
Is there a way to draw on the scene view from an asset being inspected by the Inspector? 0 Answers
How do you force a custom inspector to redraw? 7 Answers
Callback when custom PropertyAttribute changes 0 Answers
OnInspectorGUI Custom Drawing? 1 Answer