Unity Android Logcat插件 输出日志中文乱码解决

  • 背景

之前安卓真机调试看日志,一直用的是Android Studio自带的adb命令进行看日志,不太方便,改用Unity自带的安卓日志插件时,存在中文日志乱码问题。

  • 插件安装

基于Unity6000.1.11版本:Window -> Package Management -> Package Manager,即可打开包管理工具,选择Unity Registry分类,找到Android Logcat插件,导入即可。

  • 插件使用

打开插件:Window -> Analysis -> Android Logcat,手机开启调试模式后,使用数据线连接电脑,在该插件面板默认会自动选择手机并输出日志(需手机先运行程序,每次运行后,每次选择)。可在顶部选择要调试程序的包名,输出的日志更整洁。

  • 乱码解决

找到插件源码:Editor/AndroidLogcatMessageProvider.cs

cs 复制代码
        public override void Start()
        {
            var arguments = LogcatArguments();
            AndroidLogcatInternalLog.Log("\n\nStarting logcat\n\n");
            AndroidLogcatInternalLog.Log("{0} {1}", m_ADB.GetADBPath(), arguments);
            m_LogcatProcess = new Process();
            m_LogcatProcess.StartInfo.FileName = m_ADB.GetADBPath();
            m_LogcatProcess.StartInfo.Arguments = arguments;
            m_LogcatProcess.StartInfo.RedirectStandardError = true;
            m_LogcatProcess.StartInfo.RedirectStandardOutput = true;
            m_LogcatProcess.StartInfo.UseShellExecute = false;
            m_LogcatProcess.StartInfo.CreateNoWindow = true;

            //中文乱码修复,增加两行代码
            m_LogcatProcess.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;
            m_LogcatProcess.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;

            m_LogcatProcess.OutputDataReceived += OutputDataReceived;
            m_LogcatProcess.ErrorDataReceived += OutputDataReceived;
            m_LogcatProcess.Start();

            m_LogcatProcess.BeginOutputReadLine();
            m_LogcatProcess.BeginErrorReadLine();
        }

找到插件源码:Editor/AndroidTools/Shell.cscishi

cs 复制代码
        internal static ShellReturnInfo RunProcess(ShellStartInfo startInfo)
        {
            Process process = new Process();
            process.StartInfo.FileName = startInfo.FileName;
            process.StartInfo.Arguments = startInfo.Arguments;
            process.StartInfo.WorkingDirectory = startInfo.WorkingDirectory;
            process.StartInfo.UseShellExecute = false;
            process.StartInfo.RedirectStandardOutput = true;
            process.StartInfo.RedirectStandardError = true;
            process.StartInfo.CreateNoWindow = true;

            //中文乱码修复,增加两行代码
            process.StartInfo.StandardOutputEncoding = Encoding.UTF8;
            process.StartInfo.StandardErrorEncoding = Encoding.UTF8;

            ...

        }

此时,日志就能够正常显示中了。

相关推荐
安东尼肉店1 小时前
Android compose屏幕适配终极解决方案
android
2501_916007471 小时前
HTTPS 抓包乱码怎么办?原因剖析、排查步骤与实战工具对策(HTTPS 抓包乱码、gzipbrotli、TLS 解密、iOS 抓包)
android·ios·小程序·https·uni-app·iphone·webview
feiyangqingyun2 小时前
基于Qt和FFmpeg的安卓监控模拟器/手机摄像头模拟成onvif和28181设备
android·qt·ffmpeg
用户2018792831676 小时前
ANR之RenderThread不可中断睡眠state=D
android
煤球王子6 小时前
简单学:Android14中的Bluetooth—PBAP下载
android
小趴菜82277 小时前
安卓接入Max广告源
android
齊家治國平天下7 小时前
Android 14 系统 ANR (Application Not Responding) 深度分析与解决指南
android·anr
ZHANG13HAO7 小时前
Android 13.0 Framework 实现应用通知使用权默认开启的技术指南
android
【ql君】qlexcel7 小时前
Android 安卓RIL介绍
android·安卓·ril
写点啥呢7 小时前
android12解决非CarProperty接口深色模式设置后开机无法保持
android·车机·aosp·深色模式·座舱