How does Google Expect you to use Cloud Anchors in ARCore with Photon?
I'm really racking my brain here trying to understand how I should do this. I'm trying to do a multiplayer AR game, using ARCore with Cloud Anchors for my Master's Thesis.
Using Photon 2, the flow is this:
Master client makes a room.
Master client starts looking for surfaces, using ARCore.
Eventually Master Client has found some surfaces and will be placing an Anchor, to attach the Game Arena to.
When this anchor is placed, Photon should show the Arena that is now spawned but consistently in AR across devices.
The way to do this, as I understand it, is to share the initial Anchor used by the host.
At least, that's how it is in my head.
But I am hitting a snag here. I have never used Photon before so I am in doubt on what parts Photon should do and what parts I pass to Google (I already have some code that makes a room that multiple people can join).
Their Cloud Anchor example is written with UNet and I can't quite figure out how to do the translation to Photon. Frankly I shouldn't use the UNet example either way as UNet is discontinued now.
When my Pong Launcher makes the room, the following happens:
ARPongLauncher.cs
public override void OnJoinedRoom()
{
if (IsDialogShown == true)
{
IsDialogShown = false;
DialogAnimator.SetBool("IsHidden", true);
}
SnackbarText.text = $"Successfully joined {PhotonNetwork.CurrentRoom.Name}";
LobbyScreen.SetActive(false);
TopBar.SetActive(true);
PlayerCountText.text = $"{PhotonNetwork.PlayerList.Length}";
if (PhotonNetwork.LocalPlayer.IsMasterClient)
{
ARPongMasterClient masterClient = PlayerPrefab.AddComponent<ARPongMasterClient>();
masterClient.searchMessage = "Searching for Surfaces...";
masterClient.AnchorPrefabName = AnchorPrefabName;
masterClient.FirstPersonCamera = PlayerPrefab.GetComponentInChildren<Camera>();
masterClient.SnackbarText = SnackbarText;
masterClient.m_ARCoreWorldOriginHelper = PlayerPrefab.GetComponentInChildren<ARCoreWorldOriginHelper>();
masterClient.OnAnchorHosted += Handle_OnAnchorHosted;
}
}
Then in the client I do this:
MasterClient.cs
public event EventHandler<string> OnAnchorHosted;
public override void Start()
{
m_CurrentMode = ApplicationMode.Hosting;
}
public override void Update()
{
base.Update();
if (m_LastPlacedAnchor == null)
{
Touch touch;
if (Input.touchCount < 1 || (touch = Input.GetTouch(0)).phase != TouchPhase.Began)
{
return;
}
if (Application.platform != RuntimePlatform.IPhonePlayer)
{
TrackableHit hit;
if (m_ARCoreWorldOriginHelper.Raycast(
touch.position.x, touch.position.y,
TrackableHitFlags.PlaneWithinPolygon, out hit))
{
m_LastPlacedAnchor = hit.Trackable.CreateAnchor(hit.Pose);
}
}
}
if (m_LastPlacedAnchor != null)
{
if (!m_IsOriginPlaced)
{
SetWorldOrigin(m_LastPlacedAnchor.transform);
_InstantiateAnchor();
OnAnchorInstantiated();
}
}
}
SetWorldOrigin is from the base class:
ARPongAbstractClient.cs
/// <summary>
/// Sets the apparent world origin so that the Origin of Unity's World Coordinate System coincides with the
/// Anchor. This function needs to be called once the Cloud Anchor is either hosted or resolved.
/// </summary>
/// <param name="anchorTransform">Transform of the Cloud Anchor.</param>
internal void SetWorldOrigin(Transform anchorTransform)
{
if (m_IsOriginPlaced)
{
return;
}
m_IsOriginPlaced = true;
if (Application.platform != RuntimePlatform.IPhonePlayer)
{
m_ARCoreWorldOriginHelper.SetWorldOrigin(anchorTransform);
}
}[/code]
And so we finally end up at these methods back in the ARPongMasterClient:
[code=CSharp]private void _InstantiateAnchor()
{
// Instantiate Anchor model at the hit pose.
GameObject anchorObject = PhotonNetwork.Instantiate(AnchorPrefabName, Vector3.zero, Quaternion.identity, 0);
anchorObject.transform.parent = m_LastPlacedAnchor.transform;
// TODO Get anchor id and host in Google Cloud
}
/// <summary>
/// Callback indicating that the Cloud Anchor was instantiated and the host request was made.
/// </summary>
private void OnAnchorInstantiated()
{
if (m_AnchorAlreadyInstantiated)
{
return;
}
m_AnchorAlreadyInstantiated = true;
SnackbarText.text = "Hosting Cloud Anchor...";
}
Here is where I am stuck, currently. I don't know what to do from here. Any help would be appreciated :c
Your answer
![](https://koobas.hobune.stream/wayback/20220612193724im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Colliding plugins[arcore_client.aar] error when building after importing Mapbox SDK 0 Answers
i can't see other player with photon cloud server,I Can't see each other player in photon cloud 2 Answers
How to build project with google cloud (kubernetes, VM) rather than with SourceTree-git 0 Answers