内容目录
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class logdata
{
public string output = "";
public string stack = "";
public static logdata Init(string o,string s)
{
logdata log = new logdata();
log.output = o;
log.stack = s;
return log;
}
public void Show(/*bool showstack*/)
{
GUILayout.Label(output);
//if (showstack)
GUILayout.Label(stack);
}
}
/// <summary>
/// 手机调试脚本
/// 本脚本挂在一个空对象或转换场景时不删除的对象即可
/// 错误和异常输出日记路径 Application.persistentDataPath
/// </summary>
public class ShowDebugInPhone : MonoBehaviour
{
List<logdata> logDatas = new List<logdata>();//log链表
List<logdata> errorDatas = new List<logdata>();//错误和异常链表
List<logdata> warningDatas = new List<logdata>();//警告链表
static List<string> mWriteTxt = new List<string>();
Vector2 uiLog;
Vector2 uiError;
Vector2 uiWarning;
bool open = false;
bool showLog = false;
bool showError = false;
bool showWarning = false;
private string outpath;
void Start()
{
//Application.persistentDataPath Unity中只有这个路径是既可以读也可以写的。
//Debug.Log(Application.persistentDataPath);
outpath = Application.persistentDataPath + "/outLog.txt";
//每次启动客户端删除之前保存的Log
if (System.IO.File.Exists(outpath))
{
File.Delete(outpath);
}
//转换场景不删除
Application.DontDestroyOnLoad(gameObject);
}
void OnEnable()
{
//注册log监听
Application.RegisterLogCallback(HangleLog);
}
void OnDisable()
{
// Remove callback when object goes out of scope
//当对象超出范围,删除回调。
Application.RegisterLogCallback(null);
}
void HangleLog(string logString, string stackTrace, LogType type)
{
switch (type)
{
case LogType.Log:
logDatas.Add(logdata.Init(logString, stackTrace));
break;
case LogType.Error:
case LogType.Exception:
errorDatas.Add(logdata.Init(logString, stackTrace));
mWriteTxt.Add(logString);
mWriteTxt.Add(stackTrace);
break;
case LogType.Warning:
warningDatas.Add(logdata.Init(logString, stackTrace));
break;
}
}
void Update()
{
//因为写入文件的操作必须在主线程中完成,所以在Update中才给你写入文件。
if (errorDatas.Count > 0)
{
string[] temp = mWriteTxt.ToArray();
foreach (string t in temp)
{
using (StreamWriter writer = new StreamWriter(outpath, true, Encoding.UTF8))
{
writer.WriteLine(t);
}
mWriteTxt.Remove(t);
}
}
}
void OnGUI()
{
GUILayout.BeginHorizontal();
if (GUILayout.Button(">>Open", GUILayout.Height(150), GUILayout.Width(150)))
open = !open;
if (open)
{
if (GUILayout.Button("清理", GUILayout.Height(150), GUILayout.Width(150)))
{
logDatas = new List<logdata>();
errorDatas = new List<logdata>();
warningDatas = new List<logdata>();
}
if (GUILayout.Button("显示log日志:" + showLog, GUILayout.Height(150), GUILayout.Width(200)))
{
showLog = !showLog;
if (open == true)
open = !open;
}
if (GUILayout.Button("显示error日志:" + showError, GUILayout.Height(150), GUILayout.Width(200)))
{
showError = !showError;
if (open == true)
open = !open;
}
if (GUILayout.Button("显示warning日志:" + showWarning, GUILayout.Height(150), GUILayout.Width(200)))
{
showWarning = !showWarning;
if (open == true)
open = !open;
}
}
GUILayout.EndHorizontal();
if (showLog)
{
GUI.color = Color.white;
uiLog = GUILayout.BeginScrollView(uiLog);
foreach (var va in logDatas)
{
va.Show();
}
GUILayout.EndScrollView();
}
if (showError)
{
GUI.color = Color.red;
uiError = GUILayout.BeginScrollView(uiError);
foreach (var va in errorDatas)
{
va.Show();
}
GUILayout.EndScrollView();
}
if (showWarning)
{
GUI.color = Color.yellow;
uiWarning = GUILayout.BeginScrollView(uiWarning);
foreach (var va in warningDatas)
{
va.Show();
}
GUILayout.EndScrollView();
}
}
}
用法:把上面的代码复制一下,把它挂到游戏场景中的任意一个物体即可,发布apk就可以进行查看信息
这样就可以输出你的错误信息和你的调试信息。非常方便的工具可以提高开发效率
、
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Text;
public class ErrorDisplay : MonoBehaviour {
static List<string> mLines = new List<string>();
static List<string> mWriteTxt = new List<string>();
private string outpath;
void Start()
{
//Application.persistentDataPath Unity中只有这个路径是既可以读也可以写的。
outpath = Application.persistentDataPath + "/outLog.txt";
//每次启动客户端删除之前保存的Log
if (System.IO.File.Exists(outpath))
{
File.Delete(outpath);
}
//在这里做一个Log的监听
//转载的原文中是用Application.RegisterLogCallback(HandleLog);但是这个方法在unity5.0版本已经废弃不用了
Application.logMessageReceived += HandleLog;
//一个输出
Debug.Log("chenj_freedom~~~~~");
}
void Update()
{
//因为写入文件的操作必须在主线程中完成,所以在Update中哦给你写入文件。
if (mWriteTxt.Count > 0)
{
string[] temp = mWriteTxt.ToArray();
foreach (string t in temp)
{
using (StreamWriter writer = new StreamWriter(outpath, true, Encoding.UTF8))
{
writer.WriteLine(t);
}
mWriteTxt.Remove(t);
}
}
}
void HandleLog(string logString, string stackTrace, LogType type)
{
mWriteTxt.Add(logString);
if (type == LogType.Error || type == LogType.Exception)
{
Log(logString);
Log(stackTrace);
}
}
//这里我把错误的信息保存起来,用来输出在手机屏幕上
static public void Log(params object[] objs)
{
string text = "";
for (int i = 0; i < objs.Length; ++i)
{
if (i == 0)
{
text += objs[i].ToString();
}
else
{
text += ", " + objs[i].ToString();
}
}
if (Application.isPlaying)
{
if (mLines.Count > 20)
{
mLines.RemoveAt(0);
}
mLines.Add(text);
}
}
void OnGUI()
{
GUI.color = Color.red;
for (int i = 0, imax = mLines.Count; i < imax; ++i)
{
GUILayout.Label(mLines[i]);
}
}
}
生成的log文件在什么位置?
当项目编译的时候,Player Settings中的Write Acess选择“Internal Only”,那么log文件在data/data/包名/Files/outLog.txt(需要root权限);如果Write Acess选择“External (SDCard)”,那么log文件在SDCard/Android/包名/Files/outLog.txt,推荐后者。
