[C#] 利用部分方法添加调试代码(附录)
项目生成时屏蔽Debug代码的脚本: using System; using System.IO; using UnityEditor; using UnityEngine; public class BuildPlayer : ScriptableObject { static string rootPath = Application.dataPath + Path.DirectorySeparatorChar; static string rootParent = new DirectoryInfo(rootPath).Parent.FullName + Path.DirectorySeparatorChar; static string projectName = new DirectoryInfo(rootPath).Parent.Name; static string scenePath = Application.dataPath + Path.DirectorySeparatorChar + "Scenes" + Path.DirectorySeparatorChar; static string apkNameDebug = rootParent + projectName + "_Debug_[Date].apk"; static string apkNameRelease = rootParent + projectName + "_Release_[Date].apk"; [MenuItem("Build Player/Build Android Debug", false, 101)] static void BuildAndroidDebug() { AssetDatabase.Refresh(); // set build options and build player BuildOptions options = BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ShowBuiltPlayer; string[] allScenes = GetAllScenes(); BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options); } [MenuItem("Build Player/Build Android Debug(Auto Deploy)", false, 102)] static void BuildAndroidDebugAutoDeploy() { AssetDatabase.Refresh(); // set build options and build player BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.AllowDebugging; string[] allScenes = GetAllScenes(); BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options); } [MenuItem("Build Player/Build Android Debug(Auto Deploy And Connect Profiler)", false, 103)] static void BuildAndroidDebugAutoDeployAndConnectProfiler() { AssetDatabase.Refresh(); // set build options and build player BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging; string[] allScenes = GetAllScenes(); BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options); } [MenuItem("Build Player/Build Android Release", false, 201)] static void BuildAndroidRelease() { // rename all debug scripts to *.rem RenameDebugScripts(); // set build options and build player BuildOptions options = BuildOptions.ShowBuiltPlayer; string[] allScenes = GetAllScenes(); BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options); // restore all debug scripts' file names RestoreDebugScripts(); } [MenuItem("Build Player/Build Android Release(Auto Deploy)", false, 202)] static void BuildAndroidReleaseAutoDeploy() { // rename all debug scripts to *.rem RenameDebugScripts(); // set build options and build player BuildOptions options = BuildOptions.AutoRunPlayer; string[] allScenes = GetAllScenes(); BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options); // restore all debug scripts' file names RestoreDebugScripts(); } static void RenameDebugScripts() { string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories); foreach (string directory in allDebugDirectories) { string[] scripts = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories); foreach (string script in scripts) { string newScriptName = script + ".rem"; File.Move(script, newScriptName); File.Delete(script + ".meta"); } } AssetDatabase.Refresh(); } static void RestoreDebugScripts() { string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories); foreach (string directory in allDebugDirectories) { string[] renamedScripts = Directory.GetFiles(directory, "*.rem", SearchOption.AllDirectories); foreach (string renamedScript in renamedScripts) { string scriptName = renamedScript.Substring(0, renamedScript.Length - 3); File.Move(renamedScript, scriptName); File.Delete(renamedScript + ".meta"); } } AssetDatabase.Refresh(); } static string[] GetAllScenes() { string[] allScenes = null; DirectoryInfo dirInfo = new DirectoryInfo(scenePath); FileInfo[] fileInfos = dirInfo.GetFiles("*.unity", SearchOption.AllDirectories); allScenes = new string[fileInfos.Length]; int index = 0; foreach (FileInfo fileInfo in fileInfos) { string scene = fileInfo.FullName.Replace(rootParent, ""); allScenes[index++] = scene; } return allScenes; } static string InsertDateString(string apkName) { DateTime now = DateTime.Now; return apkName.Replace("[Date]", string.Format("[{0:d4}{1:d2}{2:d2}][{3:d2}{4:d2}{5:d2}]", now.Year, now.Month, now.Day, now.Hour, now.Minute, now.Second)); } }
补充:软件开发 , C# ,