- 背景
? ? ? ? 之前安卓真機調試看日志,一直用的是Android Studio自帶的adb命令進行看日志,不太方便,改用Unity自帶的安卓日志插件時,存在中文日志亂碼問題。
- 插件安裝
? ? ? ? 基于Unity6000.1.11版本:Window -> Package Management -> Package Manager,即可打開包管理工具,選擇Unity Registry分類,找到Android Logcat插件,導入即可。
- 插件使用
? ? ? ? 打開插件:Window -> Analysis -> Android Logcat,手機開啟調試模式后,使用數據線連接電腦,在該插件面板默認會自動選擇手機并輸出日志(需手機先運行程序,每次運行后,每次選擇)。可在頂部選擇要調試程序的包名,輸出的日志更整潔。
- 亂碼解決
? ? ? ? 找到插件源碼:Editor/AndroidLogcatMessageProvider.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?
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;...}
此時,日志就能夠正常顯示中了。?