Esempio n. 1
0
 /// <summary>
 /// Gets the anchor pointer from the anchor identifier.
 /// </summary>
 /// <param name="arkitSessionNativeInterface">The ARKit session native interface.</param>
 /// <param name="anchorId">The anchor identifier.</param>
 /// <returns><see cref="IntPtr"/>.</returns>
 public static IntPtr GetAnchorPointerFromId(this UnityARSessionNativeInterface arkitSessionNativeInterface, string anchorId)
 {
     return(ARKitNativeHelpers.GetAnchorPointerFromId(arkitSessionNativeInterface.GetNativeSessionPtr(), anchorId));
 }
Esempio n. 2
0
        /// <summary>
        /// Creates a new session if one does not exist.
        /// </summary>
        /// <returns>
        /// A <see cref="Task"/> that represents the operation.
        /// </returns>
#pragma warning disable CS1998 // Conditional compile statements are removing await
        public async Task CreateSessionAsync()
#pragma warning restore CS1998
        {
            // Warn if already created
            if (session != null)
            {
                Debug.LogWarning($"{nameof(CreateSessionAsync)} called but session is already created.");
                return;
            }

            // Only allow a session to be created the manager is properly configured.
            EnsureValidConfiguration(disable: false, exception: true);

            #if UNITY_ANDROID // Android Only
            // We should only run the Java initialization once
            if (!javaInitialized)
            {
                // Create a TaskCompletionSource that we can use to know when
                // the Java plugin has completed initialization on the Android
                // thread.
                TaskCompletionSource <bool> pluginInit = new TaskCompletionSource <bool>();

                // Make sure ARCore is running. This code must be executed
                // on a Java thread provided by Android.
                AndroidHelper.Instance.DispatchUiThread(unityActivity =>
                {
                    // Create the plugin
                    using (AndroidJavaClass cloudServices = new AndroidJavaClass("com.microsoft.CloudServices"))
                    {
                        // Initialize the plugin
                        cloudServices.CallStatic("initialize", unityActivity);

                        // Update static variable to say that the plugin has been initialized
                        javaInitialized = true;

                        // Set the task completion source so the CreateSession method can
                        // continue back on the Unity thread.
                        pluginInit.SetResult(true);
                    }
                });

                // Wait for the plugin to complete initialization on the
                // Java thread.
                await pluginInit.Task;
            }
            #elif UNITY_IOS // iOS Only
            // Make sure ARKit is running
            if (arkitSession == null)
            {
                arkitSession = UnityARSessionNativeInterface.GetARSessionNativeInterface();
                UnityARSessionNativeInterface.ARFrameUpdatedEvent += UnityARSessionNativeInterface_ARFrameUpdatedEvent;
            }
            #endif

            // Create the session instance
            session = new CloudSpatialAnchorSession();

            // Configure logging
            session.LogLevel = logLevel;

            // Configure authentication
            if (authenticationMode == AuthenticationMode.ApiKey)
            {
                // API Key mode just applies credentials directly
                session.Configuration.AccountId  = spatialAnchorsAccountId.Trim();
                session.Configuration.AccountKey = spatialAnchorsAccountKey.Trim();
            }
            else
            {
                // AAD mode requires an auth token workflow
                session.TokenRequired += Session_TokenRequired;
            }

            // Subscribe to session events
            session.OnLogDebug             += OnLogDebug;
            session.SessionUpdated         += OnSessionUpdated;
            session.AnchorLocated          += OnAnchorLocated;
            session.LocateAnchorsCompleted += OnLocateAnchorsCompleted;
            session.Error += OnError;


            #if UNITY_IOS
            session.Session = arkitSession.GetNativeSessionPtr();
            #elif UNITY_ANDROID
            session.Session = GoogleARCoreInternal.ARCoreAndroidLifecycleManager.Instance.NativeSession.SessionHandle;
            #elif UNITY_WSA || WINDOWS_UWP
            // No need to set a native session pointer for HoloLens.
            #else
            throw new NotSupportedException("The platform is not supported.");
            #endif
        }
 protected override void OnConfigureSession(CloudSpatialAnchorSession session)
 {
     session.Session = arkitSession.GetNativeSessionPtr();
 }