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.
What version of Uniy? Legacy XR or XR SDK?
@joejo i am using unity 2020.3.16.
for XR cardboard im using XR sdk “GitHub - googlevr/cardboard-xr-plugin: Google Cardboard XR Plugin for Unity”
and for AR i am using AR foundation
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.
@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)
TryAddLoader(XRLoader, Int32)
.
in legacy XR we can uncheck and check between AR and VR but now XR management does not allow this.
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:
- Make sure all loaders that you want are selected in XR Management.
- Disable Initialize at Startup as you will need to handle startup yourself.
- At runtime, when you know what you want you can then configure the loader list manually using those TryAdd/Remove APIs.
- 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.
@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. :(
@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 Unity QA: Building quality with passion. 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: End-user documentation | XR Plugin Management | 4.1.0
TryAddLoader API Reference: Class XRManagerSettings | XR Plugin Management | 4.1.0
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:
-
Disabling Initialize at Startup in the XR Management Player Settings.
-
Ensure the relevant loaders you wish to use are checked in the setting.
-
Add a manager that can handle initializing and deinitializing loaders based on what the user is trying to do
-
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.
public class ARAndVRLoaderManager : MonoBehaviour
{
XRManagerSettings m_XRManager;
XRLoader m_ARLoader;
XRLoader m_VRLoader;
bool HasARLoader => m_ARLoader != null;
bool HasVRLoader => m_VRLoader != null;
void OnEnable()
{
m_XRManager ??= XRGeneralSettingsPerBuildTarget.XRGeneralSettingsForBuildTarget(BuildTarget.Android).Manager;
if (m_XRManager == null)
{
Debug.LogError($"XRManagerSettings is null. Can't instantiate XR");
enabled = false;
}
if (m_ARLoader == null || m_VRLoader == null)
{
foreach (var loader in m_XRManager.activeLoaders)
{
if (loader is ARCoreLoader)
{
m_ARLoader = loader;
}
else if (loader is GoogleCardboardLoader)
{
m_VRLoader = loader;
}
}
}
if (!HasARLoader || !HasVRLoader)
{
Debug.LogError($"Loaders are missing from the list!{(!HasVRLoader ? "\n\tVR Loader Missing" : "")}{(!HasARLoader ? "\n\tAR Loader Missing." : "")}");
enabled = false;
}
}
bool SwitchToAR()
{
if (HasARLoader)
{
return false;
}
if (m_XRManager.activeLoader != null)
{
if (m_XRManager.activeLoader != m_ARLoader)
{
m_XRManager.activeLoader.Stop();
m_XRManager.activeLoader.Deinitialize();
}
}
return m_ARLoader.Initialize();
}
bool SwitchToVR()
{
if (HasVRLoader)
{
return false;
}
if (m_XRManager.activeLoader != null)
{
if (m_XRManager.activeLoader != m_VRLoader)
{
m_XRManager.activeLoader.Stop();
m_XRManager.activeLoader.Deinitialize();
}
}
return m_VRLoader.Initialize();
}
}
@davidmo_unity thank you very much for solving my problem.
@davidmo_unity and @joejo really Appreciate your efforts. thanks again to both of you.
@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.
This is something which worked for me.
7948807–1017613–ChangeScenes.cs (1.68 KB)
@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?