【发布时间】:2016-03-29 15:42:18
【问题描述】:
在 Unity 中为 Android 构建时如何禁用平板电脑或大屏幕支持?
我在播放器设置中看不到任何选项。
另外,我也看到过这个问题:Android: Disable to install app on tablets
我正在使用 Unity 构建我的 APK,所以我不知道如何编辑我的 AndroidManifest.xml 文件。
【问题讨论】:
在 Unity 中为 Android 构建时如何禁用平板电脑或大屏幕支持?
我在播放器设置中看不到任何选项。
另外,我也看到过这个问题:Android: Disable to install app on tablets
我正在使用 Unity 构建我的 APK,所以我不知道如何编辑我的 AndroidManifest.xml 文件。
【问题讨论】:
您可以通过将一个自定义 AndroidManifest.xml 放在 Assets/Plugins/Android 文件夹中来提供一个供 Unity 使用的自定义 AndroidManifest.xml。
如果您进行 Android 构建,则此文件的基本副本位于 Temp/StagingArea 中,您可以将其用作创建自定义版本的起点。请注意,当您退出 Unity 时,此目录会被删除!
【讨论】:
关于此的另一个更新:
我在 Android 发布设置中发现了这个小复选框,以使用您自己的 LauncherManifest.xml。合并所有清单时,只需使用替换工具来提高优先级。截图说明了一切:
由于这个答案很老了,这里有一点更新:
自 Unity 2019.3 起,support-screens 节点位于 LauncherManifest.xml 中,其属性不能被自定义属性覆盖。您会遇到合并冲突。
看看基础知识: https://docs.unity3d.com/Manual/android-manifest.html
那么,我做了什么: 临时构建文件夹中有 10 多个 mainfest xml 文件,很难弄清楚要操作哪一个才能在最终的 AndroidManifest.xml 中获得正确的设置。所以我编写了一个小脚本,它使用 IPostGenerateGradleAndroidProject 接口并更改所有适当的节点。然后我删除了不必要的东西,结果如下:
using System.Collections.Generic;
using System.IO;
using UnityEditor.Android;
using UnityEngine;
public class ChangeSupportedScreens : IPostGenerateGradleAndroidProject
{
public int callbackOrder { get { return 0; } }
private List<string> _androidManifestPaths = new List<string>
{
"Temp/gradleOut/launcher/src/main",
"Temp/gradleOut/unityLibrary/src/main",
};
private const string ANDROID_MANIFEST_FILENAME = "AndroidManifest.xml";
public void OnPostGenerateGradleAndroidProject(string _)
{
foreach (var manifestPath in _androidManifestPaths)
{
var androidManifestPath = GetAndroidManifestPath(manifestPath);
if (File.Exists(androidManifestPath))
{
var androidManifestXML = File.ReadAllText(androidManifestPath);
androidManifestXML = ReplaceValue(androidManifestXML, "largeScreens", "true", "false");
androidManifestXML = ReplaceValue(androidManifestXML, "xlargeScreens", "true", "false");
File.WriteAllText(androidManifestPath, androidManifestXML);
}
else
{
Debug.LogError($"Error: File {androidManifestPath} not found");
}
}
}
private static string ReplaceValue(string content, string key, string oldValue, string newValue)
=> content.Contains(key) ? content.Replace($"{key}=\"{oldValue}\"", $"{key}=\"{newValue}\"") : content;
private static string GetAndroidManifestPath(string path)
{
var separator = Path.DirectorySeparatorChar;
var manifestPath = path.Replace('/', separator);
return Path.GetFullPath(Path.Combine(Application.streamingAssetsPath, $"..{separator}..{separator}{manifestPath}", ANDROID_MANIFEST_FILENAME));
}
}
希望这会有所帮助。我在 apktool 的帮助下仔细检查了结果,以查看打包的 APK AndroidManifest.xml 中发生了什么。 当然,如果常用方法(在 Plugins/Android 中使用自己的 AndroidManifest.xml 不适合您),您可以使用此片段覆盖其他值。
免责声明:
【讨论】: