我目前正在尝试使用Unity3D为Android编写一个小游戏。因为我想要一个可见的状态栏,我修改了项目文件夹中的AndroidManifest(C:\ Users \ Public \ Documents \ Unity Projects \ ProjectName \ Temp \ StagingArea),如下所示:
<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">
但每次我生成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">
Unity3D每次都会更改我修改过的Manifests吗?
答案 0 :(得分:4)
您正在修改错误的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不希望你改变它。
答案 1 :(得分:3)
从Unity 2018开始,您应该实现IPostGenerateGradleAndroidProject
interface,这将允许您在Unity生成Android清单后对其进行编辑。在下面的代码中,我添加了设置麦克风许可权,设置硬件加速和设置应用程序主题的方法(将SetMicrophonePermission()
替换为首选方法,当Unity调用OnPostGenerateGradleAndroidProject()
时将调用该方法)。>
将以下代码添加到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);
}
}
答案 2 :(得分:0)
对于Unity运行时权限,我编写了此函数来添加跳过权限对话框(例如https://docs.unity3d.com/Manual/android-manifest.html)
例如
<manifest>
<application>
<meta-data android:name="unityplayer.SkipPermissionsDialog" android:value="true" />
这是代码:
internal void SetSkipPermissionsDialog()
{
var manifest = SelectSingleNode("/manifest");
var application = manifest.SelectSingleNode("application");
XmlElement child = CreateElement("meta-data");
application.AppendChild(child);
XmlAttribute newAttribute = CreateAndroidAttribute("name", "unityplayer.SkipPermissionsDialog");
child.Attributes.Append(newAttribute);
newAttribute = CreateAndroidAttribute("value", "true");
child.Attributes.Append(newAttribute);
}
答案 3 :(得分:0)
要使用自定义AndroidManifest文件,
您必须将自定义的 AndroidManifest 文件放在<ProjectName>Assets\Plugins\Android