romanilyinfb

ModifyUnityAndroidAppManifestSample.cs

Jan 13th, 2022
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.77 KB | None | 0 0
  1. using System.IO;
  2. using System.Text;
  3. using System.Xml;
  4. using UnityEditor.Android;
  5.  
  6. public class ModifyUnityAndroidAppManifestSample : IPostGenerateGradleAndroidProject
  7. {
  8.  
  9.     public void OnPostGenerateGradleAndroidProject(string basePath)
  10.     {
  11.         // If needed, add condition checks on whether you need to run the modification routine.
  12.         // For example, specific configuration/app options enabled
  13.  
  14.         var androidManifest = new AndroidManifest(GetManifestPath(basePath));
  15.  
  16.         androidManifest.SetMicrophonePermission();
  17.  
  18.         // Add your XML manipulation routines
  19.  
  20.         androidManifest.Save();
  21.     }
  22.  
  23.     public int callbackOrder { get { return 1; } }
  24.  
  25.     private string _manifestFilePath;
  26.  
  27.     private string GetManifestPath(string basePath)
  28.     {
  29.         if (string.IsNullOrEmpty(_manifestFilePath))
  30.         {
  31.             var pathBuilder = new StringBuilder(basePath);
  32.             pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
  33.             pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
  34.             pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
  35.             _manifestFilePath = pathBuilder.ToString();
  36.         }
  37.         return _manifestFilePath;
  38.     }
  39. }
  40.  
  41.  
  42. internal class AndroidXmlDocument : XmlDocument
  43. {
  44.     private string m_Path;
  45.     protected XmlNamespaceManager nsMgr;
  46.     public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
  47.     public AndroidXmlDocument(string path)
  48.     {
  49.         m_Path = path;
  50.         using (var reader = new XmlTextReader(m_Path))
  51.         {
  52.             reader.Read();
  53.             Load(reader);
  54.         }
  55.         nsMgr = new XmlNamespaceManager(NameTable);
  56.         nsMgr.AddNamespace("android", AndroidXmlNamespace);
  57.     }
  58.  
  59.     public string Save()
  60.     {
  61.         return SaveAs(m_Path);
  62.     }
  63.  
  64.     public string SaveAs(string path)
  65.     {
  66.         using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
  67.         {
  68.             writer.Formatting = Formatting.Indented;
  69.             Save(writer);
  70.         }
  71.         return path;
  72.     }
  73. }
  74.  
  75.  
  76. internal class AndroidManifest : AndroidXmlDocument
  77. {
  78.     private readonly XmlElement ApplicationElement;
  79.  
  80.     public AndroidManifest(string path) : base(path)
  81.     {
  82.         ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
  83.     }
  84.  
  85.     private XmlAttribute CreateAndroidAttribute(string key, string value)
  86.     {
  87.         XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
  88.         attr.Value = value;
  89.         return attr;
  90.     }
  91.  
  92.     internal XmlNode GetActivityWithLaunchIntent()
  93.     {
  94.         return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
  95.                 "intent-filter/category/@android:name='android.intent.category.LAUNCHER']", nsMgr);
  96.     }
  97.  
  98.     internal void SetApplicationTheme(string appTheme)
  99.     {
  100.         ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
  101.     }
  102.  
  103.     internal void SetStartingActivityName(string activityName)
  104.     {
  105.         GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
  106.     }
  107.  
  108.  
  109.     internal void SetHardwareAcceleration()
  110.     {
  111.         GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
  112.     }
  113.  
  114.     internal void SetMicrophonePermission()
  115.     {
  116.         var manifest = SelectSingleNode("/manifest");
  117.         XmlElement child = CreateElement("uses-permission");
  118.         manifest.AppendChild(child);
  119.         XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.RECORD_AUDIO");
  120.         child.Attributes.Append(newAttribute);
  121.     }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment