冷奴の開発メモ

UnityとかC#とかプログラミングのことをメモしていく場所

エディタ用テキストダイアログ

ソース

using UnityEngine;
using UnityEditor;

namespace Hiyayakko
{
	public sealed class EditorTextDialog : EditorWindow
	{
		private string label = "";
		private string value = "";
		private System.Action<string> onOk = null;
		private System.Action onCancel = null;

		public static void Show(string title, string label, string defaultValue, System.Action<string> onOk, System.Action onCancel)
		{
			var window = EditorWindow.GetWindow<EditorTextDialog>(title);

			window.label = label;
			window.value = defaultValue;
			window.onOk = onOk;
			window.onCancel = onCancel;

			window.Show();
		}

		private void OnGUI()
		{
			maxSize = minSize = new Vector2(400f, 60f);

			EditorGUILayout.LabelField(label);
			value = EditorGUILayout.TextField(value);
			if (GUILayout.Button("OK"))
			{
				if (onOk != null)
				{
					onOk(value);
					onCancel = null;
					Close();
				}
			}
		}

		private void OnDestroy()
		{
			if (onCancel != null)
			{
				onCancel();
			}
		}
	}
}

使用例

EditorTextDialog.Show("タイトル", "説明文", "入力文字列", v => {
	Debug.LogError("Ok 値=" + v);
}, () => {
	Debug.LogError("Cancel");
});

f:id:hiyayakkoapp:20161130005614p:plain