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;

            ...

        }

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

相关推荐
阿松爱学习28 分钟前
【Unity开发】Unity长截图功能实现
unity·c#·游戏引擎
小小数媒成员1 小时前
Rect Transform
游戏·unity·游戏引擎
baidu_247438612 小时前
Android 自定义竖直的虚线
android
传奇开心果编程3 小时前
【Jetpack Compose娓娓道来】 第25课:ViewModel就是UI界面的大脑
android·kotlin·移动开发·jetpack compose·编程语言理论·编程框架·声明式ui
马立杰3 小时前
PHP 未授权 RCE 应急响应:CVE-2026-109618 CVE-2026-109619 排查与加固实战
android·开发语言·php
NamoAmitabha1283 小时前
安装“编译器环境”(Android Studio)--deepseek--Cocos --Creator3.8.8
android·ide·android studio
聚美智数4 小时前
文档转换 API‑文档转 PDF‑PDF 转文档‑异步文档处理‑文件格式迁移‑转换结果回调推送 API 接口介绍
android·java·pdf
booboosui4 小时前
Android 上的无人回合:只放开 2 类工具,宁可空手
android
事圆则缓6 小时前
Android 性能排查实战:怎么看内存抖动、CPU 占用和 FPS
android
mmsx6 小时前
Android 三类「看着无关」的故障:大事务、UI 阻塞与 ViewModel 作用域
android·ui