Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
2 captures
12 Jun 22 - 14 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
7
Question by Muzz5 · Jun 04, 2011 at 09:34 AM · editor

Create a Button in the inspector

I'm learning editor scripting, and I can create custom windows, and wizards. But how do you create a button in the inspector? So you click on an object, and under the script title, is a button.

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

6 Replies

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

Answer by Peter G · Jun 04, 2011 at 11:07 AM

Its very easy:

 @CustomEditor(YourScript)
 // ^ This is the script we are making a custom editor for.
 public class YourScriptEditor extends Editor {
    
     override function OnInspectorGUI () {
     //Called whenever the inspector is drawn for this object.
         DrawDefaultInspector();
         //This draws the default screen.  You don't need this if you want
         //to start from scratch, but I use this when I'm just adding a button or
         //some small addition and don't feel like recreating the whole inspector.

         if(GUILayout.Button("Your ButtonText")) [
             //add everthing the button would do.
         }
    }
 }

The key part is to create a custom inspector, and override OnInspectorGUI. From there you have much more freedom in what you want to do. DrawDefaultInspector() will draw the inspector exactly like Unity would so it is useful if you are adding functionality to the end or a small addition because you can draw the default inspector then attach more functionality to the end like the above script does.

Of course, if you wanted, you could create a custom inspector from scratch, recreating every inspector field and such, but if you just need a button, that would probably be more work than you want to do.

Comment
Add comment · Show 3 · 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 Muzz5 · Jun 04, 2011 at 11:29 AM 0
Share

Where is the button displayed? I'd got this far with the docs, but didn't know where to find it. Thanks for the explanation, though. (I'm assu$$anonymous$$g this goes in a folder called 'Editor' and is not attatched to anything, right?) Could you describe the basic script set-up a bit more?

avatar image Peter G · Jun 04, 2011 at 11:33 AM 0
Share

Correct it goes in the editor folder, and it should be named "YourScriptEditor." When you are inspecting an object with YourScript attached, you should see as part of that scripts inspector. It will be at the very bottom of the inspector for the script though since we are drawing it after we draw the rest of the inspector.

avatar image Muzz5 · Jun 04, 2011 at 11:45 AM 0
Share

Forgot to rename it! Thanks!

avatar image
13

Answer by Zbyl · Dec 08, 2012 at 05:22 PM

Here's a C# version:

 [CustomEditor(typeof(DecalMeshHelper))]
 class DecalMeshHelperEditor : Editor {
   public override void OnInspectorGUI() {
     if(GUILayout.Button("Test"))
       Debug.Log("It's alive: " + target.name);
   }
 }

It's very important to use `public override`, otherwise it won't work!

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 larku · Jun 28, 2014 at 03:25 AM 1
Share

You also need to add using UnityEditor; for Editor to be visible. And the script must be in Assets/Editor/ to function.

avatar image Ekkara · Oct 14, 2020 at 02:35 PM 0
Share

I would add everything mentioned by Iarku and also add "base.OnInspectorGUI();" in the void so that you can use public variables in the inspector

avatar image
11

Answer by oStaiko · Nov 21, 2016 at 09:34 PM

I know this is extremely old, but for anyone looking here years later, there's an easier makeshift way to add buttons!

 [ExecuteInEditMode]
 public class myClass : Monobehaviour
 {
 
     public bool buttonDisplayName; //"run" or "generate" for example
     public bool buttonDisplayName2; //supports multiple buttons
 
     Update()
     {
         if (buttonDisplayName)
             ButtonFunction1 ();
         else if (buttonDisplayName2)
             ButtonFunction2 ();
         buttonDisplayName = false;
         buttonDisplayName2 = false;
     }
 
     void ButtonFunction1 ()
     {
         //DoStuff
     }
 
     void ButtonFunction2 ()
     {
         //DoStuff
     }
 }

In the editor, update is called whenever things are changed. When you press one of our "buttons", the Boolean value changes to true, and triggers Update() immediately. This runs the button function a single time, and at the end of update, turns the Boolean back to false. This allows you to not have to make a separate editor script, and gives the exact functionality, but just a tinier button.

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 ShawnFeatherly · Jun 29, 2017 at 11:51 PM 1
Share

OnValidate() may be a better spot than Update(). Great pattern tho! You can trigger more than buttons with this pattern. You can load values into the component. For example, I have a ScriptableObject field. If it's not null, I load it's values into the component, then set it to null.

avatar image carrotstien · Jul 23, 2017 at 02:41 PM 0
Share

yea this is one of the easiest hacks to do. To make your code look a bit clear thought, I'd suggest something like

 public bool
         createBox;
 
 void Update () {
         doCreateBox(ref createBox);
 }
 
 void doCreateBox(ref bool button){
      if(!button) return;
      button = false;
      //do stuff
 }
 

this way, your update looks cleaner

avatar image
2

Answer by smallsketch · May 25, 2015 at 03:48 PM

Also, in C#, be sure to use the UnityEditor namespace so that we can actually use the "Editor" class. Otherwise, you'll probably get errors.

So at the top, be sure to put the "using UnityEditor;"

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 mastarxtnz · Nov 14, 2017 at 12:12 PM

But... I am missing something here! How can you build the final project, if in order to create the build you have to save the files (using UnityEditor) in the Editor folder, and then... you cant add components from the Editor folder?

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 Bunny83 · Nov 14, 2017 at 01:40 PM 0
Share

This is not an answer. But a question. If you want to ask a question, Ask a Question. If you want to refer to a certain answer on this question, just add a link to the answer.

  • 1
  • 2
  • ›

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

13 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

Related Questions

The name 'Joystick' does not denote a valid type ('not found') 2 Answers

The parallel function to Selection.activeObject in the Inspector? 0 Answers

How do I repaint/refresh/focus without calling Key Events multiple times? 1 Answer

Object2Terrain error/wont work. 1 Answer

Is there a function like Start for starting up unity? 1 Answer


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