CmdSetPlayerSettings.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace XGame.Editor.Build
  4. {
  5. [BuildCommand((uint)BuildCommandPriority.SetPlayerSettings)]
  6. internal class CmdSetPlayerSettings : BaseBuildCommand, ICommandExecuterAsync
  7. {
  8. public void Execute()
  9. {
  10. BuildLog.Log("CmdSetPlayerSettings Execute.");
  11. var config = Context.config;
  12. PlayerSettings.productName = config.project.productName;
  13. PlayerSettings.applicationIdentifier = config.project.applicationIdentifier;
  14. PlayerSettings.bundleVersion = Context.version.ToString();
  15. SetVersionCode(config.target, config.project.versionCode);
  16. EditorUserBuildSettings.exportAsGoogleAndroidProject = config.android.exportAsGoogleAndroidProject;
  17. EditorUserBuildSettings.androidCreateSymbols = (AndroidCreateSymbols)config.androidCreateSymbols;
  18. //release
  19. var isDebug = config.publishType == PublishType.Debug;
  20. EditorUserBuildSettings.development = config.publishType != PublishType.Release;
  21. EditorUserBuildSettings.connectProfiler = config.publishType == PublishType.Profiler;
  22. EditorUserBuildSettings.allowDebugging = config.publishType == PublishType.Profiler;
  23. //StackTrace
  24. //None: 不向日志输出堆栈跟踪。
  25. //ScriptOnly: 仅输出托管堆栈跟踪。
  26. //Full: 将记录原生和托管堆栈跟踪。
  27. PlayerSettings.SetStackTraceLogType(LogType.Log, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
  28. PlayerSettings.SetStackTraceLogType(LogType.Assert, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
  29. PlayerSettings.SetStackTraceLogType(LogType.Warning, isDebug ? StackTraceLogType.ScriptOnly : StackTraceLogType.None);
  30. PlayerSettings.SetStackTraceLogType(LogType.Error, StackTraceLogType.ScriptOnly);
  31. PlayerSettings.SetStackTraceLogType(LogType.Exception, StackTraceLogType.Full);
  32. var group = Context.config.target.ToTargetGroup();
  33. var scriptImpl = Context.config.scriptImpl;
  34. PlayerSettings.SetIl2CppCompilerConfiguration(group, isDebug ? Il2CppCompilerConfiguration.Debug : Il2CppCompilerConfiguration.Release);
  35. if (group != BuildTargetGroup.iOS)
  36. { //iOS固定为IL2CPP,并且不用设置
  37. PlayerSettings.SetScriptingBackend(group, scriptImpl.ToUnityImpl());
  38. }
  39. if (group == BuildTargetGroup.Android)
  40. {
  41. var architecture = Context.config.android.architecture;
  42. if (architecture == AndroidArchitecture.None) architecture = AndroidArchitecture.ARMv7;
  43. PlayerSettings.Android.targetArchitectures = architecture.ToUnityImpl();
  44. }
  45. else if (group == BuildTargetGroup.iOS)
  46. {
  47. PlayerSettings.SetArchitecture(group, (int)AppleMobileArchitecture.ARM64);
  48. }
  49. var targetLevel = UnityEditor.ApiCompatibilityLevel.NET_4_6;
  50. if (targetLevel == PlayerSettings.GetApiCompatibilityLevel(group))
  51. {
  52. //AssetDatabase.SaveAssets();
  53. //AssetDatabase.Refresh();
  54. //打包机没启动编辑器界面,不会主动保存配置,强制重新编译脚本以保存配置
  55. //UnityEditor.Compilation.CompilationPipeline.RequestScriptCompilation();
  56. this.Completed();
  57. }
  58. else
  59. {
  60. BuildCompileEvent.RegistEvent(Context);
  61. BuildLog.Log($"PlayerSettings SetApiCompatibilityLevel. Level:{targetLevel}");
  62. PlayerSettings.SetApiCompatibilityLevel(group, targetLevel);
  63. }
  64. }
  65. private void SetVersionCode(BuildTarget target, string versionCode)
  66. {
  67. switch (target)
  68. {
  69. case BuildTarget.Android:
  70. int.TryParse(versionCode, out var code);
  71. PlayerSettings.Android.bundleVersionCode = code;
  72. break;
  73. case BuildTarget.iOS:
  74. PlayerSettings.iOS.buildNumber = versionCode;
  75. break;
  76. }
  77. }
  78. }
  79. }