Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
8
Question by Benzor · Oct 22, 2014 at 05:40 PM · ui4.6

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

4 Replies

· Add your reply
  • Sort: 
avatar image
7

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;
      }
 }




Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artifact-Jesse · Sep 17, 2016 at 02:42 AM 4
Share

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.

avatar image
6

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Artifact-Jesse · Sep 17, 2016 at 02:45 AM 0
Share

See @oOHicksyOo 's answer for a better suggestion (and my notes in the comments on his answer).

avatar image
1

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 {
   }
 }
 
Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

8 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges