123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117 |
- using System.IO;
- using System.Text;
- using System.Xml;
- using UnityEditor.Android;
- using UnityEngine;
- namespace Rokid.UXR.Editor {
- public class ModifyUnityAndroidAppManifestSample : IPostGenerateGradleAndroidProject
- {
-
- public void OnPostGenerateGradleAndroidProject(string basePath)
- {
- // If needed, add condition checks on whether you need to run the modification routine.
- // For example, specific configuration/app options enabled
- var androidManifest = new AndroidManifest(GetManifestPath(basePath));
-
- androidManifest.SetHardwareAccel();
- // Add your XML manipulation routines
- androidManifest.Save();
- }
-
- public int callbackOrder { get { return 1; } }
-
- private string _manifestFilePath;
-
- private string GetManifestPath(string basePath)
- {
- if (string.IsNullOrEmpty(_manifestFilePath))
- {
- var pathBuilder = new StringBuilder(basePath);
- pathBuilder.Append(Path.DirectorySeparatorChar).Append("src");
- pathBuilder.Append(Path.DirectorySeparatorChar).Append("main");
- pathBuilder.Append(Path.DirectorySeparatorChar).Append("AndroidManifest.xml");
- _manifestFilePath = pathBuilder.ToString();
- }
- return _manifestFilePath;
- }
- }
-
-
- internal class AndroidXmlDocument : XmlDocument
- {
- private string m_Path;
- protected XmlNamespaceManager nsMgr;
- public readonly string AndroidXmlNamespace = "http://schemas.android.com/apk/res/android";
-
- public AndroidXmlDocument(string path)
- {
- m_Path = path;
- using (var reader = new XmlTextReader(m_Path))
- {
- reader.Read();
- Load(reader);
- }
- nsMgr = new XmlNamespaceManager(NameTable);
- nsMgr.AddNamespace("android", AndroidXmlNamespace);
- }
-
- public string Save()
- {
- return SaveAs(m_Path);
- }
-
- public string SaveAs(string path)
- {
- using (var writer = new XmlTextWriter(path, new UTF8Encoding(false)))
- {
- writer.Formatting = Formatting.Indented;
- Save(writer);
- }
- return path;
- }
- }
-
-
- internal class AndroidManifest : AndroidXmlDocument
- {
- private readonly XmlElement ApplicationElement;
-
- public AndroidManifest(string path) : base(path)
- {
- ApplicationElement = SelectSingleNode("/manifest/application") as XmlElement;
- }
-
- private XmlAttribute CreateAndroidAttribute(string key, string value)
- {
- XmlAttribute attr = CreateAttribute("android", key, AndroidXmlNamespace);
- attr.Value = value;
- return attr;
- }
-
- internal XmlNode GetActivityWithLaunchIntent()
- {
- return SelectSingleNode("/manifest/application/activity[intent-filter/action/@android:name='android.intent.action.MAIN' and " +
- "intent-filter/category/@android:name='android.intent.category.LAUNCHER']", nsMgr);
- }
-
- internal XmlNode GetActivityWithUXR()
- {
- return SelectSingleNode("/manifest/application/activity[meta-data/@android:name='unityplayer.UnityActivity'] ", nsMgr);
- }
-
- internal void SetApplicationTheme(string appTheme)
- {
- ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
- }
-
- internal void SetStartingActivityName(string activityName)
- {
- GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
- }
- internal void SetHardwareAccel()
- {
- GetActivityWithUXR().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
- }
- }
- }
|