UIFramework之UnityEngine.Debug封装

本文介绍如何在Unity中封装Log系统,实现真机输出Log到文件的功能,并通过C#类库进一步封装Log信息输出,便于控制和调试。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

UIFramework之UnityEngine.Debug封装

1、真机输出Log到文件

把Log信息写在手机的客户端里。把脚本挂在任意游戏对象上。

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using UnityEngine;  
  2. using System.Collections;  
  3. using System.Collections.Generic;  
  4. using System.IO;  
  5. using System.Text;  
  6.   
  7. public class LogOut : MonoBehaviour   
  8. {  
  9.     static List<string> mLines = new List<string>();  
  10.     static List<string> mWriteTxt = new List<string>();  
  11.     private string outpath;  
  12.     void Start()  
  13.     {  
  14.         //Application.persistentDataPath 安卓沙盒路径  
  15.         outpath = Application.persistentDataPath + "/Log.txt";  
  16.         //每次启动客户端删除之前保存的Log  
  17.         if (System.IO.File.Exists(outpath))  
  18.         {  
  19.             File.Delete(outpath);  
  20.         }  
  21.         //在这里做一个Log的监听  
  22.         //Application.RegisterLogCallback(HandleLog);  
  23.         Application.logMessageReceived += HandleLog;  
  24.     }  
  25.   
  26.     void Update()  
  27.     {  
  28.         //因为写入文件的操作必须在主线程中完成,所以在Update中哦给你写入文件。  
  29.         if (mWriteTxt.Count > 0)  
  30.         {  
  31.             string[] temp = mWriteTxt.ToArray();  
  32.             foreach (string t in temp)  
  33.             {  
  34.    using (StreamWriter writer = new StreamWriter(outpath, true, Encoding.UTF8))  
  35.                 {  
  36.                     writer.WriteLine(t);  
  37.                 }  
  38.                 mWriteTxt.Remove(t);  
  39.             }  
  40.         }  
  41.     }  
  42.   
  43.     void HandleLog(string logString, string stackTrace, LogType type)  
  44.     {  
  45.         mWriteTxt.Add(logString);  
  46.         if (type == LogType.Error || type == LogType.Exception)  
  47.         {  
  48.             Log(logString);  
  49.             Log(stackTrace);  
  50.         }  
  51.     }  
  52.   
  53.     //把错误的信息保存起来,用来输出在手机屏幕上  
  54.     static public void Log(params object[] objs)  
  55.     {  
  56.         string text = "";  
  57.         for (int i = 0; i < objs.Length; ++i)  
  58.         {  
  59.             if (i == 0)  
  60.             {  
  61.                 text += objs[i].ToString();  
  62.             }  
  63.             else  
  64.             {  
  65.                 text += ", " + objs[i].ToString();  
  66.             }  
  67.         }  
  68.         if (Application.isPlaying)  
  69.         {  
  70.             if (mLines.Count > 20)  
  71.             {  
  72.                 mLines.RemoveAt(0);  
  73.             }  
  74.             mLines.Add(text);  
  75.   
  76.         }  
  77.     }  
  78.       
  79.     void OnGUI()  
  80.     {  
  81.         GUI.color = Color.red;  
  82.         for (int i = 0, imax = mLines.Count; i < imax; ++i)  
  83.         {  
  84.             GUILayout.Label(mLines[i]);  
  85.         }  
  86.     }  
  87. }  


打包之前在Android的Player Setting里面选择WriteAccess (写入访问)。
Internal Only:表示Application.persistentDataPath的路径是应用程序沙盒,(需要root不然访问不了写入的文件)。
文件路径:data/data/包名/Files/OutLog.txt
External(SDCard):表示Application.persistentDataPath的路径是SDCard的路径。(不需要root就可以访问文件)。
文件路径:SDCard/Android/包名/Files/OutLog.txt
总结 Application.persistentDataPath 会根据你的WriteAccess选项而产生对应的路径。



2、Log信息的封装

我们的Log信息的输出,需要再次进行封装。

我们新建C#类库工程:



需要引用UnityEngine.dll。

