Skip to content

Instantly share code, notes, and snippets.

Embed
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:
break;
//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);
break;
//Same with the scenes
case SmartButton.Usage.SceneLoader:
smartButton.Value = EditorGUILayout.TextField("Scene Name",smartButton.Value);
break;
//So here it gets tricky.
case SmartButton.Usage.PanelSwitcher:
//We tell the layout that the following fields should be in the same level
EditorGUILayout.BeginHorizontal();
//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
EditorGUILayout.EndHorizontal();
//Same with the go to panel
EditorGUILayout.BeginHorizontal();
EditorGUILayout.LabelField("Go to Panel");
smartButton.GoToPanel = (GameObject)EditorGUILayout.ObjectField(smartButton.GoToPanel,typeof(GameObject),true);
EditorGUILayout.EndHorizontal();
//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");
break;
case SmartButton.Usage.IAPPurchase:
//Simple text field for the item id we want to purchase
smartButton.Value = EditorGUILayout.TextField("Item Id",smartButton.Value);
break;
//Show nothing, just want to exit game
case SmartButton.Usage.Exit:
break;
//Here we will get a monobehaviour reference, and a string to now what coroutine to call
case SmartButton.Usage.CoroutineTrigger:
EditorGUILayout.BeginHorizontal();
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);
EditorGUILayout.EndHorizontal();
//And the name of the coroutine
smartButton.Value = EditorGUILayout.TextField("Coroutine Name",smartButton.Value);
break;
}
//IF something has changed, we let unity know, so it makes us save the scene, and therefore, the smart button values will be saved
if(GUI.changed)
EditorUtility.SetDirty(target);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.