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.
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();
}
}
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.
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 ?
I was looking for a solution that didn’t involve manually adding property fields for each new subclass field. I found that setting isFallback = true on the CustomEditor attribute works.
[CustomEditor(typeof(MenuButton), isFallback = true)]
public class MenuButton : Button {...}
You should now see your subclass fields.
Note that if you use
[CanEditMultipleObjects]
then during multi-selection, the subclass fields still won’t appear. I couldn’t find a fix for that except adding property fields manually.
Seems like this is because the logic in the SelectableEditor class is broken.
The method:
private void ChildClassPropertiesGUI()
{
if (IsDerivedSelectableEditor())
return;
DrawPropertiesExcluding(serializedObject, m_PropertyPathToExcludeForChildClasses);
}
This prevents the logic from running in subclassed editors like Button or anything that subclasses Button. For some reason Button just adds the OnClick property field manually.