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
0
Question by arth9130 · Jul 26, 2021 at 01:55 AM · editorcustom inspector

Draw a custom button under a variable

I whant to draw this button: alt text

Below "Play on awake" bool, how i can done this? this is my custom editor script:

     public class SFXEditor : Editor
     {
         public override void OnInspectorGUI()
         {
             if (GUILayout.Button("Play Clip"))
             {
                 //Do Something.        
             }
         }
     }

screenshot-1.png (3.2 kB)
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

1 Reply

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

Answer by viesc123 · Jul 26, 2021 at 08:01 AM

Hey there,

Couple of ways to do that:

1) Order of base.OnInspectorGUI

(Easiest, but not flexible)

Since OnInspectorGUI is an override method of the Editor base class, there is a base.OnInspectorGUI() call you can perform that will draw the "regular" inspector. Depending on the order of the call and your button code in your OnInspectorGUI method, the button will be either drawn before or after the "regular" inspector block:

 public override void OnInspectorGUI()
 {
     if (GUILayout.Button("Play Clip"))
     {            
     }
     base.OnInspectorGUI();
 }

button first

 public override void OnInspectorGUI()
 {
     base.OnInspectorGUI();
     if (GUILayout.Button("Play Clip"))
     {            
     }
 }

Button last

Just using the regular GUI + custom button you cannot get more granular than this.

2) Only custom GUI

(More work, more flexible)

If you want the button to go between other UI-Elements, you can just leave the base.OnInspectorGUI() call out and code the whole Editor GUI on your own, using EditorGUILayout methods. This is a bit more work and pretty unflexible when adding new public fields, so I usually wouldn't go that way.

 public override void OnInspectorGUI()
 {
     TestScript myTarget = (target as TestScript);

     myTarget.playOnAwake = EditorGUILayout.Toggle("Play On Awake", myTarget.playOnAwake);
 
     if (GUILayout.Button("Play Clip"))
     {
             
     }
 
     myTarget.someOtherValue = EditorGUILayout.FloatField("Some Other Value", myTarget.someOtherValue);
 }

3) Button Attribute

(My fav)

Probably the way I would go here is to add some code for a [Button] editor attribute to your project. You can find different implementations for this on the internet, but the basic idea is to add your own editor attribute that can turn any of your methods into a button without adding specific GUI code to that class. Here's an example.

 public class TestScript : MonoBehaviour
 {
     public bool playOnAwake = true;
     public float someOtherValue = 10f;
 
     [Button]
     public void PlayClip()
     {
 
     }
 }



button-last.png (21.5 kB)
button-first.png (21.5 kB)
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 arth9130 · Jul 26, 2021 at 12:18 PM 0
Share

Thanks!!!!!!!!!!!, just what I needed.

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

165 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 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 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 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 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 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 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 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 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 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 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Custom brush tool? 0 Answers

OnGUI() never being called in ExecuteInEditMode script 1 Answer

How to create custom inspector (for a scriptable object) 0 Answers

Serializale field not being serialized when using reflection? 2 Answers

Preview Window in Editor 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