SvrBuild.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using UnityEngine;
  2. using UnityEditor;
  3. using System.IO;
  4. using System.Collections.Generic;
  5. using System.Text.RegularExpressions;
  6. public class SvrBuild : MonoBehaviour
  7. {
  8. static void BuildScene(string[] scenes, string apkDir, string apkName)
  9. {
  10. Directory.CreateDirectory(apkDir);
  11. if(System.IO.File.Exists(apkDir + apkName))
  12. {
  13. System.IO.File.SetAttributes(apkDir + apkName, System.IO.File.GetAttributes(apkDir + apkName) & ~FileAttributes.ReadOnly);
  14. }
  15. BuildPipeline.BuildPlayer(scenes, apkDir + apkName, BuildTarget.Android, BuildOptions.None);
  16. }
  17. //
  18. //[MenuItem("SVR/Build Project/Landscape Left")]
  19. static void BuildProjectLL()
  20. {
  21. BuildProjectTarget(UIOrientation.LandscapeLeft, "-ll.apk");
  22. }
  23. //[MenuItem("SVR/Build Project/Landscape Right")]
  24. static void BuildProjectLR()
  25. {
  26. BuildProjectTarget(UIOrientation.LandscapeRight, "-lr.apk");
  27. }
  28. //[MenuItem("SVR/Build Project/Landscape All")]
  29. static void BuildProject()
  30. {
  31. BuildProjectTarget(UIOrientation.LandscapeLeft, "-ll.apk");
  32. BuildProjectTarget(UIOrientation.LandscapeRight, "-lr.apk");
  33. }
  34. static void BuildProjectTarget(UIOrientation landscapeOrientation, string landscapeSuffix)
  35. {
  36. try
  37. {
  38. # if UNITY_2018
  39. PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevelAuto;
  40. PlayerSettings.Android.targetArchitectures = AndroidArchitecture.All; // ARM64
  41. PlayerSettings.SetScriptingBackend(BuildTargetGroup.Android, ScriptingImplementation.IL2CPP);
  42. # elif UNITY_2017
  43. PlayerSettings.Android.targetSdkVersion = AndroidSdkVersions.AndroidApiLevel25;
  44. # else // UNITY_5
  45. # endif
  46. //Debug.Log("Bulding Project!");
  47. # if UNITY_5
  48. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTarget.Android);
  49. # else
  50. EditorUserBuildSettings.SwitchActiveBuildTarget(BuildTargetGroup.Android, BuildTarget.Android);
  51. # endif
  52. {
  53. string apkDir = "./Build/Android/";
  54. string apkName = PlayerSettings.productName.Replace(" ", "_");
  55. List<string> scenes = new List<string>();
  56. if( EditorBuildSettings.scenes != null )
  57. {
  58. for(int i=0;i<EditorBuildSettings.scenes.Length;i++)
  59. {
  60. if( EditorBuildSettings.scenes[i].enabled )
  61. {
  62. scenes.Add(EditorBuildSettings.scenes[i].path);
  63. }
  64. }
  65. }
  66. //Save off current orientation
  67. UIOrientation currentOrientation = PlayerSettings.defaultInterfaceOrientation;
  68. //Build target
  69. PlayerSettings.defaultInterfaceOrientation = landscapeOrientation;
  70. BuildScene(scenes.ToArray(), apkDir, apkName+landscapeSuffix);
  71. //revert back
  72. PlayerSettings.defaultInterfaceOrientation = currentOrientation;
  73. }
  74. }
  75. catch (IOException e)
  76. {
  77. Debug.LogError( e.Message );
  78. }
  79. }
  80. }