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