- Home /
Subclassing Button, public variable won't show up in the inspector
I've subclassed the Button class, and the new functionality seems to work fine. Though, for some reason, the one public variable I added won't show up in the inspector. I've seen similar questions asked on this website, but none of the answers worked out for me. There are no compile errors and the variable is a public non-static class variable, so I really don't understand.
Here is the script:
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.Collections;
public class MenuButton : Button {
protected bool isHighlighted = false;
protected bool isPressed = false;
public bool acceptsPointerInput = true; //wont show up in the inspector
//bunch of other functions omitted
}
Help would be much-appreciated. If it helps any, I'm using Unity 5.0.0f4.
Try Ctrl-R, restarting unity, reapplying the script, and/or making another variable 'public bool testing' Honestly I dont see a problem with what you have
Answer by Koen-Matthijs · Jan 25, 2017 at 10:40 PM
Hi
Yes you can. You need to create a custom inspector for the given class, and override OnInspectorGUI(). There you can add your custom GUI code and finish off with DrawDefaultInspector().
You will find an example for your MenuButton class below. No need to attach the script below to anything as Unity will auto-resolve the editor-script when you attach the main MenuButton script to your GUI object.
using UnityEditor;
[CustomEditor(typeof(MenuButton))]
public class MenuButtonEditor : Editor
{
public override void OnInspectorGUI()
{
MenuButton targetMenuButton = (MenuButton)target;
targetMenuButton.acceptsPointerInput = EditorGUILayout.Toggle("Accepts pointer input", targetMenuButton.acceptsPointerInput);
// Show default inspector property editor
DrawDefaultInspector();
}
}
The only revision I would suggest here is to inherit UnityEditor.UI.ButtonEditor
ins$$anonymous$$d of simply Editor
:)
I've tried your script, and it sort of works. While the variable now shows up in the editor, a bunch of other fields that don't belong also appear. When I add the $$anonymous$$enuButton script, it starts off with the Transition set to Color Tint. Despite that, it also shows the fields for Highlighted Sprite, Pressed Sprite, etc; those shouldn't show up unless I set the transition to Sprite Swap. The Trigger fields also show up, which also shouldn't be there.
The same issue occurs even after making the custom editor inherit from UnityEditor.UI.ButtonEditor ins$$anonymous$$d of Editor.
What should I do?
If you inherit UnityEditor.UI.ButtonEditor
, do not call DrawDefaultInspector()
but ins$$anonymous$$d call base.OnInspectorGUI()
EDIT: Hmm, wait, this doesn't work. It shows in the inspector, but the value isn't saved when you start
OLD:
This works -- but two follow up questions:
1) Any way to serialize this as private?
2) Any way to add tooltips or something to this on hover?
3) I noticed that multi-select doesn't show the checkbox -- any way to support this?
Answer by CirseiHyuga · Jan 25, 2017 at 10:18 PM
Maybe there is a native editor script in unity, that handle a custom editor for the Button. In that case, this editor script would consider your new button as a simple Button, and not as your MenuButton, and so, would ignore the new parameters to display ?
If it is the case, maybe you should find it and make a editor class that inherits from it ? Or try to make your own custom inspector, without inheritance ?
Answer by wanorde23 · Sep 21, 2021 at 01:40 PM
Can be done easy without any editor scripts. Just go to the top right in the inspector window and press debug. All the variables of your custom script will be show you can just change them there.,This can be done waaaaaayyy more easy just go to the top right of the inspector and switch from "normal" to "debug" all variables will be shown. Just add all the stuff you need and switch back to normal.
Answer by talyh · Jun 14, 2019 at 03:32 PM
For people struggling with this not saving the custom value, the solution seems to be to manually set the target as dirty.
// your custom code
EditorUtility.SetDirty(target);
// Show default inspector property editor
base.OnInspectorGUI();
So this works, but whenever you have that custom inspector window open, your project will indicate it has unsaved changes, even after you try saving multiple times. The same happens if you're editing a prefab and have that custom inspector open. Even worse, if you have Auto Save turned on for your prefab, it'll hitch constantly until you inspect something else or turn Auto Save off.
This is a pretty $$anonymous$$or thing but it irks me enough that I'll try and find another solution.
Got it. Here's a more appropriate way to save the state of the toggle box (and any other custom GUI elements you'd like to add):
public override void OnInspectorGUI()
{
/*
Your custom code here
*/
// Draw the original GUI element's inspector
base.OnInspectorGUI();
// Apply modified properties to mark the component as dirty.
// Reference: https://docs.unity3d.com/ScriptReference/EditorUtility.SetDirty.html
serializedObject.Apply$$anonymous$$odifiedProperties();
}
Answer by Elledan3101 · Mar 29, 2021 at 07:58 AM
Another way, by serializedObject:
[CustomEditor(typeof(GUI_ActionButton))]
public class GUI_ActionButtonEditor : ButtonEditor
{
private SerializedProperty iconProperty;
private SerializedProperty graphicsProperty;
protected override void OnEnable()
{
base.OnEnable();
// or any other private field
iconProperty = serializedObject.FindProperty("icon");
graphicsProperty = serializedObject.FindProperty("targetGraphics");
}
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
// for object
EditorGUILayout.ObjectField(iconProperty);
// for array
EditorGUILayout.PropertyField(graphicsProperty);
serializedObject.ApplyModifiedProperties();
}
}
Your answer
Follow this Question
Related Questions
How to make a UI Canvas not appear again after the first time the user hits the "I Agree" button. 2 Answers
Unity 5: UI button OnPointerDown not function as expected 1 Answer
I can't click to an UI button. 3 Answers
Change scene and play it by clicking UI Button? Scene does just load but not start playmode? 1 Answer
UI Element Got & Lost Focus Handling 2 Answers