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); }
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); }