Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
SmartButtonEditor.cs for Unity3D
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor (typeof(SmartButton))]
public class SmartButtonEditor : Editor {
/// <summary>
/// The smart button itself. Here we will have stored all of his variables
/// </summary>
private SmartButton smartButton;
public override void OnInspectorGUI ()
// Unity provides a target when overriding the inspector look, and it has always the same type we declare on CustmoEditor(typeof()) on top
smartButton = (SmartButton)target;
//We will use this variable, the selected enum in the smart button, not only to do different things in runtime, but also to show different inputs in the editor
smartButton.UseAs = (SmartButton.Usage)EditorGUILayout.EnumPopup("Use As", smartButton.UseAs);
switch (smartButton.UseAs)
//Default usage just triggers an event in case someone is listening.
case SmartButton.Usage.Default:
//Just show a textfield so the user can enter the url he wants to open
case SmartButton.Usage.URL:
smartButton.Value = EditorGUILayout.TextField("URL",smartButton.Value);
//Same with the scenes
case SmartButton.Usage.SceneLoader:
smartButton.Value = EditorGUILayout.TextField("Scene Name",smartButton.Value);
//So here it gets tricky.
case SmartButton.Usage.PanelSwitcher:
//We tell the layout that the following fields should be in the same level
//Adding a label
EditorGUILayout.LabelField("Our Parent Panel");
//Create a Game Object input. The user can drop game objects here, and we store them in OurPanel variable
smartButton.OurParentPanel = (GameObject)EditorGUILayout.ObjectField(smartButton.OurParentPanel,typeof(GameObject),true);
//Tell the layout that we can keep adding stuff in the next level
//Same with the go to panel
EditorGUILayout.LabelField("Go to Panel");
smartButton.GoToPanel = (GameObject)EditorGUILayout.ObjectField(smartButton.GoToPanel,typeof(GameObject),true);
//Good things about customizing inspector views. We can add some comments
EditorGUILayout.LabelField("If Go to Panel is null, it will only hide Our Parent Panel");
case SmartButton.Usage.IAPPurchase:
//Simple text field for the item id we want to purchase
smartButton.Value = EditorGUILayout.TextField("Item Id",smartButton.Value);
//Show nothing, just want to exit game
case SmartButton.Usage.Exit:
//Here we will get a monobehaviour reference, and a string to now what coroutine to call
case SmartButton.Usage.CoroutineTrigger:
EditorGUILayout.LabelField("Component with Coroutine");
//Just getting the Monobehaviour reference from our object input field
smartButton.ComponentToUse = (MonoBehaviour)EditorGUILayout.ObjectField(smartButton.ComponentToUse,typeof(MonoBehaviour),true);
//And the name of the coroutine
smartButton.Value = EditorGUILayout.TextField("Coroutine Name",smartButton.Value);
//IF something has changed, we let unity know, so it makes us save the scene, and therefore, the smart button values will be saved
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment