Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using NwgLibrary;
- using System.Collections.Generic;
- using System.Text;
- using UnityEngine;
- namespace Common
- {
- /// <summary>
- /// デバッグ出力用のラベルを管理するクラス
- /// </summary>
- [RequireComponent( typeof( UILabel ) )]
- public sealed class UIDebugLabel : MonoBehaviour
- {
- /// <summary>
- /// ログのレベル
- /// </summary>
- public enum Level
- {
- /// <summary>
- /// 情報
- /// </summary>
- INFO,
- /// <summary>
- /// 警告
- /// </summary>
- WARNING,
- /// <summary>
- /// エラー
- /// </summary>
- ERROR,
- /// <summary>
- /// 例外
- /// </summary>
- ASSERT,
- }
- [SerializeField] private int MaxLog = 24; // ログの最大数
- [SerializeField] private Color InfoColor = Color.white; // 情報ログの色
- [SerializeField] private Color WarningColor = Color.yellow; // 警告ログの色
- [SerializeField] private Color ErrorColor = Color.red; // エラーログの色
- [SerializeField] private Color AssertColor = Color.magenta; // 例外ログの色
- [SerializeField] private bool IsConsole = true; // Console ウィンドウに出力するかどうか
- private UILabel mUILabel; // UILabel のインスタンス
- private Queue<string> mLogQueue; // ログのキュー
- private Dictionary<Level, Color> mColorTable; // 各レベルごとの色のテーブル
- /// <summary>
- /// 初期化する時に呼び出されます
- /// </summary>
- private void Awake()
- {
- mUILabel = GetComponent<UILabel>();
- mUILabel.text = string.Empty;
- mLogQueue = new Queue<string>( MaxLog );
- mColorTable = new Dictionary<Level, Color>
- {
- { Level.INFO, InfoColor },
- { Level.WARNING, WarningColor },
- { Level.ERROR, ErrorColor },
- { Level.ASSERT, AssertColor },
- };
- }
- /// <summary>
- /// 更新する時に呼び出されます
- /// </summary>
- private void Update()
- {
- if ( mUILabel == null )
- {
- return;
- }
- var builder = new StringBuilder();
- int index = 0;
- foreach ( var n in mLogQueue )
- {
- if ( index < mLogQueue.Count - 1 )
- {
- builder.AppendLine( n );
- }
- else
- {
- builder.Append( n );
- }
- index++;
- }
- mUILabel.text = builder.ToString();
- }
- /// <summary>
- /// 情報ログを出力します
- /// </summary>
- public void Log( object message )
- {
- Push( Level.INFO, message, IsConsole );
- }
- /// <summary>
- /// 情報ログを出力します
- /// </summary>
- public void Log( object message, bool isConsole )
- {
- Push( Level.INFO, message, isConsole );
- }
- /// <summary>
- /// 情報ログを出力します
- /// </summary>
- public void Log( bool condition, object message )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.INFO, message );
- }
- /// <summary>
- /// 情報ログを出力します
- /// </summary>
- public void Log( bool condition, object message, bool isConsole )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.INFO, message, isConsole );
- }
- /// <summary>
- /// 警告ログを出力します
- /// </summary>
- public void Warning( object message )
- {
- Push( Level.WARNING, message, IsConsole );
- }
- /// <summary>
- /// 警告ログを出力します
- /// </summary>
- public void Warning( object message, bool isConsole )
- {
- Push( Level.WARNING, message, isConsole );
- }
- /// <summary>
- /// 警告ログを出力します
- /// </summary>
- public void Warning( bool condition, object message )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.WARNING, message, IsConsole );
- }
- /// <summary>
- /// 警告ログを出力します
- /// </summary>
- public void Warning( bool condition, object message, bool isConsole )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.WARNING, message, isConsole );
- }
- /// <summary>
- /// エラーログを出力します
- /// </summary>
- public void Error( object message )
- {
- Push( Level.ERROR, message, IsConsole );
- }
- /// <summary>
- /// エラーログを出力します
- /// </summary>
- public void Error( object message, bool isConsole )
- {
- Push( Level.ERROR, message, isConsole );
- }
- /// <summary>
- /// エラーログを出力します
- /// </summary>
- public void Error( bool condition, object message )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.ERROR, message, IsConsole );
- }
- /// <summary>
- /// エラーログを出力します
- /// </summary>
- public void Error( bool condition, object message, bool isConsole )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.ERROR, message, isConsole );
- }
- /// <summary>
- /// 例外ログを出力します
- /// </summary>
- public void Assert( object message )
- {
- Push( Level.ASSERT, message, IsConsole );
- }
- /// <summary>
- /// 例外ログを出力します
- /// </summary>
- public void Assert( object message, bool isConsole )
- {
- Push( Level.ASSERT, message, isConsole );
- }
- /// <summary>
- /// 例外ログを出力します
- /// </summary>
- public void Assert( bool condition, object message )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.ASSERT, message, IsConsole );
- }
- /// <summary>
- /// 例外ログを出力します
- /// </summary>
- public void Assert( bool condition, object message, bool isConsole )
- {
- if ( condition )
- {
- return;
- }
- Push( Level.ASSERT, message, isConsole );
- }
- /// <summary>
- /// ログをキューに詰めます
- /// </summary>
- public void Push( Level level, object message )
- {
- Push( level, message, IsConsole );
- }
- /// <summary>
- /// ログをキューに詰めます
- /// </summary>
- public void Push( Level level, object message, bool isConsole )
- {
- // キューが一杯だったら後ろを削除します
- if ( MaxLog <= mLogQueue.Count )
- {
- mLogQueue.Dequeue();
- }
- var nguiText = string.Format( "[{0}]{1}[-]", mColorTable[ level ].EncodeColor(), message );
- // キューに詰めます
- mLogQueue.Enqueue( nguiText );
- // コンソールに出力します
- if ( isConsole )
- {
- switch (level)
- {
- case Level.INFO: DebugUtils.Log( message ); break;
- case Level.WARNING: DebugUtils.Warning( message ); break;
- case Level.ERROR: DebugUtils.Error( message ); break;
- case Level.ASSERT: DebugUtils.Assert( message ); break;
- }
- }
- }
- /// <summary>
- /// ログをクリアします
- /// </summary>
- public void Clear()
- {
- mLogQueue.Clear();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement