- Home /
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.
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.
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?
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.
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!
You also need to add using UnityEditor;
for Editor to be visible. And the script must be in Assets/Editor/
to function.
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
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.
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.
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
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;"
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?
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.
Your answer
Follow this Question
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