游戏在手机上如何显示Log

关于怎么在手机上查看游戏log的方法。

代码如下:

using System.Collections.Generic;
using UnityEngine;

class TestConsole : MonoBehaviour
{
	struct Log
	{
		public string message;
		public string stackTrace;
		public LogType type;
	}
	//是否限制Log的最大长度
	public bool restrictLogCount = true;
	private int maxLogs = 1000;
	readonly List<Log> logs = new List<Log>();
	Vector2 scrollPosition;
	bool visible;//显示Log面板
	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, 600, 20);
	Rect windowRect = new Rect(margin, margin, Screen.width - (margin * 2), Screen.height - (margin * 2));
	
	void OnEnable()
	{
		#if UNITY_5
		Application.logMessageReceived += HandleLog;
		#else
		Application.RegisterLogCallback(HandleLog);
		#endif
	}
	
	void OnDisable()
	{
		#if UNITY_5
		Application.logMessageReceived -= HandleLog;
		#else
		Application.RegisterLogCallback(null);
		#endif
	}
	
	void OnGUI()
	{
		if(GUI.Button(new Rect(0,0,100,30), "ConsoleLog"))
		{
			visible = !visible;
		}
		if (!visible)
		{
			return;
		}
		windowRect = GUILayout.Window(123456, windowRect, DrawConsoleWindow, windowTitle, GUILayout.Width(600), GUILayout.Height(300));
	}
	void DrawConsoleWindow(int windowID)
	{
		DrawLogsList();
		DrawToolbar();
		GUI.DragWindow(titleBarRect);
	}
	void DrawLogsList()
	{
		scrollPosition = GUILayout.BeginScrollView(scrollPosition);
		for (var i = 0; i < logs.Count; i++)
		{
			var log = logs[i];
			if (collapse && i > 0)
			{
				var previousMessage = logs[i - 1].message;
				if (log.message == previousMessage)
				{
					continue;
				}
			}			
			GUIStyle bb = new GUIStyle();
			bb.wordWrap = true;
			bb.normal.textColor = logTypeColors[log.type];
			GUILayout.Label(log.message, bb, GUILayout.Width(600));
		}
		
		GUILayout.EndScrollView();
		GUI.contentColor = Color.white;
	}
	void DrawToolbar()
	{
		GUILayout.BeginHorizontal();
		
		if (GUILayout.Button(clearLabel, GUILayout.Width(500), GUILayout.Height(20)))
		{
			logs.Clear();
		}
		
		collapse = GUILayout.Toggle(collapse, collapseLabel, GUILayout.ExpandWidth(false), GUILayout.Width(90), GUILayout.Height(20));
		
		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);
	}
}


效果如图:



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

nmg10

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值