CmdSetScriptingDefine.cs 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using System.Collections.Generic;
  2. using UnityEditor;
  3. using UnityEngine;
  4. using XGame.Framework;
  5. namespace XGame.Editor.Build
  6. {
  7. [BuildCommand((uint)BuildCommandPriority.SetScriptingDefine)]
  8. class CmdSetScriptingDefine : BaseBuildCommand, ICommandExecuterAsync
  9. {
  10. public void Execute()
  11. {
  12. BuildLog.Log("CmdSetScriptingDefine Execute.");
  13. if (!Context.config.isSetScriptDefine)
  14. {
  15. //不设置宏
  16. this.Completed();
  17. return;
  18. }
  19. var targetGroup = Context.config.target.ToTargetGroup();
  20. // 预编译宏<宏名字,是否增加>
  21. var macroMap = new Dictionary<string, bool>();
  22. var publishType = Context.config.publishType;
  23. macroMap.Add(MacroDefine.DEBUG, publishType == PublishType.Debug);
  24. macroMap.Add(MacroDefine.PROFILER, publishType == PublishType.Profiler);
  25. macroMap.Add(MacroDefine.FINAL_RELEASE, publishType != PublishType.Debug);
  26. //UPR开关宏
  27. macroMap.Add("NO_PACKAGE", publishType != PublishType.Profiler);
  28. var sdList = new List<string>();
  29. //先取PlaySettings的预编译宏
  30. var lastSD = PlayerSettings.GetScriptingDefineSymbolsForGroup(targetGroup);
  31. if (!string.IsNullOrEmpty(lastSD))
  32. {
  33. var sds = lastSD.Split(';');
  34. foreach (var sd in sds)
  35. {
  36. if (!string.IsNullOrEmpty(sd))
  37. {
  38. sdList.Add(sd);
  39. }
  40. }
  41. }
  42. //根据发布版本增/减预编译宏
  43. var change = false;
  44. foreach (var macro in macroMap)
  45. {
  46. var sd = macro.Key;
  47. var isAdd = macro.Value;
  48. if (sdList.Contains(sd))
  49. {
  50. if (!isAdd)
  51. {
  52. sdList.Remove(sd);
  53. change = true;
  54. }
  55. }
  56. else
  57. {
  58. if (isAdd)
  59. {
  60. sdList.Add(sd);
  61. change = true;
  62. }
  63. }
  64. }
  65. if (change)
  66. {
  67. BuildCompileEvent.RegistEvent(Context);
  68. var nextSD = string.Join(";", sdList);
  69. BuildLog.Log($"SetScriptingDefine: {nextSD}");
  70. PlayerSettings.SetScriptingDefineSymbolsForGroup(targetGroup, nextSD);
  71. return;
  72. //AssetDatabase.SaveAssets();
  73. //AssetDatabase.Refresh();
  74. //打包机没启动编辑器界面,不会主动保存配置,强制重新编译脚本以保存配置
  75. //UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
  76. }
  77. this.Completed();
  78. }
  79. }
  80. }