[csharp]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Text;  
  4. using UnityEngine;  
  5. public class Logger  
  6. {  
  7.     public static bool EnableLogger = true;  
  8.   
  9.     public static void Log(object message)  
  10.     {  
  11.         if (EnableLogger) Debug.Log(message);  
  12.     }  
  13.   
  14.     public static void Log(object message, UnityEngine.Object context)  
  15.     {  
  16.         if (EnableLogger) Debug.Log(message, context);  
  17.     }  
  18.   
  19.     public static void LogError(object message)  
  20.     {  
  21.         if (EnableLogger) Debug.LogError(message);  
  22.     }  
  23.   
  24.     public static void LogError(object message, UnityEngine.Object context)  
  25.     {  
  26.         if (EnableLogger) Debug.LogError(message, context);  
  27.     }  
  28.   
  29.     public static void LogErrorFormat(string format, params object[] args)  
  30.     {  
  31.         if (EnableLogger) Debug.LogErrorFormat(format, args);  
  32.     }  
  33.   
  34.     public static void LogErrorFormat(UnityEngine.Object context,  
  35.                         string format, params object[] args)  
  36.     {  
  37.         if (EnableLogger) Debug.LogErrorFormat(context, format, args);  
  38.     }  
  39.   
  40.     public static void LogException(Exception exception)  
  41.     {  
  42.         if (EnableLogger) Debug.LogException(exception);  
  43.     }  
  44.   
  45.     public static void LogException(Exception exception,  
  46.                                         UnityEngine.Object context)  
  47.     {  
  48.         if (EnableLogger) Debug.LogException(exception, context);  
  49.     }  
  50.   
  51.     public static void LogFormat(string format, params object[] args)  
  52.     {  
  53.         if (EnableLogger) Debug.LogFormat(format, args);  
  54.     }  
  55.   
  56.     public static void LogFormat(UnityEngine.Object context,  
  57.                                     string format, params object[] args)  
  58.     {  
  59.         if (EnableLogger) Debug.LogFormat(context, format, args);  
  60.     }  
  61.   
  62.     public static void LogWarning(object message)  
  63.     {  
  64.         if (EnableLogger) Debug.LogWarning(message);  
  65.     }  
  66.   
  67.     public static void LogWarning(object message,  
  68.                                     UnityEngine.Object context)  
  69.     {  
  70.         if (EnableLogger) Debug.LogWarning(message, context);  
  71.     }  
  72.   
  73.     public static void LogWarningFormat(string format,  
  74.                                             params object[] args)  
  75.     {  
  76.         if (EnableLogger) Debug.LogWarningFormat(format, args);  
  77.     }  
  78.   
  79.     public static void LogWarningFormat(UnityEngine.Object context,  
  80.                                         string format, params object[] args)  
  81.     {  
  82.         if (EnableLogger) Debug.LogWarningFormat(context, format, args);  
  83.     }  
  84. }  



设置工程属性。


我们可以在工程目录里面找到Logger.dll文件,这样我们就完成了对Unity的Log的封装。

我们将Logger.dll文件放入Unity的Plugins文件目录下。


这样就可以在我们的Unity工程中直接使用了。

设置

Logger.EnableLogger = false;

就可以关闭Log的输出了。


=====================================================================

结束。


在Unity中使用脚本进行调试时,如果遇到错误提示“debug is an ambiguous reference between \"UnityEngine.Debug\" and \"System.Diagnositivs.Debug\"”,这是因为在代码中同时引用了UnityEngine.Debug和System.Diagnostics.Debug,导致编译器无法确定你要使用的是哪一个Debug类。 要解决这个问题,可以采取以下几种方法: 1. **使用完整命名空间**: 在使用Debug类时,明确指定其命名空间。例如,如果你想使用UnityEngine.Debug,可以使用: ```csharp UnityEngine.Debug.Log("This is a log message"); ``` 如果你想使用System.Diagnostics.Debug,可以使用: ```csharp System.Diagnostics.Debug.WriteLine("This is a debug message"); ``` 2. **使用别名**: 你可以在文件顶部为其中一个Debug类创建一个别名,以避免歧义。例如: ```csharp using UnityDebug = UnityEngine.Debug; using SystemDebug = System.Diagnostics.Debug; ``` 然后在代码中使用别名: ```csharp UnityDebug.Log("This is a log message"); SystemDebug.WriteLine("This is a debug message"); ``` 3. **移除不必要的using语句**: 如果你只需要使用其中一个Debug类,可以在代码顶部移除不需要的using语句。例如,如果你只需要使用UnityEngine.Debug,可以移除或注释掉`using System.Diagnostics;`。 4. **使用静态导入**: 你可以在文件顶部使用静态导入来导入特定的Debug类。例如: ```csharp using static UnityEngine.Debug; ``` 然后在代码中直接使用Debug方法: ```csharp Log("This is a log message"); ``` 通过以上方法,你可以明确指定要使用的Debug类,从而解决歧义问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值