Skip to content

Instantly share code, notes, and snippets.

@birdinforest
Last active October 9, 2016 23:44
Show Gist options
  • Save birdinforest/f4ee24bad84886da878d6b7ba36fc2ec to your computer and use it in GitHub Desktop.
Save birdinforest/f4ee24bad84886da878d6b7ba36fc2ec to your computer and use it in GitHub Desktop.
Pop up a editor window with int input field. Example: Create a menu item on editor, when user select that option, pop up a window with int field. Unlock level based on user's input.
#if UNITY_EDITOR
using UnityEngine;
using UnityEditor;
class EditorWindowIntField : EditorWindow {
private static int input = 1;
private static string _title = string.Empty;
private static string _btnName = string.Empty;
private static System.Action<int> _onBtnClick;
// To create multiple int fileds.
private static int count = 1;
private static int[] inputs;
private static System.Action<int[]> _onBtnClickMultiInputs;
public static void Create(string title, string btnName, System.Action<int> onBtnClick) {
_title = title;
_onBtnClick = onBtnClick;
_btnName = btnName;
count = 1;
Init(title);
}
// To create multiple int fileds.
public static void CreateMultiple(string title, string btnName, System.Action<int[]> onBtnClick, int inputNum = 1) {
_title = title;
_onBtnClickMultiInputs = onBtnClick;
_btnName = btnName;
inputs = new int[count];
count = inputNum;
// Create window after all setting done.
Init(title);
}
private static void Init(string title) {
var window = (EditorWindowIntField)EditorWindow.GetWindow(typeof(EditorWindowIntField), true, title, true);
window.Show();
}
private void OnGUI() {
if (count == 1) {
input = EditorGUILayout.IntField ("Number:", input);
}
else {
for (int i = 0; i < count; i++) {
inputs[i] = EditorGUILayout.IntField ("Number:", inputs[i]);
}
}
if(GUILayout.Button(_btnName))
{
if(_onBtnClick != null && count ==1)
_onBtnClick (input);
else if(_onBtnClickMultiInputs != null)
_onBtnClickMultiInputs (inputs);
this.Close();
}
}
}
#endif
public static class GameDebugger
{
[MenuItem ("Tools/Game Debug/Unlock Level by number", false, 1)]
public static void UnlockLevelByNumber ()
{
EditorWindowIntField.CreateNew("Unlock level", "Unlock", input => {
GameManager.maxLevel = input;
});
}
[MenuItem ("Tools/Game Debug/Mission/TestMissions", false, 1)]
public static void SetTestMissions ()
{
EditorWindowIntField.CreateMultiple("Unlock level", "Unlock", (inputs) => {
MissionManager.SetTestMission (inputs);
}, 3);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment