Beispiel #1
0
    public bool TrySharingAnchor(String anchorId, GameObject gameObject)
    {
        if (HolographicSettings.IsDisplayOpaque)
        {
            Debug.LogFormat("[NetworkAnchorManager] Ignoring share anchor request, as this device doesn't support anchoring. (anchor id: {0})", anchorId);
            return(false);
        }

        if (AnchorSource.AnchorId == anchorId)
        {
            Debug.LogFormat("[NetworkAnchorManager] Ignoring share anchor request, as anchor is already being shared. (anchor id: {0})", anchorId);
            return(false);
        }

        if (LastReceivedAnchor != null && LastReceivedAnchor.AnchorId == anchorId)
        {
            Debug.LogFormat("[NetworkAnchorManager] Ignoring share anchor request, as anchor was just received. (anchor id: {0})", anchorId);
            return(false);
        }

        WorldAnchor worldAnchor = gameObject.GetComponent <WorldAnchor>();

        if (worldAnchor == null)
        {
            Debug.LogErrorFormat("[NetworkAnchorManager] Unable to acquire anchor ownership. Game object is missing an anchor. (anchor id: {0})", anchorId);
            return(false);
        }

        Debug.LogFormat("[NetworkAnchorManager] Attempting to acquire anchor ownership and share anchor with other players. (new anchor id: {0}) {1} {2}", anchorId, AnchorSource.ToString(), DebugInfo());

        // The last received anchor will no longer be relevant since we're taking ownership
        LastReceivedAnchor = null;

        // Stop all pending work on the anchor transmitter
        anchorTransmitter.StopAll();

        // Export binary data
        List <byte> buffer             = new List <byte>();
        WorldAnchorTransferBatch batch = new WorldAnchorTransferBatch();

        batch.AddWorldAnchor(anchorId, worldAnchor);
        WorldAnchorTransferBatch.ExportAsync(
            batch,
            (byte[] data) => { buffer.AddRange(data); },
            (SerializationCompletionReason status) => { ExportAnchorDataComplete(status, buffer.ToArray(), anchorId, gameObject); });

        return(true);
    }
        static async Task <byte[]> ExportWorldAnchorForGameObjectAsync(
            GameObject gameObject)
        {
            byte[] bits = null;

            var worldAnchor = gameObject.GetComponent <WorldAnchor>();

            await UpdateCheckPredicate.WaitForPredicateAsync(
                gameObject,
                () => worldAnchor.isLocated);

            using (var worldAnchorBatch = new WorldAnchorTransferBatch())
            {
                worldAnchorBatch.AddWorldAnchor("anchor", worldAnchor);

                var completion = new TaskCompletionSource <bool>();

                using (var memoryStream = new MemoryStream())
                {
                    Debug.Log("Exporting world anchor...");

                    WorldAnchorTransferBatch.ExportAsync(
                        worldAnchorBatch,
                        data =>
                    {
                        memoryStream.Write(data, 0, data.Length);
                    },
                        reason =>
                    {
                        Debug.Log("Export completed - succeeded? " +
                                  (reason == SerializationCompletionReason.Succeeded));

                        if (reason != SerializationCompletionReason.Succeeded)
                        {
                            bits = null;
                        }
                        else
                        {
                            bits = memoryStream.ToArray();
                        }
                        completion.SetResult(bits != null);
                    }
                        );
                    await completion.Task;
                }
            }
            return(bits);
        }
        /// <summary>
        /// Exports and uploads the anchor to the sharing service.
        /// </summary>
        /// <param name="anchor">The anchor to export.</param>
        /// <returns>Success</returns>
        protected override void ExportAnchor(WorldAnchor anchor)
        {
            if (SharingStage.Instance == null ||
                SharingStage.Instance.Manager == null ||
                SharingStage.Instance.CurrentRoom == null)
            {
                Debug.LogErrorFormat("[SharingWorldAnchorManager] Failed to export anchor \"{0}\"!  The sharing service was not ready.", anchor.name);
                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nFailed to export anchor \"{0}\"!  The sharing service was not ready.", anchor.name);
                }

                return;
            }

            if (!shouldExportAnchors)
            {
                if (ShowDetailedLogs)
                {
                    Debug.LogWarningFormat("[SharingWorldAnchorManager] Attempting to export anchor \"{0}\".", anchor.name);
                }

                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nAttempting to export anchor \"{0}\".", anchor.name);
                }

                if (currentAnchorTransferBatch == null)
                {
                    currentAnchorTransferBatch = new WorldAnchorTransferBatch();
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nCreating a new World Anchor Transfer Batch...";
                    }
                }
                else
                {
                    Debug.LogWarning("[SharingWorldAnchorManager] We didn't properly cleanup our WorldAnchorTransferBatch!");
                    if (AnchorDebugText != null)
                    {
                        AnchorDebugText.text += "\nWe didn't properly cleanup our WorldAnchorTransferBatch!";
                    }
                }

                currentAnchorTransferBatch.AddWorldAnchor(anchor.name, anchor);
                shouldExportAnchors = true;
            }
        }
    /// <summary>
    /// Exports the currently created anchor.
    /// </summary>
    private void Export()
    {
        WorldAnchor anchor = GetComponent <WorldAnchor>();

        if (anchor == null)
        {
            Debug.Log("We should have made an anchor by now...");
            return;
        }

        exportingAnchorName = "robot-placement";

        sharedAnchorInterface = new WorldAnchorTransferBatch();
        sharedAnchorInterface.AddWorldAnchor(exportingAnchorName, anchor);
        WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, OnExportComplete);
    }
        /// <summary>
        /// Exports the anchor on the objectToAnchor.
        /// </summary>
        private void ExportAnchor()
        {
            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            WorldAnchor worldAnchor       = objectToAnchor.GetComponent <WorldAnchor>();

            watb.AddWorldAnchor(exportingAnchorName, worldAnchor);
            WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);

            // If we started the observer to find a good anchor position, then we need to
            // stop the observer.
            if (StartedObserver)
            {
                spatialMapping.StopObserver();
                StartedObserver = false;
            }
        }
Beispiel #6
0
    /// <summary>
    /// If we are supposed to create the anchor for export, this is the function to call.
    /// </summary>
    public void CreateAnchor()
    {
        objectToAnchor = SharedCollection.Instance.gameObject;
        WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
        WorldAnchor worldAnchor       = objectToAnchor.GetComponent <WorldAnchor>();

        if (worldAnchor == null)
        {
            worldAnchor = objectToAnchor.AddComponent <WorldAnchor>();
        }

        exportingAnchorName = Guid.NewGuid().ToString();
        Debug.Log("exporting " + exportingAnchorName);
        watb.AddWorldAnchor(exportingAnchorName, worldAnchor);
        WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);
    }
    public void SaveAllColumnAnchorsToDisk(WorldAnchor[] columnAnchors)
    {
        OnSaveAnchorsToDiskStarted();

        WorldAnchorTransferBatch batch = new WorldAnchorTransferBatch();

        foreach (WorldAnchor anchor in columnAnchors)
        {
            batch.AddWorldAnchor(anchor.gameObject.name, anchor);
        }

        ProgressIndicator.Instance.SetProgress(0.10f);
        ProgressIndicator.Instance.SetMessage("Serializing World Anchors to bytes...");

        WorldAnchorTransferBatch.ExportAsync(batch, OnExportDataAvailable, OnExportCompleteColumn);
    }
        /// <summary>
        /// If we are supposed to create the anchor for export, this is the function to call.
        /// </summary>
        public void CreateAnchor()
        {
            if (PlayerPrefs.HasKey(SavedAnchorKey) && AttachToCachedAnchor(PlayerPrefs.GetString(SavedAnchorKey)))
            {
                AnchorName = PlayerPrefs.GetString(SavedAnchorKey);
                Debug.Log("found " + AnchorName + " again");
            }
            else
            {
                Debug.Log("Could not find cached anchor.");
                AnchorName = Guid.NewGuid().ToString();
            }

            exportingAnchorBytes.Clear();
            AnchorNetworkTransmitter.Instance.SetData(null);

            WorldAnchor worldAnchor = objectToAnchor.GetComponent <WorldAnchor>();

            if (worldAnchor == null)
            {
                Debug.Log("Adding a new anchor.");
                worldAnchor = objectToAnchor.AddComponent <WorldAnchor>();
            }

            if (!AnchorEstablished)
            {
                if (worldAnchor.isLocated)
                {
                    Debug.Log("World anchor already located.");

                    Debug.Log("Saving Anchor.");
                    AnchorEstablished = true;
                    SaveAnchor(worldAnchor);
                }
                else
                {
                    worldAnchor.OnTrackingChanged += Anchor_OnTrackingChanged;
                    Anchor_OnTrackingChanged(worldAnchor, worldAnchor.isLocated);
                }
            }

            Debug.Log("exporting " + AnchorName);
            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();

            watb.AddWorldAnchor(AnchorName, worldAnchor);
            WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);
        }
    public override void SaveWorldAnchor(GameObject gameObj, AnchorSavedDelegate anchorSaved)
    {
        if (isDisplayOpaque)
        {
            base.SaveWorldAnchor(gameObj, anchorSaved);
            return;
        }

        WorldAnchor anchor = gameObj != null?gameObj.GetComponent <WorldAnchor>() : null;

        if (anchor == null)
        {
            anchor = gameObj != null?gameObj.GetComponentInParent <WorldAnchor>() : null;
        }

        if (anchor == null)
        {
            if (anchorSaved != null)
            {
                anchorSaved(string.Empty, "AnchorNotFound");
            }
            return;
        }

        // init buffer
        InitMemBuffer(MemBufferLength);

        string anchorId = anchor.gameObject.name;
        WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

        transferBatch.AddWorldAnchor(anchorId, anchor);

        WorldAnchorTransferBatch.ExportAsync(transferBatch,
                                             (data) =>
        {
            WriteMemBuffer(data);
        },
                                             (result) =>
        {
            if (anchorSaved != null)
            {
                anchorSaved(result == SerializationCompletionReason.Succeeded ? anchorId : string.Empty,
                            result == SerializationCompletionReason.Succeeded ? string.Empty : result.ToString());
            }
        });
    }
    void ExportWorldAnchor()
    {
        string guidString = Guid.NewGuid().ToString();

        exportingAnchorName = guidString;

        // Save the anchor to our local anchor store.
        if (SharingStage.Instance.ShowDetailedLogs)
        {
            Debug.Log("Anchor Manager: Exporting anchor " + exportingAnchorName);
        }

#if UNITY_WSA && !UNITY_EDITOR
        worldAnchorTransferBatch = new WorldAnchorTransferBatch();
        worldAnchorTransferBatch.AddWorldAnchor(guidString, worldAnchor);
        WorldAnchorTransferBatch.ExportAsync(worldAnchorTransferBatch, WriteBuffer, ExportComplete);
#endif // UNITY_WSA
    }
Beispiel #11
0
    public void TransferAnchor(WorldAnchor worldAnchor)
    {
        if (worldAnchor == null)
        {
            return;
        }

        exportingAnchorName = worldAnchor.gameObject.name; //Guid.NewGuid().ToString();
        _anchorStore.Delete(exportingAnchorName);

        if (_anchorStore.Save(exportingAnchorName, worldAnchor))
        {
            exportingAnchorBytes.Clear();
            var batch = new WorldAnchorTransferBatch();
            batch.AddWorldAnchor(exportingAnchorName, worldAnchor);
            WorldAnchorTransferBatch.ExportAsync(batch, OnDataAvailable, OnDataExported);
        }
    }
Beispiel #12
0
        /// <summary>
        /// Exports a world anchor over MQTT
        /// Attaches (creates) an anchor, if there is non attached
        /// </summary>
        public void exportAnchorToWorldAnchorExportManager()
        {
            if (Application.isEditor)
            {
                //WA does not work in editor mode
                return;
            }
            //we make sure there is an anchor attached
            attachWorldAnchorNow();

            WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

            transferBatch.AddWorldAnchor(gameObject.name, GetComponent <WorldAnchor>());


            //reset export list
            //toExport = null;

            currentExportProcess = new WorldAnchorExport(transferBatch, onExportProcessComplete);
        }
    private void ProcessNetworkAnchors()
    {
        // Send New Anchors
        if (isNewAnchor && worldAnchor != null)
        {
            isNewAnchor = false;

            Debug.LogError("AnchorControl: ProcessNetworkAnchors");

            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            if (watb.AddWorldAnchor(ClientId, worldAnchor))
            {
                WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, AnchorExportHandler);
            }
            else
            {
                Debug.LogError("WorldAnchorTransferBatch-Add Anchor Error");
            }
        }
    }
