简体   繁体   English

在 Unity 中禁用平板电脑或大屏幕支持?

[英]Disable tablet or large screen support in Unity?

How can I disable tablet or large screen support when building for Android in Unity?在 Unity 中为 Android 构建时如何禁用平板电脑或大屏幕支持?

I don't see any options in Player settings.我在播放器设置中没有看到任何选项。

Also, I've seen this question: Android: Disable to install app on tablets另外,我看过这个问题: Android: Disable to install app on tablet

I'm building my APK with Unity so I don't know how to edit my AndroidManifest.xml file.我正在使用 Unity 构建我的 APK,所以我不知道如何编辑我的AndroidManifest.xml文件。

You can provide a custom AndroidManifest.xml for Unity to use by placing one in the Assets/Plugins/Android folder. 您可以通过在Assets / Plugins / Android文件夹中放置一个自定义AndroidManifest.xml来供Unity使用。

If you make an Android build, a basic copy of this file, which you can use as a starting point for creating a custom version, is located in Temp/StagingArea. 如果您进行的是Android构建,则此文件的基本副本位于Temp / StagingArea中,您可以将其用作创建自定义版本的起点。 Note that this directory gets deleted when you exit Unity! 请注意,当您退出Unity时,此目录将被删除!

Another update on this:关于此的另一个更新:

I found this little checkbox in the Android publish settings to use your own LauncherManifest.xml.我在 Android 发布设置中找到了这个小复选框以使用您自己的 LauncherManifest.xml。 Just use the replace tool to raise the priority when merging all manifests.合并所有清单时,只需使用替换工具来提高优先级。 The screenshots says it all:截图说明了一切:

在此处输入图片说明


Since this answer is quite old, here is a little update:由于这个答案很旧,这里有一个小更新:

Since Unity 2019.3, the support-screens node is located in LauncherManifest.xml and its properties cannot be overridden by the self-defined properties.从 Unity 2019.3 开始,support-screens 节点位于 LauncherManifest.xml 中,其属性不能被自定义属性覆盖。 You'll get merge conflicts.你会遇到合并冲突。

Have a look at the basics: https://docs.unity3d.com/Manual/android-manifest.html看看基础知识: https : //docs.unity3d.com/Manual/android-manifest.html

So, what I did: There are more than 10 mainfest xml files in the temporary build folder and it was quite difficult to figure out which one to manipulate to get the correct settings in the final AndroidManifest.xml.所以,我做了什么:在临时构建文件夹中有 10 多个 mainfest xml 文件,并且很难弄清楚要操作哪个才能在最终的 AndroidManifest.xml 中获得正确的设置。 So I wrote a small script that uses the IPostGenerateGradleAndroidProject interface and changes all the appropriate nodes.所以我写了一个小脚本,使用 IPostGenerateGradleAndroidProject 接口并更改所有适当的节点。 Then I removed unnecessary things and this is the result:然后我删除了不必要的东西,结果如下:

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));
    }
}

Hope this helps.希望这可以帮助。 I double checked the result with the help of apktool to see what's going in the packed APK AndroidManifest.xml.我在 apktool 的帮助下仔细检查了结果,以查看打包的 APK AndroidManifest.xml 中发生了什么。 Of course you can use this snipped to override other values if the common method, using an own AndroidManifest.xml in Plugins/Android isn't working for you.当然,如果通用方法(在 Plugins/Android 中使用自己的 AndroidManifest.xml 对您不起作用),您可以使用此剪辑来覆盖其他值。

Disclaimer:免责声明:

  1. Used on a Windows Unity Editor在 Windows Unity 编辑器上使用
  2. I know that parsing the string to a XML document might be better.我知道将字符串解析为 XML 文档可能会更好。 I leave that to someone else.我把它留给别人。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM