protected override MLResult.Code StartAPI()
        {
            if (!MLDevice.IsReady())
            {
                MLPluginLog.WarningFormat("MLCamera API is attempting to start before the MagicLeap XR Loader has been initialiazed, this could cause issues with MLCVCamera features. If your application needs these features please wait to start API until Monobehavior.Start and if issue persists make sure ProjectSettings/XR/Initialize On Startup is enabled.");
            }

            MLResult result = MLHeadTracking.Start();

            if (result.IsOk)
            {
                result = MLHeadTracking.GetState(out MLHeadTracking.State headTrackingState);
                if (!result.IsOk)
                {
                    MLPluginLog.ErrorFormat("MLCVCamera.StartAPI failed to get head pose state. Reason: {0}", result);
                }

                headTrackerHandle = headTrackingState.Handle;
                MLHeadTracking.Stop();
            }
            else
            {
                MLPluginLog.ErrorFormat("MLCVCamera.StartAPI failed to get head pose state. MLHeadTracking could not be successfully started.");
            }

            return(MLCVCameraNativeBindings.MLCVCameraTrackingCreate(ref Handle));
        }
        /// <summary>
        /// Utility function set up instance and tracks successful _startCount
        /// </summary>
        /// <param name="requiresXRLoader">Flag to determine if this API requires the XR Loader being initialized.</param>
        /// <returns>
        /// MLResult.Result will be <c>MLResult.Code.Ok</c> if successful.
        /// MLResult.Result will be <c>MLResult.Code.UnspecifiedFailure</c> if failed due to internal error (MagicLeap XR Loader not loaded, no device, DLL not found, no API symbols).
        /// MLResult.Result will otherwise be return value specific API's StartAPI function.
        /// </returns>
        protected static MLResult BaseStart(bool requiresXRLoader = false)
        {
            MLResult result;

            try
            {
                // Check to see if we have already successfully initialized a valid instance
                if (startCount > 0)
                {
                    startCount++;
                    result = MLResult.Create(MLResult.Code.Ok);
                }
                else
                {
                    if (requiresXRLoader && !MLDevice.IsReady())
                    {
                        MLPluginLog.ErrorFormat("MLAPISingleton.BaseStart failed to start {0} API. Reason: MagicLeap XR Loader is not initialized. Please wait to start API until Monobehavior.Start and if issue persists make sure ProjectSettings/XR/Initialize On Startup is enabled.", typeof(T).Name);
                        return(MLResult.Create(MLResult.Code.UnspecifiedFailure, "MagicLeap XR Loader not initialized"));
                    }

                    result = Instance.StartAPI();
                    if (result.IsOk)
                    {
                        // Everything started correctly register the update and increament _startCount
                        MLDevice.RegisterUpdate(Instance.Update);
                        MLDevice.RegisterApplicationPause(Instance.OnApplicationPause);
                        startCount++;

                        Instance.perceptionHandle     = PerceptionHandle.Acquire();
                        Instance.perceptionHasStarted = true;
                    }
                    else
                    {
                        MLPluginLog.ErrorFormat("MLAPISingleton.BaseStart failed to start {0} API. Reason: {1}", typeof(T).Name, result);
                        _instance = null;
                    }
                }

                return(result);
            }
            catch (System.DllNotFoundException)
            {
                MLPluginLog.ErrorFormat(_instance.DllNotFoundError, typeof(T).Name);
                result = MLResult.Create(MLResult.Code.UnspecifiedFailure, "Dll not found");
                MLPluginLog.ErrorFormat("MLAPISingleton.BaseStart failed to start {0} API. Reason: {1}", typeof(T).Name, result);
                _instance = null;
            }
            catch (System.EntryPointNotFoundException)
            {
                string errorMessage = string.Format("{0} API symbols not found", typeof(T).Name);
                result = MLResult.Create(MLResult.Code.UnspecifiedFailure, errorMessage);
                MLPluginLog.ErrorFormat("MLAPISingleton.BaseStart failed to start {0} API. Reason: {1}", typeof(T).Name, result);
                _instance = null;
            }

            return(result);
        }
Example #3
0
        /// <summary>
        /// Calls OnUpdateActions event and dispatches all queued callbacks.
        /// </summary>
        protected override void Update()
        {
            base.Update();

            if (!this.isReady && !(this.isReady = MLDevice.IsReady()))
            {
                return;
            }

            this.OnUpdateEvent?.Invoke();

            MLThreadDispatch.DispatchAll();
        }
        /// <summary>
        ///     This is the only way to initialize this class.
        /// </summary>
        private static void Init()
        {
            allowInit = true;
            instance  = new T();
            allowInit = false;

            if (requiresXRLoader && !MLDevice.IsReady())
            {
                MLPluginLog.Error($"Magic Leap XR Loader is not initialized, and the {typeof(T).Name} API must be started afterwards.");
                return;
            }
            else
            {
                instance.StartInternal();
            }
        }