Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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
6
Question by Tespy · Jan 25, 2017 at 08:21 PM · uibuttonunity5inspectorsubclassing

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.

Comment
Add comment · Show 1
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 juicyz · Jan 25, 2017 at 10:38 PM 0
Share

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

5 Replies

· Add your reply
  • Sort: 
avatar image
10
Best Answer

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

Comment
Add comment · Show 5 · 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 Adam-Mechtley · Jan 26, 2017 at 07:57 AM 4
Share

The only revision I would suggest here is to inherit UnityEditor.UI.ButtonEditor ins$$anonymous$$d of simply Editor:)

avatar image Tespy · Jan 27, 2017 at 05:36 PM 0
Share

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?

avatar image Adam-Mechtley Tespy · Jan 27, 2017 at 07:14 PM 4
Share

If you inherit UnityEditor.UI.ButtonEditor, do not call DrawDefaultInspector() but ins$$anonymous$$d call base.OnInspectorGUI()

avatar image Tespy Adam-Mechtley · Jan 31, 2017 at 06:46 PM 0
Share

That fixed it. Thanks :>

avatar image MrLucid72 · Mar 22, 2019 at 07:20 AM 0
Share

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?

avatar image
1

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 ?

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
1

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.

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 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();
Comment
Add comment · Show 2 · 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 TrevorReed · Jan 31, 2020 at 06:47 PM 0
Share

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.

avatar image TrevorReed TrevorReed · Jan 31, 2020 at 07:21 PM 1
Share

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();
 }
avatar image
0

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

14 People are following this question.

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

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


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