ModifyUnityAndroidAppManifestSample.cs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. using System.IO;
  2. using System.Text;
  3. using System.Xml;
  4. using UnityEditor.Android;
  5. using UnityEngine;
  6. namespace Rokid.UXR.Editor {
  7. public class ModifyUnityAndroidAppManifestSample : IPostGenerateGradleAndroidProject
  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. var androidManifest = new AndroidManifest(GetManifestPath(basePath));
  14. androidManifest.SetHardwareAccel();
  15. // Add your XML manipulation routines
  16. androidManifest.Save();
  17. }
  18. public int callbackOrder { get { return 1; } }
  19. private string _manifestFilePath;
  20. private string GetManifestPath(string basePath)
  21. {
  22. if (string.IsNullOrEmpty(_manifestFilePath))
  23. {
  24. var pathBuilder = new StringBuilder(basePath);
  25. pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
  26. pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
  27. pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
  28. _manifestFilePath = pathBuilder.ToString();
  29. }
  30. return _manifestFilePath;
  31. }
  32. }
  33. internal class AndroidXmlDocument : XmlDocument
  34. {
  35. private string m_Path;
  36. protected XmlNamespaceManager nsMgr;
  37. public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
  38. public AndroidXmlDocument(string path)
  39. {
  40. m_Path = path;
  41. using (var reader = new XmlTextReader(m_Path))
  42. {
  43. reader.Read();
  44. Load(reader);
  45. }
  46. nsMgr = new XmlNamespaceManager(NameTable);
  47. nsMgr.AddNamespace("android", AndroidXmlNamespace);
  48. }
  49. public string Save()
  50. {
  51. return SaveAs(m_Path);
  52. }
  53. public string SaveAs(string path)
  54. {
  55. using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
  56. {
  57. writer.Formatting = Formatting.Indented;
  58. Save(writer);
  59. }
  60. return path;
  61. }
  62. }
  63. internal class AndroidManifest : AndroidXmlDocument
  64. {
  65. private readonly XmlElement ApplicationElement;
  66. public AndroidManifest(string path) : base(path)
  67. {
  68. ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
  69. }
  70. private XmlAttribute CreateAndroidAttribute(string key, string value)
  71. {
  72. XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
  73. attr.Value = value;
  74. return attr;
  75. }
  76. internal XmlNode GetActivityWithLaunchIntent()
  77. {
  78. return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
  79. "intent-filter/category/@android:name='android.intent.category.LAUNCHER']", nsMgr);
  80. }
  81. internal XmlNode GetActivityWithUXR()
  82. {
  83. return SelectSingleNode("/manifest/application/activity[meta-data/@android:name='unityplayer.UnityActivity'] ", nsMgr);
  84. }
  85. internal void SetApplicationTheme(string appTheme)
  86. {
  87. ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
  88. }
  89. internal void SetStartingActivityName(string activityName)
  90. {
  91. GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
  92. }
  93. internal void SetHardwareAccel()
  94. {
  95. GetActivityWithUXR().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
  96. }
  97. }
  98. }