Beispiel #14
0
        /// <summary>
        /// If we are supposed to create the anchor for export, this is the function to call.
        /// </summary>
        public void CreateAnchor()
        {
            exportingAnchorBytes.Clear();
            GenericNetworkTransmitter.Instance.SetData(null);
            objectToAnchor = SharedCollection.Instance.gameObject;

            if (PlayerPrefs.HasKey(SavedAnchorKey) && AttachToCachedAnchor(PlayerPrefs.GetString(SavedAnchorKey)))
            {
                exportingAnchorName = PlayerPrefs.GetString(SavedAnchorKey);
                Debug.Log("found " + AnchorName + " again");
            }
            else
            {
                exportingAnchorName = Guid.NewGuid().ToString();
            }

            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            WorldAnchor worldAnchor       = objectToAnchor.GetComponent <WorldAnchor>();

            if (worldAnchor == null)
            {
                Debug.Log("Boink 11");
                string name = gameObject.GetComponent <WorldAnchorManager>().AttachAnchor(objectToAnchor, exportingAnchorName);
                Debug.Log("Boink 12");
                Debug.Log("Added anchor: " + name);
            }
            else
            {
                Debug.Log("Boink 13");
                if (worldAnchor.name != null)
                {
                    Debug.Log("Updating anchor: " + worldAnchor.name);
                }
            }

            Debug.Log("exporting " + exportingAnchorName);
            watb.AddWorldAnchor(exportingAnchorName, worldAnchor);
            Debug.Log("Boink 16");
            WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);
            Debug.Log("Boink 17");
        }
Beispiel #15
0
        /// <summary>
        /// Exports the currently created anchor.
        /// </summary>
        private void Export()
        {
            var anchor = GetComponent <WorldAnchor>();

            string guidString = Guid.NewGuid().ToString();

            exportingAnchorName = guidString;

            // Save the anchor to our local anchor store.
            if (anchor != null && anchorStore.Save(exportingAnchorName, anchor))
            {
                sharedAnchorInterface = new WorldAnchorTransferBatch();
                sharedAnchorInterface.AddWorldAnchor(guidString, anchor);
                WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, ExportComplete);
            }
            else
            {
                Debug.LogWarning("Failed to export anchor, trying again...");
                currentState = ImportExportState.InitialAnchorRequired;
            }
        }
    private void StartExportingWorldAnchor()
    {
        Debug.Log("StartExportingWorldAnchor");

        if (worldAnchor == null)
        {
            worldAnchor = gameObject.GetComponent <WorldAnchor>();
        }

        if (worldAnchor == null)
        {
            return;
        }

        worldAnchorBuffer.Clear();

        WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

        transferBatch.AddWorldAnchor(gameObject.name, worldAnchor);
        WorldAnchorTransferBatch.ExportAsync(transferBatch, OnExportDataAvailable, OnExportComplete);
    }
Beispiel #17
0
        /// <summary>
        ///导出锚点数据
        /// </summary>
        /// <param name="objectToUploadAnchor"></param>
        /// <param name="anchorName"></param>
        public void ExportAnchorData(GameObject objectToUploadAnchor, string anchorName)
        {
            exportAnchorName = anchorName;
#if !UNITY_EDITOR && UNITY_WSA
            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            WorldAnchor worldAnchor       = objectToUploadAnchor.GetComponent <WorldAnchor>();
            if (worldAnchor == null)
            {
                Debug.Log("物体无锚点,无法导出!!!");
            }
            else
            {
                Debug.Log("导出锚点: " + anchorName);
                MYDialog.Instance.Write("导出锚点: " + anchorName);
                watb.AddWorldAnchor(anchorName, worldAnchor);
                WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);
            }
