Last active
September 11, 2019 02:47
-
-
Save kankikuchi/c27c0316ae84496a47f5b04abc4468a9 to your computer and use it in GitHub Desktop.
「はい」と「いいえ」など、2つの選択肢を出して確認するためのウィンドウ【Unity】【エディタ拡張】
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ConfirmationEditorWindow.cs | |
// http://kan-kikuchi.hatenablog.com/entry/ConfirmationEditorWindow | |
// | |
// Created by kan.kikuchi on 2019.06.09. | |
using System; | |
using UnityEngine; | |
using UnityEditor; | |
/// <summary> | |
/// 「はい」と「いいえ」など、2つの選択肢を出して確認するためのウィンドウ | |
/// </summary> | |
public class ConfirmationEditorWindow : EditorWindow { | |
//表示中のウィンドウ | |
private static ConfirmationEditorWindow _window; | |
//各設定値 | |
private string _message, _trueSelectionText, _falseSelectionText; | |
private MessageType _messageType; | |
private Action<bool> _callback; | |
//================================================================================= | |
//表示 | |
//================================================================================= | |
/// <summary> | |
/// 表示 | |
/// </summary> | |
public static void Open(string title = "確認", string message = "", MessageType messageType = MessageType.None, string trueSelectionText = "はい", string falseSelectionText = "いいえ", Action<bool> callback = null) { | |
if (_window == null) { | |
_window = CreateInstance<ConfirmationEditorWindow>(); | |
} | |
_window.SetValue(title, message, messageType, trueSelectionText, falseSelectionText, callback); | |
_window.ShowUtility (); | |
} | |
//各設定値を設定 | |
private void SetValue(string title, string message, MessageType messageType, string trueSelectionText, string falseSelectionText, Action<bool> callback) { | |
titleContent.text = title; | |
_message = message; | |
_messageType = messageType; | |
_trueSelectionText = trueSelectionText; | |
_falseSelectionText = falseSelectionText; | |
_callback = callback; | |
} | |
//================================================================================= | |
//非表示、破棄 | |
//================================================================================= | |
private void OnDestroy() { | |
//ウィンドウが消えた時にコールバックが残っていれば(左上のバツボタンで消したら)、falseで実行 | |
if (_callback != null) { | |
_callback(false); | |
_callback = null; | |
} | |
} | |
//コールバックを実行してウィンドウを閉じる | |
private void Close(bool isSelectedTrue) { | |
if (_callback != null) { | |
_callback(isSelectedTrue); | |
_callback = null; | |
} | |
Close(); | |
} | |
//================================================================================= | |
//更新 | |
//================================================================================= | |
private void OnGUI (){ | |
//メッセージ表示 | |
if (!string.IsNullOrEmpty(_message)) { | |
EditorGUILayout.HelpBox(_message, _messageType); | |
GUILayout.Space(10); | |
} | |
//選択肢のボタン表示 | |
EditorGUILayout.BeginHorizontal(GUI.skin.box); | |
if (!string.IsNullOrEmpty(_trueSelectionText) && GUILayout.Button(_trueSelectionText)) { | |
Close(true); | |
} | |
if (!string.IsNullOrEmpty(_trueSelectionText) && !string.IsNullOrEmpty(_falseSelectionText)) { | |
GUILayout.Space(20); | |
} | |
if (!string.IsNullOrEmpty(_falseSelectionText) && GUILayout.Button(_falseSelectionText)) { | |
Close(false); | |
} | |
EditorGUILayout.EndHorizontal(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment