Skip to content

Instantly share code, notes, and snippets.

@baobao
Created November 8, 2016 17:45
Show Gist options
  • Save baobao/f0fc6e61a4b407cc3bb1e3eff055e25a to your computer and use it in GitHub Desktop.
Save baobao/f0fc6e61a4b407cc3bb1e3eff055e25a to your computer and use it in GitHub Desktop.
テキスト入力確認ウィンドウサンプル
using UnityEngine;
using System.Collections;
using UnityEditor;
[CustomEditor(typeof(ConfirmTest))]
public class ConfirmTestEditor : Editor
{
ConfirmTest _target;
void OnEnable ()
{
_target = (ConfirmTest)target;
_isDisable = false;
}
bool _isDisable = false;
string _text = "";
public override void OnInspectorGUI ()
{
// Windowを表示しているときに全体を非アクティブにするDisableGroup
EditorGUI.BeginDisabledGroup (_isDisable);
GUILayout.Label ("専用Windowから入力するテスト");
// 直接入力させないために常に非アクティブにしておくDisableGroup
EditorGUI.BeginDisabledGroup (true);
_text = EditorGUILayout.TextField (_text);
EditorGUI.EndDisabledGroup ();
if (GUILayout.Button ("入力ボタン", GUILayout.Width (100f), GUILayout.Height (30f)))
{
// ウィンドウを開いたら全体を非アクティブへ
_isDisable = true;
OpenWindow(x=>{
// キャンセルの場合はnullなのでnullチェック
if (string.IsNullOrEmpty (x) == false)
{
_text = x ;
}
// 非アクティブ解除
_isDisable = false;
});
}
EditorGUI.EndDisabledGroup ();
}
void OpenWindow (System.Action<string> callback)
{
// 引数をtrueにして常に最前面へ持ってくる
var w = EditorWindow.GetWindow <InputConfirmWindow> (true);
w.Setup (callback);
w.position = new Rect (150f, 150f, 325f, 150f);
}
}
// テキスト入力Window
public class InputConfirmWindow :EditorWindow
{
private System.Action<string> _callback;
public void Setup(System.Action<string> callback)
{
_callback = callback;
}
string _input = "";
bool _isInit = false;
void OnGUI ()
{
GUILayout.Label ("入力してください");
GUILayout.Space (10f);
GUI.SetNextControlName ("ForcusField");
_input = GUILayout.TextField (_input);
GUILayout.Space (10f);
// 何かしら入力しないとOKボタンを押せないようにするDisableGroup
EditorGUI.BeginDisabledGroup (string.IsNullOrEmpty (_input));
GUILayout.BeginHorizontal ();
if (GUILayout.Button ("OK", GUILayout.Height (30f)))
{
_callback (_input);
Close ();
}
EditorGUI.EndDisabledGroup ();
if (GUILayout.Button ("CANCEL", GUILayout.Height (30f)))
{
_callback (null);
Close ();
}
GUILayout.EndHorizontal ();
if (_isInit == false)
{
// テキストフィールドにフォーカスをあてる
EditorGUI.FocusTextInControl ("ForcusField");
}
_isInit = true;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment