Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Resolved Toggle Between VR and AR through XR Plugin Management

Discussion in 'VR' started by HamzaMushtaq, Sep 28, 2021.

  1. HamzaMushtaq

    HamzaMushtaq

    Joined:
    Sep 24, 2017
    Posts:
    10
    hi guys i want o to do toggle between XR cardboard Plugin and AR core but I am unable to do so due to XR plugin management.
     
  2. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    What version of Uniy? Legacy XR or XR SDK?
     
    HamzaMushtaq likes this.
  3. HamzaMushtaq

    HamzaMushtaq

    Joined:
    Sep 24, 2017
    Posts:
    10

    Attached Files:

  4. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    VR and AR are going to vary widely in terms of feature set, user expectations, etc. I'm not sure that trying to implement ONE application that supports both will work. Right off the top of my head I doubt that most of the feature set enabled by AR Foundation is at all applicable or usable on Cardboard.

    I think the best bet for this is to make your game code that is independent of AR/VR separate and then script your build to target AR and VR separately. XR Management can be scripted to allow you to change the loader systems you want in a build but beyond that it will depend on your code and how much you can share as opposed to what needs to be turned on/off completely for each build.

    Not sure how much of a help that is for you? There may be others on the forums with more experience in trying to pull this off than I for sure. Just my $.02.
     
  5. HamzaMushtaq

    HamzaMushtaq

    Joined:
    Sep 24, 2017
    Posts:
    10
    @joejo Thankyou for your response. the main issue is that only "TryRemoveLoader(XRLoader)" function is working in XR Management and that funcation totally remove 1 leader like if i clicked on AR button it will remove VR loarder and my app is totally AR now and vise versa
    these 2 functions are not working at runtime.
    TrySetLoaders(List<XRLoader>)
    TryAddLoader(XRLoader, Int32)
    .

    in legacy XR we can uncheck and check between AR and VR but now XR management does not allow this.
     
  6. joejo

    joejo

    Unity Technologies

    Joined:
    May 26, 2016
    Posts:
    958
    You can't add loaders at runtime that were not available at the time of the build. Those APIs are used at build time when scripting XR Management.

    In order to do what are describing you have to do the following:

    1) Make sure all loaders that you want are selected in XR Management.
    2) Disable Initialize at Startup as you will need to handle startup yourself.
    3) At runtime, when you know what you want you can then configure the loader list manually using those TryAdd/Remove APIs.
    4) You are going to have to make sure that, on Android, you don't have the ARCore and Cardboard loaders active in the list at the same time. XR Management will immediately stop at the first loader that succeeds regardless on whether or not you want one or the other to run.
     
  7. HamzaMushtaq

    HamzaMushtaq

    Joined:
    Sep 24, 2017
    Posts:
    10
    @joejo thank you for your precious time. i did these steps but the results are same.
    • Remove API removes loader but TryAdd API does not add loader at runtime.
    • my app is a hybrid APP that is using AR and VR. user can toggle between them at runtime.kindly guild me on how we can achieve this.

      Right now if user select AR then app will remove VR loader from TryRemove API.then if user came back from AR and select vr he is unable to go in VR .as TryRemove APi removed VR loader and TryAdd APi is not working at runtime. :(:(
     
  8. davidmo_unity

    davidmo_unity

    Unity Technologies

    Joined:
    Jun 18, 2019
    Posts:
    99
    @HamzaMustaq You shouldn't need to remove any loaders from your list but if the TryAddLoader API is returning false in a scenario where it should return true during runtime then please log a bug https://unity3d.com/unity/qa/bug-reporting. Keep in mind that for TryAddLoader to succeed, the loader type (i.e. ARCoreLoader) must have existed in the list at startup. See the documentation links below for more details.

    Relevant XR Management Documentation Links
    Modifying the List of Loaders Example: https://docs.unity3d.com/Packages/c...ndUser.html#example-modifying-the-loader-list
    TryAddLoader API Reference: https://docs.unity3d.com/Packages/c...tyEngine_XR_Management_XRLoader_System_Int32_

    You should not need to remove any loaders during runtime, however. Instead what you should be doing is manually managing the lifecycle of your loaders by calling Initialize and Deinitialize on the loaders in question. To manually handle the lifecycle you should do the following:

    1. Disabling Initialize at Startup in the XR Management Player Settings.
    2. Ensure the relevant loaders you wish to use are checked in the setting.
    3. Add a manager that can handle initializing and deinitializing loaders based on what the user is trying to do
    4. Build your application.
    Something like the following would work for loader management though it's not a complete solution and has not been tested to ensure compilation.

    Code (CSharp):
    1.  
    2. public class ARAndVRLoaderManager : MonoBehaviour
    3. {
    4.     XRManagerSettings m_XRManager;
    5.  
    6.     XRLoader m_ARLoader;
    7.     XRLoader m_VRLoader;
    8.  
    9.     bool HasARLoader => m_ARLoader != null;
    10.     bool HasVRLoader => m_VRLoader != null;
    11.  
    12.     void OnEnable()
    13.     {
    14.         m_XRManager ??= XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTarget.Android).Manager;
    15.         if (m_XRManager == null)
    16.         {
    17.             Debug.LogError($"XRManagerSettings is null. Can't instantiate XR");
    18.             enabled = false;
    19.         }
    20.  
    21.         if (m_ARLoader == null || m_VRLoader == null)
    22.         {
    23.             foreach (var loader in m_XRManager.activeLoaders)
    24.             {
    25.                 if (loader is ARCoreLoader)
    26.                 {
    27.                     m_ARLoader = loader;
    28.                 }
    29.                 else if (loader is GoogleCardboardLoader)
    30.                 {
    31.                     m_VRLoader = loader;
    32.                 }
    33.             }
    34.         }
    35.  
    36.         if (!HasARLoader || !HasVRLoader)
    37.         {
    38.             Debug.LogError($"Loaders are missing from the list!{(!HasVRLoader ? "\n\tVR Loader Missing" : "")}{(!HasARLoader ? "\n\tAR Loader Missing." : "")}");
    39.             enabled = false;
    40.         }
    41.     }
    42.  
    43.     bool SwitchToAR()
    44.     {
    45.         if (HasARLoader)
    46.         {
    47.             return false;
    48.         }
    49.  
    50.         if (m_XRManager.activeLoader != null)
    51.         {
    52.             if (m_XRManager.activeLoader != m_ARLoader)
    53.             {
    54.                 m_XRManager.activeLoader.Stop();
    55.                 m_XRManager.activeLoader.Deinitialize();
    56.             }
    57.         }
    58.  
    59.         return m_ARLoader.Initialize();
    60.     }
    61.  
    62.     bool SwitchToVR()
    63.     {
    64.         if (HasVRLoader)
    65.         {
    66.             return false;
    67.         }
    68.  
    69.         if (m_XRManager.activeLoader != null)
    70.         {
    71.             if (m_XRManager.activeLoader != m_VRLoader)
    72.             {
    73.                 m_XRManager.activeLoader.Stop();
    74.                 m_XRManager.activeLoader.Deinitialize();
    75.             }
    76.         }
    77.  
    78.         return m_VRLoader.Initialize();
    79.     }
    80. }
    81.  
     
  9. HamzaMushtaq

    HamzaMushtaq

    Joined:
    Sep 24, 2017
    Posts:
    10
  10. skjagan

    skjagan

    Joined:
    May 18, 2020
    Posts:
    6
    @davidmo_unity
    All of the methods in the solution which you gave returns true, but they never work (eg: TryAddLoader is returning true but it never added any loader). This bug is very problematic for AR/VR developers developing for mobiles. So, please fix thing ASAP.
     
  11. skjagan

    skjagan

    Joined:
    May 18, 2020
    Posts:
    6
    This is something which worked for me.
     

    Attached Files:

    HenryBech likes this.
  12. Brady

    Brady

    Joined:
    Sep 25, 2008
    Posts:
    2,473
    @davidmo_unity, I don't remember how to report problems with the documentation, but there are multiple errors in the examples given here:
    https://docs.unity3d.com/Packages/com.unity.xr.management@4.4/manual/EndUser.html

    Things like variable spelling errors (reorderedLoaderList vs reorderedLoadersList) and incorrect arguments, including to the very APIs being documented! The example for modifying the loader list at runtime even uses EDITOR-ONLY code! WTF?!

    It's rather unsettling that these examples were obviously not tested (since they won't even compile), which makes me lose all confidence that even after the compiler errors are solved, it will even work at all as intended. And to solve all the compiler errors, I have to make some guesses about substituting some of the editor-only code. Kinda defeats the purpose of documentation, don't you think?
     
    Last edited: Sep 28, 2023
    HenryBech likes this.