#else
            Debug.Log("非Hololens无法导出锚点!!!");
#endif
        }
Beispiel #18
0
        /// <summary>
        /// If we are supposed to create the anchor for export, this is the function to call.
        /// </summary>
        public void CreateAnchor()
        {
#if UNITY_EDITOR
            Debug.LogError("Anchors cannot be created from the Unity editor.");
#endif

            exportingAnchorBytes.Clear();
            GenericNetworkTransmitter.Instance.SetData(null);
            objectToAnchor = UNetAnchorManager.Instance.gameObject;

            WorldAnchor worldAnchor = objectToAnchor.GetComponent <WorldAnchor>();
            if (worldAnchor == null)
            {
                worldAnchor = objectToAnchor.AddComponent <WorldAnchor>();
            }

            Debug.Log("Checking for saved anchor: " + AnchorName);
            if (PlayerPrefs.HasKey(SavedAnchorKey) && AttachToCachedAnchor(PlayerPrefs.GetString(SavedAnchorKey)))
            {
                exportingAnchorName = PlayerPrefs.GetString(SavedAnchorKey);
                Debug.Log("found " + AnchorName + " again");
            }
            else if (PlayerPrefs.HasKey(AnchorName) && AttachToCachedAnchor(AnchorName))
            {
                exportingAnchorName = AnchorName;
                Debug.Log("_found " + AnchorName + " again");
            }
            else
            {
                exportingAnchorName = Guid.NewGuid().ToString();
            }

            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();

            Debug.Log("exporting " + exportingAnchorName);
            watb.AddWorldAnchor(exportingAnchorName, worldAnchor);
            WorldAnchorTransferBatch.ExportAsync(watb, WriteBuffer, ExportComplete);

            SaveAnchor(exportingAnchorName);
        }
    static async Task <byte[]> ExportWorldAnchorForGameObjectAsync(
        GameObject gameObject)
    {
        byte[] bits = null;

        var worldAnchor = gameObject.GetComponent <WorldAnchor>();

        using (var worldAnchorBatch = new WorldAnchorTransferBatch())
        {
            worldAnchorBatch.AddWorldAnchor("anchor", worldAnchor);

            var completion = new TaskCompletionSource <bool>();

            using (var memoryStream = new MemoryStream())
            {
                WorldAnchorTransferBatch.ExportAsync(
                    worldAnchorBatch,
                    data =>
                {
                    memoryStream.Write(data, 0, data.Length);
                },
                    reason =>
                {
                    if (reason != SerializationCompletionReason.Succeeded)
                    {
                        bits = null;
                    }
                    else
                    {
                        bits = memoryStream.ToArray();
                    }
                    completion.SetResult(bits != null);
                }
                    );
                await completion.Task;
            }
        }
        return(bits);
    }
Beispiel #20
0
        /// <summary>
        /// 导出anchor资料并上传
        /// 注意,上传期间应当阻止应用活动,以避免上传完成之前再次调用
        /// </summary>
        /// <param name="anchors"></param>
        /// <param name="cbFinish"></param>
        public void ExportGameRootAnchor(List <AnchorObjectInfo> anchors, System.Action <bool, string> cbFinish)
        {
            cbExportFinish    = cbFinish;
            uploadFinish      = false;
            uploadErrorString = null;
            uploadApi         = null;

            WorldAnchorTransferBatch transferBatch = new WorldAnchorTransferBatch();

            for (int i = 0; i < anchors.Count; i++)
            {
                AnchorObjectInfo info = anchors[i];
                if (info.anchor != null)
                {
                    bool flag = transferBatch.AddWorldAnchor(info.anchorName, info.anchor);
                    Debug.Log(flag);
                }
            }

            Buffer = new byte[0];
            WorldAnchorTransferBatch.ExportAsync(transferBatch, OnExportDataAvailable, OnExportComplete);
        }
