Unity在运行时候打印log输出日志

这篇博客介绍如何在Unity中实现运行时的日志输出和显示。通过`Application.RegisterLogCallback`方法监听并处理日志,将日志信息存储在字符串变量`mSzLog`中,并在自定义窗口中使用`GUI.TextArea`展示。用户可以通过按键P切换日志显示,并提供按钮方便添加新日志或清空当前日志。
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"); 
}
}
}
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值