欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

如何保存Unity中的Log日志

 更新時(shí)間:2021年04月09日 15:13:01   作者:q527955281  
這篇文章主要介紹了如何保存Unity中的Log日志的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

代碼中的debug日志保存本地

using System.Collections;
using UnityEngine;
using System.IO; 
public class SaveLog : MonoBehaviour
{
    private float length;
    Queue queue;
 
    private void Awake()
    {
        DontDestroyOnLoad(this);
        LogToFile("Version of the runtime: " + Application.unityVersion, true, false);
        Application.logMessageReceivedThreaded += OnReceiveLogMsg;
        queue = new Queue();
    }
    // Start is called before the first frame update    
    void OnReceiveLogMsg(string condition, string stackTrace, LogType type) {
        string _type = "";
        switch (type)
        {
            case LogType.Error:
                _type = "error";
                break;
            case LogType.Assert:
                _type = "Assert";
                break;
            case LogType.Warning:
                _type = "Warning";
                break;
            case LogType.Log:
                _type = "Log";
                break;
            case LogType.Exception:
                _type = "Exception";
                break;
            default:
                break;
        }
        string msg = "[MSG]:" + condition + "--" + "[station]:" + stackTrace + "-" + "[LogType]:" + _type;
        queue.Enqueue(msg);
    }
    // Update is called once per frame
    void Update()
    {
        CheckLogs();
    }
    void CheckLogs() {
        if (queue.Count != 0) {
            LogToFile(queue.Peek().ToString(), true, true,()=> { queue.Dequeue(); });           
        }
    }
    public  void LogToFile(string str, bool bwithTime, bool bAppendLineFeed,System.Action callback = null) {
        if (str == null) return;
        try
        {
#if UNITY_EDITOR
            string fname = Application.dataPath + "/Unitylog.txt";
#else
            string fname = Application.persistentDataPath+ "/Unitylog.txt";
#endif 
            if (fname == "" || fname == null) return; 
            StreamWriter writer = new StreamWriter(fname, true, System.Text.Encoding.Default);
            
            if (bwithTime) writer.WriteLine("\r\n\r\n---------" + System.DateTime.Now.ToString());
            if (bAppendLineFeed) writer.WriteLine(str);
            else writer.Write(str);
            writer.Close();
            callback?.Invoke();
        }
        catch
        { 
            throw;
        }
    }
}

結(jié)果:

補(bǔ)充:Unity3D log寫(xiě)入文件

關(guān)鍵代碼:Application.RegisterLogCallback(logCallBack);

using UnityEngine;
using System.IO; 
public class Logger
{
    string fullPath; 
    public void InitLogger()
    {
        fullPath = Application.dataPath + "/output.txt";
        if (File.Exists(fullPath)) File.Delete(fullPath);
        Debug.Log(fullPath.Replace("/output.txt", ""));
        if (Directory.Exists(fullPath.Replace("/output.txt", "")))
        {
            FileStream fs = File.Create(fullPath);
            fs.Close();
            Application.logMessageReceived += logCallBack;
        }
        else
        {
            Debug.LogError("directory is not exist");
        }
    } 
 
    private void logCallBack(string condition, string stackTrace, LogType type)
    {
        if (File.Exists(fullPath))
        {
            using (StreamWriter sw = File.AppendText(fullPath))
            {
                sw.WriteLine(condition);
                sw.WriteLine(stackTrace);
            }
        }
    }
 
    private static Logger s_instance;
    public static Logger instance
    {
        get
        {
            if (null == s_instance)
                s_instance = new Logger();
            return s_instance;
        }
    }
}
 

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教。

相關(guān)文章

  • 淺談C#中的Infinity和NaN

    淺談C#中的Infinity和NaN

    下面小編就為大家?guī)?lái)一篇淺談C#中的Infinity和NaN。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-07-07
  • 實(shí)例代碼講解c# 線程(下)

    實(shí)例代碼講解c# 線程(下)

    這篇文章主要介紹了c# 線程的的相關(guān)資料,文中示例代碼非常細(xì)致,對(duì)大家的學(xué)習(xí)有很大幫助,感興趣的朋友可以了解下
    2020-06-06
  • C#實(shí)現(xiàn)QQ窗口抖動(dòng)效果

    C#實(shí)現(xiàn)QQ窗口抖動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了C#實(shí)現(xiàn)QQ窗口抖動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-11-11
  • C# WPF 通過(guò)委托實(shí)現(xiàn)多窗口間的傳值的方法

    C# WPF 通過(guò)委托實(shí)現(xiàn)多窗口間的傳值的方法

    這篇文章主要介紹了C# WPF 通過(guò)委托實(shí)現(xiàn)多窗口間的傳值的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-09-09
  • C#使用IComparer自定義List類(lèi)實(shí)現(xiàn)排序的方法

    C#使用IComparer自定義List類(lèi)實(shí)現(xiàn)排序的方法

    這篇文章主要介紹了C#使用IComparer自定義List類(lèi)實(shí)現(xiàn)排序的方法,涉及C#使用IComparer接口定義List類(lèi)進(jìn)行排序的相關(guān)技巧,需要的朋友可以參考下
    2015-08-08
  • C#創(chuàng)建安全的棧(Stack)存儲(chǔ)結(jié)構(gòu)

    C#創(chuàng)建安全的棧(Stack)存儲(chǔ)結(jié)構(gòu)

    這篇文章主要為大家詳細(xì)介紹了C#創(chuàng)建安全的棧(Stack)存儲(chǔ)結(jié)構(gòu)的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-10-10
  • C#中const和readonly的用法比較

    C#中const和readonly的用法比較

    今天小編就為大家分享一篇關(guān)于C#中const和readonly的用法比較,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧
    2018-10-10
  • Unity實(shí)現(xiàn)物體弧線運(yùn)動(dòng)到規(guī)定的坐標(biāo)

    Unity實(shí)現(xiàn)物體弧線運(yùn)動(dòng)到規(guī)定的坐標(biāo)

    這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)物體以弧線的形式運(yùn)動(dòng)到規(guī)定的坐標(biāo),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2020-06-06
  • c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼

    c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼

    這篇文章主要介紹了c# 獲取照片的經(jīng)緯度和時(shí)間的示例代碼,幫助大家更好的理解和使用c#,感興趣的朋友可以了解下
    2020-11-11
  • Unity常用音頻操作類(lèi)示例代碼

    Unity常用音頻操作類(lèi)示例代碼

    這篇文章主要介紹了Unity常用音頻操作類(lèi),本文通過(guò)示例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-07-07

最新評(píng)論