I’m using the new Unity 4.6 beta to do some UI work. I want to inherit from Button to run some custom code when the button is pressed and released (e.g. moving another sprite somewhere else on screen).
I’d like to expose a float in the inspector so I can adjust it per instance of this button, but it doesn’t seem to appear. Here is my code:
public class CustomButton : Button
{
public float OffsetY = 10.0f; // TODO: Why doesn't the inspector expose this?
private Vector3 offset;
protected override void Awake()
{
base.Awake();
offset = new Vector3(0.0f, OffsetY, 0.0f);
}
}
The inspector definitely works normally for all other scripts. If I make this class inherit from MonoBehaviour it works, I can clearly see and adjust the OffsetY variable. So what is it about the Button class that seems to prevent custom public variables from being shown in the inspector?
What prevents public variables from showing as normal is, I believe, the fact that Unity developers created custom editor scripts to display uGUI components such as Button, Image, etc. Those are why you have in the inspector a pretty dropdown menu to choose the button’s transition, indented options, buttons, etc. These scripts only show properties that they are explicitly told to show, which doesn’t include your OffsetY variable or really anything that does not belong to the original Button.
As a not-at-all-pretty workaround, you can press the button near the lock in the top right corner of the inspector. It will open a dropdown menu where you can change the inspector from “Normal” to “Debug”. This way it will ignore editor scripts and just show you everything. Everything includes private variables, which you can’t change, and stuff that we’re generally better off not knowing You can change OffsetY in this mode, if you really want to.
Unfortunately, I have no better suggestions and would also like to know how to do this cleanly, if that is at all possible.
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System;
[Serializable]
public class UIButton : Button
{
public enum TestEnum {ON,OFF}
public int testInt;
public TestEnum testEnumVar;
}
Class used to override the inspectorGUI
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(UIButton))]
public class UIButtonEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
UIButton t = (UIButton)target;
}
}
I’m not sure about buttons but some UI classes don’t really need custom editors. For example, the Layout Group components. The custom editor just gets in the way of publics in descendant classes. Here is what I did when I extended HorizontalOrVerticalLayoutGroup to allow my new publics to show in the IDE. Place this class in an ‘Editor’ folder.
namespace UnityEditor.UI {
/// <summary>
/// Override the Unity UI custom editor for HorizontalOrVerticalLayoutGroup
/// and do exactly nothing. The built-in editor prevents editing publics in
/// descendant classes.
/// </summary>
[CustomEditor(typeof(SDD.UI.DynamicLayoutGroup), true)]
[CanEditMultipleObjects]
public class DynamicLayoutGroupEditor : Editor {
}
}
Inherit from that script and expose the variables you want.
EDIT:
Looks like unity isn’t including the DLLs for it’s editor scripts. There’s a workaround in this thread. It’s quite old so the issue may have been resolved in newer versions of unity.