Beispiel #21
0
    /// <summary>
    /// 房主将本地锚点共享给其他人
    /// </summary>
    private void Export()
    {
        // 获取锚点,这个组件会在CreateAnchorLocally()中自动添加
        WorldAnchor anchor = GetComponent <WorldAnchor>();

        anchorStore.Clear();
        // 本地保存该锚点
        if (anchor != null && anchorStore.Save(ExportingAnchorName, anchor))
        {
            debug("保存锚点完成,准备导出! - Export()");
            // 将锚点导出
            sharedAnchorInterface = new WorldAnchorTransferBatch();
            sharedAnchorInterface.AddWorldAnchor(ExportingAnchorName, anchor);
            WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, ExportComplete);
        }
        else
        {
            debug("保存本地锚点失败! - Export()");

            currentState = ImportExportState.InitialAnchorRequired;
        }
    }
Beispiel #22
0
        /// <summary>
        /// Exports the currently created anchor.
        /// </summary>
        private void Export()
        {
            WorldAnchor anchor     = this.GetComponent <WorldAnchor>();
            string      guidString = Guid.NewGuid().ToString();

            exportingAnchorName = guidString;

            // Save the anchor to our local anchor store.
            if (anchor != null && anchorStore.Save(exportingAnchorName, anchor))
            {
                if (SharingStage.Instance.ShowDetailedLogs)
                {
                    Debug.Log("Anchor Manager: Exporting anchor " + exportingAnchorName);
                }

                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nExporting anchor {0}", exportingAnchorName);
                }

                sharedAnchorInterface = new WorldAnchorTransferBatch();
                sharedAnchorInterface.AddWorldAnchor(guidString, anchor);
                WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, ExportComplete);
            }
            else
            {
                Debug.LogWarning("Anchor Manager: Failed to export anchor, trying again...");

                if (AnchorDebugText != null)
                {
                    AnchorDebugText.text += string.Format("\nFailed to export anchor, trying again...");
                }

                currentState = ImportExportState.InitialAnchorRequired;
            }
        }
    public bool ExportAnchorAsync(int attempts, String anchorId, GameObject gameObject, ExportingAnchorCompleteDelegate completeDelegate)
    {
        ExportingAnchorResult result = ExportingAnchorResult.Unknown;

        lock (ImportingAndExportingLock)
        {
            WorldAnchor worldAnchor = gameObject.GetComponent <WorldAnchor>();

            if (HolographicSettings.IsDisplayOpaque)
            {
                Debug.LogFormat("[NetworkAnchorManager] Ignoring export anchor request, as this device doesn't support anchoring. (anchor id: {0})", anchorId);
                result = ExportingAnchorResult.FailedDisplayIsOpaque;
            }
            else if (SyncVar_AnchorSource.AnchorId == anchorId)
            {
                Debug.LogFormat("[NetworkAnchorManager] Ignoring export anchor request, as anchor is already being shared. (anchor id: {0})", anchorId);
                result = ExportingAnchorResult.FailedAnchorIsAlreadyShared;
            }
            else if (ImportedAnchor != null && ImportedAnchor.AnchorId == anchorId)
            {
                Debug.LogFormat("[NetworkAnchorManager] Ignoring export anchor request, as anchor was just received. (anchor id: {0})", anchorId);
                result = ExportingAnchorResult.FailedAnchorWasJustReceived;
            }
            else if (worldAnchor == null)
            {
                Debug.LogErrorFormat("[NetworkAnchorManager] Unable to export anchor. Game object is missing an anchor. (anchor id: {0})", anchorId);
                result = ExportingAnchorResult.FailedGameObjectMissingAnchor;
            }
            else if (attempts > retryExportAttempts)
            {
                Debug.LogErrorFormat("[NetworkAnchorManager] Failed to export, attempted to retry exporting too many times. (anchor id: {0})", anchorId);
                result = ExportingAnchorResult.FailedRetriedTooManyTimes;
            }

            if (result == ExportingAnchorResult.Unknown)
            {
                Debug.LogFormat("[NetworkAnchorManager] Attempting to export an anchor, so it can be shared with other players. (new anchor id: {0}) {1} {2}", anchorId, SyncVar_AnchorSource.ToString(), DebugInfo());

                try
                {
                    // Stop all pending work on the anchor transmitter
                    anchorTransmitter.StopAll();

                    // Export binary data
                    List <byte> buffer             = new List <byte>();
                    WorldAnchorTransferBatch batch = new WorldAnchorTransferBatch();
                    batch.AddWorldAnchor(anchorId, worldAnchor);
                    WorldAnchorTransferBatch.ExportAsync(
                        batch,
                        (byte[] data) => { buffer.AddRange(data); },
                        (SerializationCompletionReason status) => { ExportAnchorDataComplete(attempts, status, buffer.ToArray(), anchorId, gameObject, completeDelegate); });
                }
                catch (Exception e)
                {
                    Debug.LogFormat("[NetworkAnchorManager] Unknown error occurred when trying to export anchor. (exception message: {0}) (new anchor id: {1}) {2} {3}", e.Message, anchorId, SyncVar_AnchorSource.ToString(), DebugInfo());
                    result = ExportingAnchorResult.FailedUnknown;
                }
            }

            if (result == ExportingAnchorResult.Unknown)
            {
                // The last received anchor will no longer be relevant since we're taking ownership
                ImportedAnchor = null;

                // no longer loading an anchor
                ImportingAnchorSource = SharedAnchorData.Empty;

                // save the anchor being exported
                ExportingAnchorSource = SharedAnchorData.Create(anchorId);
            }
        }

        // Notify callback of failure
        if (result != ExportingAnchorResult.Unknown && completeDelegate != null)
        {
            completeDelegate(anchorId, gameObject, result);
        }

        return(result == ExportingAnchorResult.Unknown);
    }
