简体   繁体   English

在 Unity3d 中在运行时禁用/启用 ARKit - C#

[英]Disable/enable ARKit during runtime in Unity3d - C#

Iam working with Unity3d, using C# and the ARKit plugin (2.0 from Github)我正在使用 Unity3d,使用 C# 和 ARKit 插件(来自 Github 的 2.0)

In my current application iam using the ARKit for measuring distances.在我当前的应用程序中,我使用 ARKit 来测量距离。 The tool iam creating needs this functionality only for this reason, so i was wondering how i could enable the ARKit, when the user needs the ruler and disable it, if not.我创建的工具仅出于这个原因才需要此功能,所以我想知道如何启用 ARKit,当用户需要标尺并禁用它时,如果不需要。

I want to avoid that there is some performance losing while the user is using a non ARKit Tool.我想避免在用户使用非 ARKit 工具时出现一些性能损失。 Em i right if i would say, that the ARKit still works in the background, if you where initializing it once?我是对的,如果我要说,ARKit 仍然在后台工作,如果你在哪里初始化它一次? Iam new on the ARKit thing, so i dont have an perfect overview of how to handle it.我是 ARKit 的新手,所以我对如何处理它没有一个完美的概述。

To drop some Code lines makes no sence, its basically the plugin importet into the project, and my own script which depends on some functions - i didnt changed anything in the source code of the plugin.删除一些代码行没有任何意义,它基本上是项目中的插件导入,以及我自己的依赖于某些功能的脚本 - 我没有更改插件的源代码中的任何内容。 The measuring tool itself which i programmed works pretty well, but i could not determine how to activate and deactivate basically the ARKit.我编程的测量工具本身运行良好,但我无法确定如何激活和停用 ARKit。

Can someone help me out with this?有人可以帮我解决这个问题吗? When iam disabeling the GameObjects, the scripts are running at it seems to be a "dirty" method to avoid those functionallitys but i have to make it clean (for excample also the video map in the background needs to be disabled - and i guess those ARKit functions will not be paused or disabled, just because some scripts are disalbed, it seems the api still runs in the background because it lags when i do so)当我禁用游戏对象时,正在运行的脚本似乎是一种避免这些功能的“肮脏”方法,但我必须使其干净(例如,背景中的视频地图也需要禁用 - 我猜那些ARKit 函数不会被暂停或禁用,只是因为某些脚本被禁用,api 似乎仍在后台运行,因为它在我这样做时滞后)

If you need more informations, please let me know.如果您需要更多信息,请告诉我。 Every help or suggestion would be very nice.每一个帮助或建议都会非常好。

Thanks a lot!非常感谢!

The current ARKit API doesn't have a method to disable or enable it in Unity, during run-time at this point.当前的 ARKit API 没有在运行时在 Unity 中禁用或启用它的方法。

With than being said, Unity has its own function to enable and disable VR, AR or XR plugins.话虽如此,Unity 有自己的功能来启用和禁用 VR、AR 或 XR 插件。 If ARKit is built correctly, this method should work.如果 ARKit 构建正确,则此方法应该可以工作。 So, you might be able to disable/enable ARKit by setting XRSettings.enabled to false and enable it by setting it to true .因此,您可以通过将XRSettings.enabled设置为false并通过将其设置为true来启用它来禁用/启用 ARKit。

It's also a good idea to call XRSettings.LoadDeviceByName with an empty string, wait for frame, before setting XRSettings.enabled to false to disable it:使用空字符串调用XRSettings.LoadDeviceByName也是一个好主意,等待帧,然后将XRSettings.enabled设置为false以禁用它:

IEnumerator DisableAR()
{
    XRSettings.LoadDeviceByName("");
    yield return null;
    XRSettings.enabled = false;
}

then call to disable:然后调用禁用:

StartCoroutine(DisableAR());

I guess I am answering a pretty old post.我想我正在回答一个很老的帖子。 I found a way but I don't know if that is what you are expecting.我找到了一种方法,但我不知道这是否是您的期望。

Like @Programmer told就像@Programmer 说的

The current ARKit API doesn't have a method to disable or enable it in Unity, during run-time at this point.当前的 ARKit API 没有在运行时在 Unity 中禁用或启用它的方法。

So the way that I used is incorporating Programmer's code along with that if you need the camera to render some skybox or solid color, I have done something like that in Non-AR mode by saving the current texture before changing it, as the live video is given as texture to the material and after saving that changed the texture to null and when you want to re-enable AR you set the textures back to the saved value and it gets loaded correctly.所以我使用的方法是结合程序员的代码,如果你需要相机来渲染一些天空盒或纯色,我在非 AR 模式下做了类似的事情,通过在更改之前保存当前纹理,作为实时视频被作为材质提供给材质,保存后将纹理更改为空,当您想要重新启用 AR 时,您将纹理设置回保存的值并正确加载。

    bool ARMode;
    bool isSupported;
    Camera cam;
    UnityARCameraManager ARCameraManager;
    private Texture2D _videoTextureY;
    private Texture2D _videoTextureCbCr;

    private void Awake()
    {
        cam = Camera.main;
        isSupported = FindObjectOfType<UnityARCameraManager>().sessionConfiguration.IsSupported;
        ARMode = isSupported;
        ARCameraManager = FindObjectOfType<UnityARCameraManager>();
    }

    void DisableAR()
    {

        XRSettings.enabled = false;
        ARCameraManager.enabled = false;
        _videoTextureY = (Texture2D)cam.GetComponent<UnityARVideo>().m_ClearMaterial.GetTexture("_textureY");
        _videoTextureCbCr = (Texture2D)cam.GetComponent<UnityARVideo>().m_ClearMaterial.GetTexture("_textureCbCr");

        cam.GetComponent<UnityARVideo>().m_ClearMaterial.SetTexture("_textureY", Texture2D.blackTexture);
        cam.GetComponent<UnityARVideo>().m_ClearMaterial.SetTexture("_textureCbCr", Texture2D.blackTexture);
        cam.clearFlags = CameraClearFlags.SolidColor;
        cam.backgroundColor = Color.black;
        cam.GetComponent<UnityARVideo>().enabled = false;
    }

    void EnableAR()
    {
        ARCameraManager.enabled = true;
        XRSettings.enabled = true;
        cam.clearFlags = CameraClearFlags.Depth;
        cam.GetComponent<UnityARVideo>().m_ClearMaterial.SetTexture("_textureY", _videoTextureY);
        cam.GetComponent<UnityARVideo().m_ClearMaterial.SetTexture("_textureCbCr", _videoTextureCbCr);
        cam.GetComponent<UnityARVideo>().enabled = true;
    }

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

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