这个一个简单日志了,日志只要挂在摄像机上,移动端测试时,自动打印系统错误日志到沙盒目录File底下,也可以在自己想打印的地方使用Log.print(string info)打印。
using UnityEngine;
using System.Collections;
using System.IO;
public class Log : MonoBehaviour
{
static string path;
static int line = 0;
void Start()
{
Application.logMessageReceived += SystemLogPrint;
if(Application.platform==RuntimePlatform.Android)
path = Application.persistentDataPath + "/LogFile.txt";
else
path = Application.dataPath + "/LogFile.txt";
}
void SystemLogPrint(string condition, string stackTrace, LogType type)
{
print(condition);
}
public static void print(string info)
{
line++;
StreamWriter sw;
//Debug.Log (path);
if (line == 1)
{
sw = new StreamWriter(path, false);
string fileTitle = "日志文件创建的时间 " + System.DateTime.Now.ToString();
sw.WriteLine(fileTitle);
}
else
{
sw = new StreamWriter(path, true);
}
string lineInfo = line + "\t" + "时刻 " + Time.time + ": ";
sw.WriteLine(lineInfo);
sw.WriteLine(info);
Debug.Log(info);
sw.Flush();
sw.Close();
}
// Update is called once per frame
void Update()
{
// test
if (Input.GetKeyDown(KeyCode.Space))
{
Log.print("Unity");
}
}
}
版权声明:本文为qq_24807077原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。