- Home /
Why aren't public variables shown in the Inspector when inheriting from Unity 4.6 UI's Button?
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?
Answer by oOHicksyOo · Feb 22, 2015 at 11:10 PM
Class used on game object
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;
}
}
With a couple of edits, this is the best answer.
You don't actually even need the line UIButton t = (UIButton)target;
Nor do you need using UnityEngine;
nor using System.Collections
. Just implementing a custom inspector with base.OnInspectorGUI() is enough to get your public variables to show up.
You also might want to mention that as a CustomEditor the second script belongs in Assets/Editor/ or it won't work.
Answer by CanisLupus · Nov 22, 2014 at 02:59 PM
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.
See @oOHicksyOo 's answer for a better suggestion (and my notes in the comments on his answer).
Answer by robertwahler · Feb 01, 2015 at 09:42 PM
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 {
}
}
Answer by Geebles · Sep 10, 2015 at 08:54 AM
The full ui source is available: ButtonEditor.cs
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.
Your answer
Follow this Question
Related Questions
[Unity 4.6] Button Tracking UI System? 0 Answers
(c#) Photon networking, get players in room? 1 Answer
4.6 UI Text rect does not expand automatically 2 Answers
Unity 4.6 button movement problem 1 Answer
[4.6 - UI] Keep aspect ratio 2 Answers