-1

Iam working with Unity3d, using C# and the ARKit plugin (2.0 from Github)

In my current application iam using the ARKit for measuring distances. 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.

I want to avoid that there is some performance losing while the user is using a non ARKit Tool. Em i right if i would say, that the ARKit still works in the background, if you where initializing it once? Iam new on the ARKit thing, so i dont have an perfect overview of how to handle it.

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.

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)

If you need more informations, please let me know. Every help or suggestion would be very nice.

Thanks a lot!

IDK
  • 127
  • 11
  • I haven't done this before but I am but I am sure you can't do this with the ARKIT API at this point. Maybe you could with Unity's API. Can you try setting `XRSettings.enabled` to true/false and see if that works? – Programmer Jul 24 '18 at 09:07
  • Hmmm thanks for your input, but i dont think this is the solution in my case. I would have a bit of more control for the plugin. – IDK Jul 24 '18 at 09:32
  • I don't understand your comment. Did you try it? If so, what's the outcome? – Programmer Jul 24 '18 at 09:34

2 Answers2

1

The current ARKit API doesn't have a method to disable or enable it in Unity, during run-time at this point.

With than being said, Unity has its own function to enable and disable VR, AR or XR plugins. If ARKit is built correctly, this method should work. So, you might be able to disable/enable ARKit by setting XRSettings.enabled to false and enable it by setting it to true.

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:

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

then call to disable:

StartCoroutine(DisableAR());
Programmer
  • 121,791
  • 22
  • 236
  • 328
  • Thank you! I'll try this out and give you a feedback, if it works. I didnt knowed ARKit cannot be enabled or disabled by a simple function or something. – IDK Jul 24 '18 at 11:47
0

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

The current ARKit API doesn't have a method to disable or enable it in Unity, during run-time at this point.

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.

    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;
    }
tvdias
  • 821
  • 2
  • 10
  • 25