gen*_*ray 9 c# java xml android unity-game-engine
我目前正在尝试使用Unity3D为Android编写一个小游戏。因为我想要一个可见的状态栏,所以我在Project文件夹(C:\ Users \ Public \ Documents \ Unity Projects \ ProjectName \ Temp \ StagingArea)中修改了AndroidManifest,如下所示:
<application android:theme="Theme.Light.NoTitleBar" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
Run Code Online (Sandbox Code Playgroud)
但是每次我生成APK时,Unity都会将Android Manifest更改为:
<application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">
Run Code Online (Sandbox Code Playgroud)
Unity3D是否每次都会更改修改过的清单?
Pro*_*mer 14
您正在修改错误的AndroidManifest文件。那AndroidManifest从<ProjectName>\Temp\StagingArea你正在修改通过统一每次生成项目时生成。
要使用自定义AndroidManifest文件,您必须将自定义AndroidManifest文件放在<ProjectName>Assets\Plugins\Android。
1.转到<UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk,将AndroidManifest.xml文件复制到<ProjectName>Assets\Plugins\Android
2.从中打开复制的清单文件<ProjectName>Assets\Plugins\Android 并添加清单。
在您的特定情况下,添加<application android:theme="Theme.Light.NoTitleBar" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="false" android:isGame="true" android:banner="@drawable/app_banner">到它。保存,构建和运行。
现在,Unity将使用该AndroidManifest文件。如果您遇到崩溃或任何其他问题,则Unity不希望您更改它。
Starting with Unity 2018 you should implement the IPostGenerateGradleAndroidProject interface which will allow you to edit the android manifest after Unity produces it. In the code below I have added methods to set the microphone permission, set hardware acceleration, and set your application theme ( replace SetMicrophonePermission() to your preferred method, which is called when Unity calls OnPostGenerateGradleAndroidProject()).
Add the following code to Assets/Editor/ModifyUnityAndroidAppManifestSample.cs
using System.IO;
using System.Text;
using System.Xml;
using UnityEditor.Android;
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.SetMicrophonePermission();
// 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 void SetApplicationTheme(string appTheme)
{
ApplicationElement.Attributes.Append(CreateAndroidAttribute("theme", appTheme));
}
internal void SetStartingActivityName(string activityName)
{
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("name", activityName));
}
internal void SetHardwareAcceleration()
{
GetActivityWithLaunchIntent().Attributes.Append(CreateAndroidAttribute("hardwareAccelerated", "true"));
}
internal void SetMicrophonePermission()
{
var manifest = SelectSingleNode("/manifest");
XmlElement child = CreateElement("uses-permission");
manifest.AppendChild(child);
XmlAttribute newAttribute = CreateAndroidAttribute("name", "android.permission.RECORD_AUDIO");
child.Attributes.Append(newAttribute);
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
13010 次 |
| 最近记录: |