using UnityEngine;
using System.Collections;
public class GuiDebug : MonoBehaviour {
private bool bShow=false;
public Rect windowRect0 = new Rect(80,100,420,366);
public string mSzLog="";
// Use this for initialization
void Start () {
}
void OnEnable() {
Application.RegisterLogCallback(HandleLog);
}
void OnDisable() {
Application.RegisterLogCallback(null);
}
void HandleLog(string logString, string stackTrace, LogType type) {
//output = logString;
//stack = stackTrace;
mSzLog+=logString+"\n";
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp (KeyCode.P))
{
bShow=!bShow;
if (bShow)
{
OnEnable();
}
else
{
OnDisable();
}
}
}
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(20,20,80,20),"Add"))
{
mSzLog+="Hello!!\n";
}
if(GUI.Button(new Rect(120,20,80,20),"Clear"))
{
mSzLog="";
}
GUI.TextArea(new Rect(20,50,380,300),mSzLog);
//GUI.DragWindow();//
GUI.DragWindow(new Rect(0, 0, 600, 480));
//
}
void OnGUI()
{
if (bShow)
{
windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "Draggable Window");
}
}
}
using System.Collections;
public class GuiDebug : MonoBehaviour {
private bool bShow=false;
public Rect windowRect0 = new Rect(80,100,420,366);
public string mSzLog="";
// Use this for initialization
void Start () {
}
void OnEnable() {
Application.RegisterLogCallback(HandleLog);
}
void OnDisable() {
Application.RegisterLogCallback(null);
}
void HandleLog(string logString, string stackTrace, LogType type) {
//output = logString;
//stack = stackTrace;
mSzLog+=logString+"\n";
}
// Update is called once per frame
void Update () {
if (Input.GetKeyUp (KeyCode.P))
{
bShow=!bShow;
if (bShow)
{
OnEnable();
}
else
{
OnDisable();
}
}
}
void DoMyWindow(int windowID)
{
if(GUI.Button(new Rect(20,20,80,20),"Add"))
{
mSzLog+="Hello!!\n";
}
if(GUI.Button(new Rect(120,20,80,20),"Clear"))
{
mSzLog="";
}
GUI.TextArea(new Rect(20,50,380,300),mSzLog);
//GUI.DragWindow();//
GUI.DragWindow(new Rect(0, 0, 600, 480));
//
}
void OnGUI()
{
if (bShow)
{
windowRect0 = GUI.Window (0, windowRect0, DoMyWindow, "Draggable Window");
}
}
}
这篇博客介绍如何在Unity中实现运行时的日志输出和显示。通过`Application.RegisterLogCallback`方法监听并处理日志,将日志信息存储在字符串变量`mSzLog`中,并在自定义窗口中使用`GUI.TextArea`展示。用户可以通过按键P切换日志显示,并提供按钮方便添加新日志或清空当前日志。
6909

被折叠的 条评论
为什么被折叠?



