refer to
yusong:http://www.xuanyusong.com/archives/2477
Sandals: https://www.cnblogs.com/liangxiegame/p/Unity-you-xi-kuang-jia-da-jian-ba-jian-shao-jia-ba.html
Based on the information on the Internet, it has been integrated and modified.
Use thread brush
The generated files are in the persistentDataPath directory
using UnityEngine; using System.Collections; using System.Collections.Generic; using System.IO; using System.Text; using System.Threading; public class OutLog : MonoBehaviour { private object mLogLock = null; private Thread mFileLogThread = null; static List<string> mLines = new List<string>(); static List<string> mWriteTxt = new List<string>(); private string outpath; /// <summary> /// This manual start can also be manually controlled /// </summary> void Start() { mLogLock = new object (); // Application.persistentDataPath Only this path in Unity can be read or written. outpath = Application.persistentDataPath + " /outLog.txt " ; // Each time the client is started to delete the previously saved Log if (System.IO.File.Exists(outpath)) { File.Delete(outpath); } // Do a Log listener here (old method, deprecated) // Application.RegisterLogCallback(HandleLog); //用线程刷 Application.logMessageReceivedThreaded += HandleLog; this.mFileLogThread = new Thread(new ThreadStart(WriteLog)); this.mFileLogThread.Start(); // One output Debug.Log( " =============Unity Client Log Log ========= " ); } /// <summary> /// Thread brush /// </summary> void WriteLog() { while (true) { // Thread lock lock (mLogLock) { 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); } } } } } /// <summary> /// Use update to refresh (deprecated) /// </summary> void UpdateNotUse() { // Because the operation of writing the file must be completed in the main thread, so write the file for you in 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); } } } public static string getHead() { return "[" + System.DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss fff") + "] "; } void HandleLog(string logString, string stackTrace, LogType type) { mWriteTxt.Add(getHead() + logString); if (type == LogType.Error || type == LogType.Exception) { //Log(logString); //Log(stackTrace); mWriteTxt.Add("ERROR: " + stackTrace); } } // Here I save the wrong information for output on the phone screen (temporarily closed) 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]); } } }