首页 > 解决方案 > 在运行时更改图形 API (Unity)

问题描述

我目前正在尝试同时使用两种不同的 AR 算法(ARCore 和 EasyAR)。ARCore 目前只支持一小部分设备,所以我们希望在 ARCore 不足的时候让 EasyAR 接手。

问题是,EasyAR 需要 OpenGL ES2,而 ARCore 需要 OpenGL ES3。我编写了检查设备上是否存在 ARCore 的代码,然后导航到使用适当技术的场景。我还实现了统一的PlayerSettings.SetGraphicsAPIs(),但这在部署时不起作用,只能在 Unity 的编辑器中使用。现在我正在寻找一种确实有效的替代方案。

代码需要在安卓和IOS上运行。

public IEnumerator CheckCompatibility()
{
    AsyncTask<ApkAvailabilityStatus> checkTask = Session.CheckApkAvailability();
    CustomYieldInstruction customYield = checkTask.WaitForCompletion();
    yield return customYield;
    ApkAvailabilityStatus result = checkTask.Result;
    switch (result)
    {
        case ApkAvailabilityStatus.SupportedApkTooOld:
            _ShowAndroidToastMessage("Supported apk too old");
            break;
        case ApkAvailabilityStatus.SupportedInstalled:
            _ShowAndroidToastMessage("Supported and installed");
            GraphicsDeviceType[] easyAPI = { GraphicsDeviceType.OpenGLES2, GraphicsDeviceType.Vulkan };

            SceneManager.LoadScene("ARCoreImageTracker");
            break;
        case ApkAvailabilityStatus.SupportedNotInstalled:
            _ShowAndroidToastMessage("Supported, not installed, requesting installation");
            Session.RequestApkInstallation(false);
            break;
        case ApkAvailabilityStatus.UnknownChecking:
            _ShowAndroidToastMessage("Unknown Checking");
            break;
        case ApkAvailabilityStatus.UnknownError:
            _ShowAndroidToastMessage("Unknown Error");
            break;
        case ApkAvailabilityStatus.UnknownTimedOut:
            _ShowAndroidToastMessage("Unknown Timed out");
            break;
        case ApkAvailabilityStatus.UnsupportedDeviceNotCapable:
            _ShowAndroidToastMessage("Unsupported Device Not Capable");
            GraphicsDeviceType[] coreAPI = { GraphicsDeviceType.OpenGLES2 };
            PlayerSettings.SetGraphicsAPIs(BuildTarget.Android, coreAPI);
            SceneManager.LoadScene("EasyARImageTracker");
            break;
    }
}

目前,我得到一个错误列表,上面写着"The name 'PlayerSettings' does not exist in the current context"。我可以尝试任何替代方案吗?

标签: c#unity3d

解决方案


据我所知, PlayerSettings.SetGraphicsAPIs 在运行时不起作用。它仅适用于 UnityEditor。我认为您应该为 ARCore 和 EasyAR 单独构建。


推荐阅读