Skip to content

Instantly share code, notes, and snippets.

@neon-izm
Created March 15, 2019 01:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save neon-izm/a17b437f80d88baa199dd59722154813 to your computer and use it in GitHub Desktop.
Save neon-izm/a17b437f80d88baa199dd59722154813 to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
/// <summary>
/// ポップアップしてメッセージをユーザに伝える
/// Debug.LogErrorは読んでもらえないがち…それはそう…
/// </summary>
public class PopupContentMessage : PopupWindowContent
{
private string showmessage = "default message";
/// <summary>
/// サイズを取得する
/// </summary>
public override Vector2 GetWindowSize()
{
return new Vector2(300, 150);
}
/// <summary>
/// GUI描画
/// </summary>
public override void OnGUI(Rect rect)
{
GUIStyle centerbold = new GUIStyle()
{
//alignment = TextAnchor.MiddleCenter,
//fontStyle = FontStyle.Bold,
wordWrap = true,
richText = true
};
EditorGUILayout.LabelField(showmessage,centerbold);
//if (GUILayout.Button("OK"))
{
}
}
public void SetMessage(string newContent)
{
showmessage = newContent;
}
/// <summary>
/// 開いたときの処理
/// </summary>
public override void OnOpen()
{
}
/// <summary>
/// 閉じたときの処理
/// </summary>
public override void OnClose()
{
}
}
@neon-izm
Copy link
Author

Usage

HogeBehaviour.cs

private void OnGUI() 
{
      if(someErrorCaused)
      {
           // クリックした位置を視点とするRectを作る
           // 本来のポップアップの用途として使う場合はボタンのRectを渡す
           var mouseRect = new Rect(Event.current.mousePosition, Vector2.one);

           // PopupWindowContentを生成
           var content = new PopupContentMessage();

           content.SetMessage("something\n wrong \n たすけて");
            // 開く
           PopupWindow.Show(mouseRect, content);
      }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment