OVRExpansionFileGenerator.cs 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. using System;
  2. using System.IO;
  3. using System.Xml;
  4. using UnityEngine;
  5. using UnityEditor;
  6. public class BuildAssetBundles : MonoBehaviour
  7. {
  8. [MenuItem("Oculus/Tools/Build Mobile-Quest Expansion File", false, 100000)]
  9. public static void BuildBundles()
  10. {
  11. // Create expansion file directory and call build asset bundles
  12. string path = Application.dataPath + "/../Asset Bundles/";
  13. if (!System.IO.Directory.Exists(path))
  14. {
  15. System.IO.Directory.CreateDirectory(path);
  16. }
  17. BuildPipeline.BuildAssetBundles(path, BuildAssetBundleOptions.ChunkBasedCompression, BuildTarget.Android);
  18. // Rename asset bundle file to the proper obb string
  19. if (File.Exists(path + "Asset Bundles"))
  20. {
  21. string expansionName = "main." + PlayerSettings.Android.bundleVersionCode + "." + PlayerSettings.applicationIdentifier + ".obb";
  22. try
  23. {
  24. if (File.Exists(path + expansionName))
  25. {
  26. File.Delete(path + expansionName);
  27. }
  28. File.Move(path + "Asset Bundles", path + expansionName);
  29. UnityEngine.Debug.Log("OBB expansion file " + expansionName + " has been successfully created at " + path);
  30. UpdateAndroidManifest();
  31. }
  32. catch (Exception e)
  33. {
  34. UnityEngine.Debug.LogError(e.Message);
  35. }
  36. }
  37. }
  38. public static void UpdateAndroidManifest()
  39. {
  40. string manifestFolder = Application.dataPath + "/Plugins/Android";
  41. try
  42. {
  43. // Load android manfiest file
  44. XmlDocument doc = new XmlDocument();
  45. doc.Load(manifestFolder + "/AndroidManifest.xml");
  46. string androidNamepsaceURI;
  47. XmlElement element = (XmlElement)doc.SelectSingleNode("/manifest");
  48. if(element == null)
  49. {
  50. UnityEngine.Debug.LogError("Could not find manifest tag in android manifest.");
  51. return;
  52. }
  53. // Get android namespace URI from the manifest
  54. androidNamepsaceURI = element.GetAttribute("xmlns:android");
  55. if (!string.IsNullOrEmpty(androidNamepsaceURI))
  56. {
  57. // Check if the android manifest already has the read external storage permission
  58. XmlNodeList nodeList = doc.SelectNodes("/manifest/application/uses-permission");
  59. foreach (XmlElement e in nodeList)
  60. {
  61. string attr = e.GetAttribute("name", androidNamepsaceURI);
  62. if (attr == "android.permission.READ_EXTERNAL_STORAGE")
  63. {
  64. UnityEngine.Debug.Log("Android manifest already has the proper permissions.");
  65. return;
  66. }
  67. }
  68. element = (XmlElement)doc.SelectSingleNode("/manifest/application");
  69. if (element != null)
  70. {
  71. // Insert read external storage permission
  72. XmlElement newElement = doc.CreateElement("uses-permission");
  73. newElement.SetAttribute("name", androidNamepsaceURI, "android.permission.READ_EXTERNAL_STORAGE");
  74. element.AppendChild(newElement);
  75. doc.Save(manifestFolder + "/AndroidManifest.xml");
  76. UnityEngine.Debug.Log("Successfully modified android manifest with external storage permission.");
  77. return;
  78. }
  79. }
  80. UnityEngine.Debug.LogError("Could not find android naemspace URI in android manifest.");
  81. }
  82. catch (Exception e)
  83. {
  84. UnityEngine.Debug.LogError(e.Message);
  85. }
  86. }
  87. }