Beispiel #24
0
    /// <summary>
    /// Exports the currently created anchor.
    /// </summary>
    void ExportFromAnchorStore()
    {
        //WorldAnchor anchor = GetComponent<WorldAnchor>();

        //if (anchor == null)
        //{
        //    DisplayUI.Instance.AppendText("We should have made an anchor by now...");
        //    return;
        //}

        //string guidString = Guid.NewGuid().ToString();
        //exportingAnchorName = guidString;

        // Save the anchor to our local anchor store.
        //if (anchorStore.Save(exportingAnchorName, anchor))
        //{
        //    sharedAnchorInterface = new WorldAnchorTransferBatch();
        //    sharedAnchorInterface.AddWorldAnchor(guidString, anchor);
        //    WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, ExportComplete);
        //}
        //else
        //{
        //    DisplayUI.Instance.AppendText("This anchor didn't work, trying again");
        //    currentState = ImportExportState.InitialAnchorRequired;
        //}

        DisplayUI.Instance.AppendText("ExportFromAnchorStore()");
        var  anchorCount = anchorStore.anchorCount;
        bool exportReady = true;

        if (anchorCount > 0)
        {
            var sb = new StringBuilder("EXPORTING Anchor");
            sb.Append(anchorCount == 1 ? ": " : "s: ");
            sharedAnchorInterface = new WorldAnchorTransferBatch();
            foreach (var id in anchorStore.GetAllIds())
            {
                sb.Append(id);
                sb.Append(" ");
                // TODO: ENABLE!
                if (exportReady)
                {
                    try
                    {
                        sharedAnchorInterface.AddWorldAnchor(id, anchorStore.Load(id, transform.gameObject));
                        exportingAnchorName = id;
                    }
                    catch (Exception ex)
                    {
                        DisplayUI.Instance.AppendText("ExportFromAnchorStore() ERROR: " + ex.Message);
                        exportReady = false;
                    }
                }
            }
            DisplayUI.Instance.AppendText(sb.ToString());

            if (exportReady)
            {
                WorldAnchorTransferBatch.ExportAsync(sharedAnchorInterface, WriteBuffer, ExportComplete);
            }
        }
        else
        {
            DisplayUI.Instance.AppendText("ExportFromAnchorStore() - ERROR -- NO ANCHORS!");
        }
    }
    public void PressAndGenerate()
    {
        if (Choice == Selection.CreateNew)
        {
            //TextAsset asset = Resources.Load("data_demonstration") as TextAsset;
            //anchorData = asset.bytes;
            //WorldAnchorTrans wat = new WorldAnchorTrans
            //{
            //    header = "wa",
            //    spaceName = spaceId,
            //    data = anchorData
            //};
            //tCP.SendWorlAnchor(wat);

            TimeCost tc4 = new TimeCost("OFALL", Time.time.ToString() + "start generating the anchor");
            tCP.SocketSendByte(tc4);
            //GameObject parentanchor = GameObject.Find("anchor1");
            WorldAnchor wa1 = anchorObject1.AddComponent <WorldAnchor>();
            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            watb.AddWorldAnchor(anchorObject1.name, wa1);
            WorldAnchor wa2 = anchorObject2.AddComponent <WorldAnchor>();
            watb.AddWorldAnchor(anchorObject2.name, wa2);
            TimeCost tc5 = new TimeCost("OFALL", Time.time.ToString() + "Generating anchor done");
            tCP.SocketSendByte(tc5);
            imgSet.SetActive(true);
            //tCP.InitSocket();

            exportedData = new byte[0];
            e            = new List <byte>();
            indicator.GetComponent <MeshRenderer>().material.color = Color.yellow;
            syncOrNot = true;
            TimeCost tc6 = new TimeCost("OFALL", Time.time.ToString() + "start serializing the anchor");
            tCP.SocketSendByte(tc6);
            WorldAnchorTransferBatch.ExportAsync(watb,
                                                 (data) =>
            {
                e.AddRange(data);
                exportedData = data;
            },
                                                 (reason) =>
            {
                if (reason == SerializationCompletionReason.Succeeded)
                {
                    WorldAnchorTrans wat = new WorldAnchorTrans
                    {
                        header    = "wa",
                        spaceName = spaceId,
                        data      = e.ToArray()
                    };
                    TimeCost tc7 = new TimeCost("OFALL", Time.time.ToString() + "start sending anchor data");
                    tCP.SocketSendByte(tc7);
                    tCP.SendWorlAnchor(wat);
                    //CreateNewAnchorInManager();
                    indicator.GetComponent <MeshRenderer>().material.color = Color.green;

                    syncOrNot = true;
                }
                else
                {
                    Debug.Log("failed to upload world anchor, please try agagin");
                    indicator.GetComponent <MeshRenderer>().material.color = Color.red;
                }
            });
        }
        else if (Choice == Selection.SycnDirectly)
        {
            //tCP.InitSocket();
            StartCoroutine(DownloadAnchor(spaceId));
        }
        else if (Choice == Selection.LocationSync)
        {
            SendMsg msgNeed = new SendMsg("NeedToSyncPlantSet");
            tCP.SocketSendByte(msgNeed);
        }

        else if (Choice == Selection.CreateNew_multi)
        {
            WorldAnchor wa1 = anchorObject1.AddComponent <WorldAnchor>();
            WorldAnchorTransferBatch watb = new WorldAnchorTransferBatch();
            watb.AddWorldAnchor(anchorObject1.name, wa1);
            WorldAnchor wa2 = anchorObject2.AddComponent <WorldAnchor>();
            watb.AddWorldAnchor(anchorObject2.name, wa2);
            WorldAnchor wa3   = anchorObject3.AddComponent <WorldAnchor>();
            WorldAnchor wa4   = anchorObject4.AddComponent <WorldAnchor>();
            WorldAnchor wa5   = anchorObject5.AddComponent <WorldAnchor>();
            WorldAnchor wa2_1 = anchorObject2_1.AddComponent <WorldAnchor>();
            WorldAnchor wa3_1 = anchorObject3_1.AddComponent <WorldAnchor>();
            imgSet.SetActive(true);
        }
        else if (Choice == Selection.Version1)
        {
            anchorObject1.SetActive(true);
            anchorObject2_1.SetActive(true);
            anchorObject3_1.SetActive(true);
        }
        else if (Choice == Selection.Version2)
        {
            anchorObject1.SetActive(true);
            anchorObject2.SetActive(true);
            anchorObject3.SetActive(true);
            anchorObject4.SetActive(true);
            anchorObject5.SetActive(true);
        }
    }