Search Unity

Getting events and tracking multiple objects in ARFoundation

Discussion in 'AR' started by shellyalon, Oct 16, 2019.

  1. shellyalon

    shellyalon

    Joined:
    Mar 26, 2013
    Posts:
    27
    Hello,
    I am having a bit of trouble with understanding the ARFoundation API.
    Unlike the ARKit 2 API, which is nice and cozy, I don't get where I'm supposed to get to get any info about anything. If you could point me in the right direction, I'd be grateful forever (or for a year at least!)

    To start with. I need to track 4 different objects (i've got ARObjects from the Apple App) in a room and instantiate 4 different prefabs. How do I do that? Do I need multiple AR Sessions? Multiple Session Origins? Multiple TrackedObjectManagers?

    Then. I want to get the instantiated prefab. I couldn't even find the place in the API where it is being instantiated. Where can I get that?

    So. Now, where can I subscribe to the events of the tracked objects? I'd like to update the instantiated GameObject each time when the tracked Object is being moved. Can I get a) an event, and b) the delta.position or something from the tracked object?

    I want to achieve something pretty simple. With the ARKit 2 sample projects from bitbucket, it worked perfectly - and I understood the code. I am now searching for the equivalents of it in ARFoundation. I haven't found any good example projects or answers for my questions in the documentation. I'm pretty lost.

    Can you point me to the right directions? Thanks <3

    Below is the arkit2 code, just for reference to what I mean.

    Code (CSharp):
    1. // CODE FROM ARKIT 2
    2. // This is so simple, I love it. What are the equivalents of this in ARFoundation.
    3.  
    4.     [SerializeField]
    5.     private ARReferenceObjectAsset referenceObjectAsset;
    6.  
    7.     [SerializeField]
    8.     private GameObject prefabToGenerate;
    9.  
    10.     private GameObject objectAnchorGO;
    11.  
    12.     // THOSE EVENTS
    13.     void Start () {
    14.         UnityARSessionNativeInterface.ARObjectAnchorAddedEvent += AddObjectAnchor;
    15.         UnityARSessionNativeInterface.ARObjectAnchorUpdatedEvent += UpdateObjectAnchor;
    16.         UnityARSessionNativeInterface.ARObjectAnchorRemovedEvent += RemoveObjectAnchor;
    17.  
    18.     }
    19.  
    20.     void AddObjectAnchor(ARObjectAnchor arObjectAnchor)
    21.     {
    22.         Debug.Log ("object anchor added");
    23.         if (arObjectAnchor.referenceObjectName == referenceObjectAsset.objectName) {
    24.             Vector3 position = UnityARMatrixOps.GetPosition (arObjectAnchor.transform);
    25.             Quaternion rotation = UnityARMatrixOps.GetRotation (arObjectAnchor.transform);
    26.  
    27.             // THIS REFERENCE
    28.             objectAnchorGO = Instantiate<GameObject> (prefabToGenerate, position, rotation);
    29.         }
    30.     }
    31.  
    32.     //THIS
    33.     void UpdateObjectAnchor(ARObjectAnchor arObjectAnchor)
    34.     {
    35.         Debug.Log ("object anchor updated");
    36.         if (arObjectAnchor.referenceObjectName == referenceObjectAsset.objectName) {
    37.             objectAnchorGO.transform.position = UnityARMatrixOps.GetPosition (arObjectAnchor.transform);
    38.             objectAnchorGO.transform.rotation = UnityARMatrixOps.GetRotation (arObjectAnchor.transform);
    39.         }
    40.  
    41.     }