示例#1
0
    /// <summary>
    /// Export the anchor data stored in game object, take anchor ownership of the shared anchor, and broadcast anchor
    /// data to other players.
    /// </summary>
    public void ShareNetworkAnchor(String anchorId, GameObject gameObject)
    {
        if (NetworkAnchorManager.Instance == null)
        {
            Debug.LogFormat("[NetworkAnchorPlayer] Ignoring share anchor request, as there is no anchor server. (anchor id: {0}) {1}", anchorId, DebugInfo());
            return;
        }

        if (NetworkAnchorManager.Instance.TrySharingAnchor(anchorId, gameObject))
        {
            // Start taking ownership of the anchor
            CmdShareAnchor(SharedAnchorData.Create(anchorId));
        }
    }
    /// <summary>
    /// Invoked after the anchor data has been exported, and can now to shared to other players.
    /// </summary>
    /// <param name="sharedAnchorId">The id of the shared anchor</param>
    /// <param name="sharedGameObject">The game object that owns the anchor</param>
    /// <param name="result">The share result</param>
    private void ExportingAnchorComplete(String sharedAnchorId, GameObject sharedGameObject, NetworkAnchorManager.ExportingAnchorResult result)
    {
        // Start taking ownership of the anchor
        if (result == NetworkAnchorManager.ExportingAnchorResult.Success)
        {
            Debug.LogFormat("[NetworkAnchorPlayer] Succeeded to export. Sending anchor check-in request. {0}", DebugInfo());
            CheckInAnchor(SharedAnchorData.Create(sharedAnchorId));

            if (ExportedAnchor != null)
            {
                ExportedAnchor(this, sharedAnchorId);
            }
        }
        else
        {
            Debug.LogFormat("[NetworkAnchorPlayer] Failed to export. Sending anchor check-in request. {0}", DebugInfo());
            CheckInAnchor(SharedAnchorData.Empty);
        }
    }
    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);
    }