当前位置:   article > 正文

[C#] 利用部分方法添加调试代码(附录)_allowdebugging mono

allowdebugging mono

项目生成时屏蔽Debug代码的脚本:

  1. using System;
  2. using System.IO;
  3. using UnityEditor;
  4. using UnityEngine;
  5. public class BuildPlayer : ScriptableObject
  6. {
  7. static string rootPath = Application.dataPath + Path.DirectorySeparatorChar;
  8. static string rootParent = new DirectoryInfo(rootPath).Parent.FullName + Path.DirectorySeparatorChar;
  9. static string projectName = new DirectoryInfo(rootPath).Parent.Name;
  10. static string scenePath = Application.dataPath + Path.DirectorySeparatorChar + "Scenes" + Path.DirectorySeparatorChar;
  11. static string apkNameDebug = rootParent + projectName + "_Debug_[Date].apk";
  12. static string apkNameRelease = rootParent + projectName + "_Release_[Date].apk";
  13. [MenuItem("Build Player/Build Android Debug", false, 101)]
  14. static void BuildAndroidDebug()
  15. {
  16. AssetDatabase.Refresh();
  17. // set build options and build player
  18. BuildOptions options = BuildOptions.Development | BuildOptions.AllowDebugging | BuildOptions.ShowBuiltPlayer;
  19. string[] allScenes = GetAllScenes();
  20. BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
  21. }
  22. [MenuItem("Build Player/Build Android Debug(Auto Deploy)", false, 102)]
  23. static void BuildAndroidDebugAutoDeploy()
  24. {
  25. AssetDatabase.Refresh();
  26. // set build options and build player
  27. BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.AllowDebugging;
  28. string[] allScenes = GetAllScenes();
  29. BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
  30. }
  31. [MenuItem("Build Player/Build Android Debug(Auto Deploy And Connect Profiler)", false, 103)]
  32. static void BuildAndroidDebugAutoDeployAndConnectProfiler()
  33. {
  34. AssetDatabase.Refresh();
  35. // set build options and build player
  36. BuildOptions options = BuildOptions.Development | BuildOptions.AutoRunPlayer | BuildOptions.ConnectWithProfiler | BuildOptions.AllowDebugging;
  37. string[] allScenes = GetAllScenes();
  38. BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameDebug), BuildTarget.Android, options);
  39. }
  40. [MenuItem("Build Player/Build Android Release", false, 201)]
  41. static void BuildAndroidRelease()
  42. {
  43. // rename all debug scripts to *.rem
  44. RenameDebugScripts();
  45. // set build options and build player
  46. BuildOptions options = BuildOptions.ShowBuiltPlayer;
  47. string[] allScenes = GetAllScenes();
  48. BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options);
  49. // restore all debug scripts' file names
  50. RestoreDebugScripts();
  51. }
  52. [MenuItem("Build Player/Build Android Release(Auto Deploy)", false, 202)]
  53. static void BuildAndroidReleaseAutoDeploy()
  54. {
  55. // rename all debug scripts to *.rem
  56. RenameDebugScripts();
  57. // set build options and build player
  58. BuildOptions options = BuildOptions.AutoRunPlayer;
  59. string[] allScenes = GetAllScenes();
  60. BuildPipeline.BuildPlayer(allScenes, InsertDateString(apkNameRelease), BuildTarget.Android, options);
  61. // restore all debug scripts' file names
  62. RestoreDebugScripts();
  63. }
  64. static void RenameDebugScripts()
  65. {
  66. string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories);
  67. foreach (string directory in allDebugDirectories)
  68. {
  69. string[] scripts = Directory.GetFiles(directory, "*.cs", SearchOption.AllDirectories);
  70. foreach (string script in scripts)
  71. {
  72. string newScriptName = script + ".rem";
  73. File.Move(script, newScriptName);
  74. File.Delete(script + ".meta");
  75. }
  76. }
  77. AssetDatabase.Refresh();
  78. }
  79. static void RestoreDebugScripts()
  80. {
  81. string[] allDebugDirectories = Directory.GetDirectories(rootPath, "_Debug", SearchOption.AllDirectories);
  82. foreach (string directory in allDebugDirectories)
  83. {
  84. string[] renamedScripts = Directory.GetFiles(directory, "*.rem", SearchOption.AllDirectories);
  85. foreach (string renamedScript in renamedScripts)
  86. {
  87. string scriptName = renamedScript.Substring(0, renamedScript.Length - 3);
  88. File.Move(renamedScript, scriptName);
  89. File.Delete(renamedScript + ".meta");
  90. }
  91. }
  92. AssetDatabase.Refresh();
  93. }
  94. static string[] GetAllScenes()
  95. {
  96. string[] allScenes = null;
  97. DirectoryInfo dirInfo = new DirectoryInfo(scenePath);
  98. FileInfo[] fileInfos = dirInfo.GetFiles("*.unity", SearchOption.AllDirectories);
  99. allScenes = new string[fileInfos.Length];
  100. int index = 0;
  101. foreach (FileInfo fileInfo in fileInfos)
  102. {
  103. string scene = fileInfo.FullName.Replace(rootParent, "");
  104. allScenes[index++] = scene;
  105. }
  106. return allScenes;
  107. }
  108. static string InsertDateString(string apkName)
  109. {
  110. DateTime now = DateTime.Now;
  111. 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));
  112. }
  113. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/118857
推荐阅读
相关标签
  

闽ICP备14008679号