在android手机中生成日志

来源:互联网 发布:网络负面新闻相关图片 编辑:程序博客网 时间:2024/05/05 12:09

把这个脚本挂载到场景中;保存路径文件路径:内部储存设备/Android/data/包名/Files/OutLog.txt 如果设置的储存权限设为sdcard则:SDCard/Android/包名/Files/OutLog.txt然后可以通过文件管理工具在对应目录找到该日志,也可以扩展为上传到服务器。 该日志内容包括了使用UnityEngine.Debug.XXXX的信息,即在unity编辑器输出到unity控制台的信息。


public class OutLog : MonoBehaviour{#if UNITY_ANDROID    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 (File.Exists(outpath))        {            File.Delete(outpath);        }        Application.logMessageReceived += HandleLog;    }    void 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);        }    }        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];            }        }        if (Application.isPlaying)        {            if (mLines.Count > 20)            {                mLines.RemoveAt(0);            }            mLines.Add(text);        }    }#endif}



阅读全文
0 0
原创粉丝点击