#游戏unity-音之国度#战斗系统中的实时显示状态
在游戏中能够实时显示回合制次数的一般的做法就是调用GUI中的Label,还可以添加一些艺术字的效果。我也是那么做的,但是博主想到,能不能做一个类似控台的样子的界面实时输出角色状态呢。于是就有了这篇博客。
既然是模仿控台的输出,那么就是要用代码实现滚动条。
其实本质就是写一个面板,往上面写字,并且与代码实时连接起来就好啦。
原理很简单,上代码
这里设置了一个切换按键,就是可以控制是否显示这个面板的,按键为BackQuote,就是在ESC按键的下面就是这个“~”,你也可以修改。
using System.Collections.Generic;
using UnityEngine;
namespace Consolation
{
class Test : MonoBehaviour
{
struct Log
{
public string message;
public string stackTrace;
public LogType type;
}
public KeyCode toggleKey = KeyCode.BackQuote;
public bool shakeToOpen = true;
public float shakeAcceleration = 3f;
public bool restrictLogCount = false;
public int maxLogs = 1000;
readonly List<Log> logs = new List<Log>();
Vector2 scrollPosition;
bool visible;
bool collapse;
static readonly Dictionary<LogType, Color> logTypeColors = new Dictionary<LogType, Color>
{
{ LogType.Assert, Color.white },
{ LogType.Error, Color.red },
{ LogType.Exception, Color.red },
{ LogType.Log, Color.white },
{ LogType.Warning, Color.yellow },
};
const string windowTitle = "Console";
const int margin = 20;
static readonly GUIContent clearLabel = new GUIContent("Clear", "Clear the contents of the console.");
static readonly GUIContent collapseLabel = new GUIContent("Collapse", "Hide repeated messages.");
readonly Rect titleBarRect = new Rect(0, 0, 10000, 20);
Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));
void OnEnable()
{
Application.logMessageReceived += HandleLog;
}
void OnDisable()
{
Application.logMessageReceived -= HandleLog;
void Update()
{
if (Input.GetKeyDown(toggleKey))
{
visible = !visible;
}
if (shakeToOpen && Input.acceleration.sqrMagnitude > shakeAcceleration)
{
visible = true;
}
}
void OnGUI()
{
if (!visible)
{
return;
}
windowRect = GUILayout.Window(123456, windowRect, DrawConsoleWindow, windowTitle);
}
void DrawConsoleWindow(int windowID)
{
DrawLogsList();
DrawToolbar();
// Allow the window to be dragged by its title bar.
GUI.DragWindow(titleBarRect);
}
void DrawLogsList()
{
scrollPosition = GUILayout.BeginScrollView(scrollPosition);
// Iterate through the recorded logs.
for (var i = 0; i < logs.Count; i++)
{
var log = logs[i];
// Combine identical messages if collapse option is chosen.
if (collapse && i > 0)
{
var previousMessage = logs[i - 1].message;
if (log.message == previousMessage)
{
continue;
}
}
GUI.contentColor = logTypeColors[log.type];
GUILayout.Label(log.message);
}
GUILayout.EndScrollView();
// Ensure GUI colour is reset before drawing other components.
GUI.contentColor = Color.white;
}
/// <summary>
/// Displays options for filtering and changing the logs list.
/// </summary>
void DrawToolbar()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button(clearLabel))
{
logs.Clear();
}
collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false));
GUILayout.EndHorizontal();
}
void HandleLog(string message, string stackTrace, LogType type)
{
logs.Add(new Log
{
message = message,
stackTrace = stackTrace,
type = type,
});
TrimExcessLogs();
}
void TrimExcessLogs()
{
if (!restrictLogCount)
{
return;
}
var amountToRemove = Mathf.Max(logs.Count - maxLogs, 0);
if (amountToRemove == 0)
{
return;
}
logs.RemoveRange(0, amountToRemove);
}
}
}
接下来是测试代码,新建一个脚本来写
// Update is called once per frame
void Update ()
{
Debug.Log("Debug log test");
Console.WriteLine("this is Console Write line test ");
}
效果图如下