Unity 打包后的日志文件

using System;
using System.Text;
using UnityEngine;
using System.IO;
using System.Diagnostics;
public sealed class DebugMonitor : MonoBehaviour
{
	//
	private StringBuilder _builder = new StringBuilder();
	
	//文件保存路径
	private string _logPath = "";
	 
	 //是否自动清理
	 public bool isAutoClear = true;
	
	 //多久后清理
	 public int maxDay = 2;
	
	 private void Start()
	 {
#if UNITY_EDITOR
        return;
#endif
		Init();
	 }	
	 private void OnDestroy()
	 {
		 Application.logMessageReceived -= LogMessage;
         Application.quitting -= ApplicationQuit;
	 }
	
	 //初始化
	 private void Init()
	 {
	 	 //不同平台下,设置不同路径
	 	 _logPath = Application.streamingAssetsPath + "/DebugMessage.txt";
		
		 // debug 日志 回调
         Application.logMessageReceived += LogMessage;
  
  		 //程序退出中 回调
         Application.quitting += ApplicationQuit;
         
		 //
		 if(isAutoClear && File.Exists(_logPath))
		 {
		 	try
            {
                DateTime _lastTime = File.GetLastWriteTime(_logPath);
                if ((DateTime.Now - _lastTime).TotalDays > maxDay)
                {
                    DeleteFile(_logPath);
                }
            }
            catch { }
		 }
	 }
     
    /// <summary>
    /// 程序退出
    /// </summary>
    private void ApplicationQuit()
    {
        //
        _builder.Append(DateTime.Now + "-----------------------退出程序---------------------------" + "\n");

        //
        SaveToTxt(_logPath, _builder.ToString());
    }
    
    /// <summary>
    /// 实时监听 打印日志
    /// </summary>
    /// <param name="condition"></param>
    /// <param name="stackTrace"></param>
    /// <param name="type"></param>
    private void LogMessage(string condition, string stackTrace, LogType type)
    {
        //string all = string.Format("第一个参数 : {0} 。 第二个参数 : {1} 。 第三个参数 : {2}", condition, stackTrace, type.ToString());

        _builder.Append(type);
        _builder.Append("->");
        _builder.Append("Info->");
        _builder.Append(condition);
        _builder.Append("\n");

        //获取堆栈调用信息
        StackTrace st = new StackTrace(true);
        var frames = st.GetFrames();

        for (int i = 0; i < frames.Length; i++)
        {
            _builder.Append(i);
            _builder.Append("\n");

            //方法
            var method = frames[i].GetMethod();
            _builder.Append("Method->");
            _builder.Append(method);
            _builder.Append("\n");

            //文件类
            var fileName = frames[i].GetFileName();
            _builder.Append("File->");
            _builder.Append(fileName);
            _builder.Append("\n");
        }

        _builder.Append("\n");
    }
    
    private void DeleteFile(string filePath)
    {
 		if (File.Exists(filePath))
 		{
 			try
            {
                File.SetAttributes(filePath, FileAttributes.Normal);
                File.Delete(filePath);
            }
            catch (IOException e)
            {
                Debug.LogError(e.Message + "\n" + filePath);
            }
		}
 	}
	
	private bool SaveToTxt(string filePath,string source)
	{
	    if (string.IsNullOrWhiteSpace(filePath))
        {
             Debug.Log("filePath is null");
             return false;
        }
        
        if (source == null)
        {
             Debug.Log("source is not allowed to be null");
             return false;
        }
		
		try
        {
             using(FileStream stream = new FileStream(filePath, FileMode.Append, FileAccess.Write))
             {
                  byte[] arr = Encoding.UTF8.GetBytes(source);
                  stream.Write(arr,0,arr.Length);
             }
        }
        catch (Exception e)
        {
              Debug.LogError(e.Message);
              return false;
         }

         Debug.Log(source.GetType() + " Txt Save Success~");
         return true;
	}
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!对于Unity签名后无法打包的问题,有几个常见的原因可能导致这种情况发生。 首先,请确保您已经正确地签名了Unity项目。签名是为了确保应用程序的安全性和完整性,以便在设备上运行。您可以按照以下步骤进行检查和设置签名: 1. 打开Unity编辑器,并选择“File(文件)”菜单下的“Build Settings(构建设置)”选项。 2. 在弹出的对话框中,选择您的目标平台(例如Android或iOS)。 3. 在选定平台的构建设置中,找到并选择“Player Settings(播放器设置)”按钮。 4. 在Player Settings窗口中,找到并展开“Other Settings(其他设置)”部分。 5. 在“Identification(标识)”下,您可以设置应用程序的包名(Android)或Bundle Identifier(iOS)。 6. 确保您提供了正确的包名或Bundle Identifier,并保存设置。 如果您已经正确签名了Unity项目,但仍然无法打包,请检查以下可能的问题: 1. 检查证书和密钥:如果您在打包过程中使用了证书和密钥,确保它们有效且与项目匹配。检查证书是否过期,并确保使用正确的密钥来签名应用程序。 2. 检查权限和配置:某些平台(例如Android)可能要求在打包之前配置许多权限和设置。请确保您正确配置了所需的权限和设置,以便应用程序可以顺利打包。 3. 检查日志和错误信息:在打包过程中,Unity通常会生成日志和错误信息。请查看这些日志和错误信息,以获取更多关于问题的详细信息。根据这些信息,您可以更容易地确定问题所在并采取相应的解决方法。 如果您遇到了具体的错误消息或其他详细信息,请提供给我,我将尽力帮助您解决问题。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值