1
\$\begingroup\$

So I've seen people do stuff like

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class [ScriptName] : Editor
{
   // A bunch of code that I can't understand.
}


but what exactly do they do to make buttons on their script?

\$\endgroup\$
2
  • \$\begingroup\$ This is usually done by overriding OnInspectorGUI and including a call to GUI.Button, or the newer method with UIElements by overriding CreateInspectorGUI and creating a Button element. If you can explain what specific code you need help with, we can target answers to help with the specific aspect you're confused about. \$\endgroup\$
    – DMGregory
    Sep 24, 2021 at 0:43
  • \$\begingroup\$ However, if you don't understand any of it, then you might be a bit early in your learning journey yet for this particular task, and you may want to consider following some beginner tutorials to get a solid grasp of the language and the conventions for how it's used in Unity before returning to this problem. \$\endgroup\$
    – DMGregory
    Sep 24, 2021 at 0:45

2 Answers 2

1
\$\begingroup\$

Unity provides an ability to override the OnInspectorGUI function which is called when drawing an inspector window for a specific script.

The way it works is you can just create a new class derived from Editor class and tell the unity to create our custom inspector windows for your specific script.

And then in your OnInspectorGUI you can ask unity to draw all sorts of controls including buttons, color dialogs, sliders, etc!

Now to make a simple button with the traditional "Hello World" text, follow this code:

using UnityEngine;
using UnityEditor;

public class MyScript : MonoBehaviour
{
    public bool doesntMatter;
}


[CustomEditor(typeof(MyScript))]
public class MyScriptEditor : Editor
{
    void OnInspectorGUI()
    {
        var myScript = target as MyScript;
        myScript.doesntMatter= EditorGUILayout.Toggle("Hello World"); //Returns true when user clicks
    }
}


Now if you've got a good grasp, here's something that you might actually need when creating custom inspector windows:

When the user toggles a checkmark, more variables wqill be shown to the user.

using UnityEngine;
using UnityEditor;

public class MyScript2 : MonoBehaviour
{
    public bool show = false;
    public int num1;
    public float num2;
    public bool anotherToggle = false;
    public GameObject go;
}


[CustomEditor(typeof(MyScript2))]
public class MyScript2Editor : Editor
{
    void OnInspectorGUI()
    {
        var myScript2 = target as MyScript2;

        myScript2.show = EditorGUILayout.Toggle("Show Hidden Values");
        myScript2.anotherToggle = EditorGUILayout.Toggle("Show Another Hidden Value");

        if(myScript2.show)
            myScript2.num1 = EditorGUILayout.IntField("Enter a number:", myScript2.num1);
            myScript2.num2 = EditorGUILayout.Slider("Choose float between 1 to 10:", myScript2.num2, 0, 10f);
            if(myScript2.anotherToggle)
                myScript2.go = EditorGUILayout.ObjectField("Very hidden gameObject:", go, typeof(GameObject));
    }
}



Hope this helped you create your first inspector windows :)

\$\endgroup\$
2
  • \$\begingroup\$ This is actually very simillar to a Brackeys video I watched about custom inspectors \$\endgroup\$
    – user156620
    Sep 25, 2021 at 14:51
  • \$\begingroup\$ cannot convert string to boolean for you first example, on this line myScript.doesntMatter= EditorGUILayout.Toggle("Hello World"); //Returns true when user clicks \$\endgroup\$
    – Confused
    Jan 9, 2023 at 13:52
1
\$\begingroup\$

A solution I often use is creating "pseudo-buttons" using bools and Unity's OnValidate() method

    void ExampleMethod()
    {
        Debug.Log("Hello World!");
    }

    public bool pseudoButton = false;
    void OnValidate()
    {
        if (pseudoButton)
        {
            ExampleMethod();

            pseudoButton = false;
        }
    }

It's a bit of a hacky, but its useful for quick debugging, and to not waste time with creating a new custom

\$\endgroup\$
0

You must log in to answer this question.