Creating anchors on xreal markers

Hello.
I’m trying to develop a project where the app should create an anchor wherever it detects an xreal marker. I tryed using the CoastersImageTracking scene as a base and then insert some components from the MappingExample demo scene for the anchors. I managed to make it create an anchor when it detects a marker, althouh it places it in a wierd position.
I don’t understand if i’m using a wrong method or what.
Here are some screenshots for some context:
here’s where i call the method (whenever it detects the marker with index 10)

Here’s the code:
[SerializeField] private GameObject prefabOPbject;
public void SetTarget(Transform newTarget)
{
target = newTarget;
Debug.Log(“sposto target”);
AddAnchor(prefabOPbject);
}

    public void AddAnchor(GameObject prefab)
    {
        if (m_NRWorldAnchorStore == null || target == null || prefab == null)
        {
            return;
        }

        if (m_NRWorldAnchorStore.IsCreatingNewAnchor)
        {
            return;
        }

        var anchorItem = prefab.GetComponent<AnchorItem>();
        if (anchorItem == null)
        {
            print("anchor item non trovato");
            return;
        }
        var go = Instantiate(prefab);
        go.transform.position = target.position;
        go.transform.rotation = target.rotation;
        Debug.Log("target position: " + target.position + " " + target.rotation + ".\nGO position: " + go.transform.position + " " + go.transform.rotation);

        string key = anchorItem.key;
        NRWorldAnchor anchor = go.AddComponent<NRWorldAnchor>();
        anchor.UserDefinedKey = key;
        bool success = anchor.CreateAnchor();

        if (success)
        {
            print("creo il mapping thing");
            MapQualityIndicator.SetCurrentAnchor(anchor, false);

            ShowMapQualityIndicator(anchor);
        }
        else
        {
            print("qualcosa è andato storto");
            DestroyImmediate(go);
        }

    }

I don’t know if there should be another method that makes it a bit easier.

[EDIT]: I forgot to mention that it actually places the anchor in the last place it detected a yellow marker, is it just an order of code executing?

Nvm i figured that out.
If you need to know how i fixed it reply to this comment and i’ll respond…if i’m still working with xreal stuff.

Where did you get the coasters scene from? How did you fix the issue?

I found the coaster scene by following this guide here:
https://docs.xreal.com/Image%20Tracking/Marker
You download the experimental packet, import it in unity, install the new stuff from unity and you can find the scene in NRSDKExperimental > demos folder.

I solved my issue by setting the LocalMapExample script’s “target” position to the MarkerDemo script’s image position, found in the OnImageLoaded method.
So basically i create a method “SetTarget(Vector3 pos, Vector3 rot)” in the LocalMapExample script, and in OnImageLoaded i wrote localMap.SetTarget(image.GetCenterPose().position, image.GetCenterPose().rotation.eulerAngles).
Then you create an anchor and should add it to the central position of the image.

I don’t know if it’s the best solution, but it’s the one I found.

1 Like