🕶️
NRSDK(Old)
New DocumentationSDK DownloadAPI Reference
v2.1.0
v2.1.0
  • NRSDK Fundamentals
    • NRSDK Overview
    • XREAL Devices
      • XREAL Glasses
      • Controller
      • Compatibility
    • Getting Started with NRSDK
    • Sample Code
    • Tutorials
    • Release Note
      • NRSDK 2.1.1
      • NRSDK 2.1.0
      • NRSDK 1.10.2
      • NRSDK 1.9.5
      • NRSDK 1.9.3
      • NRSDK 1.9.1
      • NRSDK 1.8.0
      • NRSDK 1.7.0
      • NRSDK 1.6.0
  • Development
    • Input and Camera
      • NRInput
      • Interact with Unity UI (Tutorial)
      • Customize Controller UI
      • NRCameraRig
    • Hand Tracking
    • Image Tracking
    • Plane Detection (Tutorial)
      • Overview
      • Import the package
      • Detect planes in the real world
      • Perform a hit test against detected planes
      • Add a car
      • Add gems
      • Wrap up
    • Depth Mesh
      • Meshing Manager Overview
      • Use Meshes in the Editor
      • Tutorial: Mesh Collision
    • Spatial Anchor
      • Mapping Example Scene
      • Tutorial: Halloween Treasure Hunt
        • Handle the Situation of Failed Anchor Saving
      • Tutorial: Sharing Anchors
        • Setting Up Photon
        • Cloud Storage: Firebase (optional)
        • Cloud Storage: Aliyun OSS (optional)
        • Implementing Cloud Save and Load
        • Sharing Anchors with Photon
    • Tools
      • Single Pass Stereo Rendering
      • First Person View
      • Emulator
      • XR Streaming
    • Miscellaneous
      • Access RGB Camera
      • NRSDK Coordinate Systems
      • MRTK Integration
      • Notification popup
      • Reset Camera
      • Render Metrics
      • Render MonoMode
  • API Reference
  • Frequently Asked Questions
  • DESIGN GUIDE
    • Design Guide Overview
    • Displaying
    • Interacting
    • Controlling
    • Navigating
Powered by GitBook
On this page
  • 1. Setting up the UUIDManager
  • 2. Add a ShareUUID method to AnchorItem to receive the UUID of the anchor.
  • 3. Add ShareUUID in the CloudSave method.
  • Conclusion

Was this helpful?

  1. Development
  2. Spatial Anchor
  3. Tutorial: Sharing Anchors

Sharing Anchors with Photon

PreviousImplementing Cloud Save and LoadNextTools

Last updated 1 year ago

Was this helpful?

1. Setting up the UUIDManager

Create a script named UUIDManager.cs. Within the UUIDManager class, utilize a PhotonView to synchronize the UUID. In the AnchorItem class, when there's a need to share the UUID, call the relevant method from UUIDManager. The UUIDManager will then use Photon's RPC mechanism to send the UUID to other clients. Ensure that the UUIDManager class is attached to a GameObject in your scene and that this GameObject has a PhotonView component added to it.

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using Photon.Pun;
using Photon.Realtime;
using NRKernal.NRExamples;

public class UUIDManager : MonoBehaviourPun
{
    public static UUIDManager instance; // 单例实例

    private  Awake()
    {
        if (instance == null)
        {
            instance = this;
        }
        else if (instance != this)
        {
            Destroy(gameObject);
        }
    }

    [PunRPC]
    public void ShareAnchorUUID(string uuid)
    {
        photonView.RPC("ReceiveAnchorUUID", RpcTarget.Others, uuid);
    }

    [PunRPC]
    public void ReceiveAnchorUUID(string uuid)
    {
        Debug.Log("Received UUID: " + uuid);

        GameObject localMapDemo = GameObject.Find("LocalMapDemo");
        if (localMapDemo != null)
        {
            LocalMapExample localMapExample = localMapDemo.GetComponent<LocalMapExample>();
            if (localMapExample != null)
            {
                localMapExample.CloudLoad(uuid);
                Debug.Log("CloudLoad:"+uuid);
            }
            else
            {
                Debug.LogError("LocalMapExample component not found");
            }
        }
        else
        {
            Debug.LogError("LocalMapDemo GameObject not found");
        }
    }

}

2. Add a ShareUUID method to AnchorItem to receive the UUID of the anchor.

 public void ShareUUID()
        {
            if (m_NRWorldAnchor != null)
            {
                UUIDManager.instance.ShareAnchorUUID(m_NRWorldAnchor.UUID);
                Debug.Log("Shared anchor: " + m_NRWorldAnchor.UUID);
            }
        }

3. Add ShareUUID in the CloudSave method.

When a player in the room performs cloud storage, it will be automatically shared with other players in the room.

public async void CloudSave()
        {
            if (m_NRWorldAnchor != null)
            {
                bool result=await NRWorldAnchorStore.Instance.CloudSaveAnchor(m_NRWorldAnchor);
                if (result)
                {
                    NRDebugger.Info("Ready to share anchor: " + m_NRWorldAnchor.UUID);
                    Debug.Log("Ready to share anchor: " + m_NRWorldAnchor.UUID);
                    ShareUUID();
                }
                else
                {
                    Debug.LogError("Failed to save anchor");
                }
            }
            else
            {
                Debug.LogError("m_NRWorldAnchor is null, failed to save this anchor to CloudStorage");
            }
        }  

Conclusion

Congratulations on completing this comprehensive guide on sharing anchors using Photon and Cloud Storage! You've taken significant steps in enhancing the multi-user AR experience. Thank you for dedicating your time and effort to this tutorial. Best of luck with your future AR projects!