Search Unity

Question Basic Subscribe to Events Example

Discussion in 'Unity MARS' started by Realift, Nov 26, 2021.

  1. Realift

    Realift

    Joined:
    Oct 12, 2021
    Posts:
    9
    Honestly, I'm not sure how anybody is figuring out how to do anything with MARS as the docs are abhorrent! What would be great is some snippets in the docs like the normal scripting docs have. I spent the past week trying to using the scripting API and only got anything working based on a forum post I found here. I'm posting my code in hopes someone can show me how to subscribe (aka listen) to an event. The GetBodies is working as expected but both Unity and Visual Studio complain there is no SubscribeBodyAdded method despite it being listed in VS. Thanks in advance!

    Code (CSharp):
    1. using System;
    2. using System.Collections.Generic;
    3. using Unity.MARS.Data;
    4. using Unity.MARS.Providers;
    5. using Unity.XRTools.ModuleLoader;
    6. using UnityEngine;
    7. using UnityEngine.UI;
    8.  
    9. public class DynamicCameraFacingTest : MonoBehaviour, IUsesMarsBodyTracking, IUsesFunctionalityInjection
    10. {
    11.     List<IMarsBody> m_Bodies = new List<IMarsBody>();
    12.  
    13.     IProvidesMarsBodyTracking IFunctionalitySubscriber<IProvidesMarsBodyTracking>.provider { get; set; }
    14.  
    15.     IProvidesFunctionalityInjection IFunctionalitySubscriber<IProvidesFunctionalityInjection>.provider { get; set; }
    16.  
    17.     void Awake()
    18.     {
    19.         this.EnsureFunctionalityInjected();
    20.         this.SubscribeBodyAdded(OnBodyAdded);
    21.     }
    22.  
    23.     void Update()
    24.     {
    25.         this.GetBodies(m_Bodies);
    26.         Debug.Log(m_Bodies);
    27.     }
    28.  
    29.     void OnBodyAdded(Action<IMarsBody> action)
    30.     {
    31.  
    32.     }
    33. }
    34.  
     
  2. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    199
    Hi there,

    It's complaining because it wants an Action as the parameter, while OnBodyAdded() not an Action but rather a method taking an Action as a parameter. Change it to this and you should be good:
    Code (CSharp):
    1. void OnBodyAdded(IMarsBody body)
    2. {
    3.  
    4. }
    And I'm certainly not going to argue with you that the documentation could be better! We're working on that...
     
  3. Realift

    Realift

    Joined:
    Oct 12, 2021
    Posts:
    9
    Thanks Ciaran!

    I somehow figured that out before you replied. However, I'm curious if I should be seeing those events fire when running the Body Tracking simulation in MARS? I don't get any events to fire in the simulation, but when I build it to my phone I do get the events then. Is there additional configuration I need to do within the simulation?
     
  4. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    199
    You should get those events in simulation but you don't - that's an error on our part in the simulation environment, but it's easily fixable.

    If you look in Assets > MARS > Content > SimulationEnvironments > BodyTracking you should see the BodyTracking_Standing prefab that is the simulation environment.

    Open the prefab, find the Synthetic Body game object and add a Simulated Object component to it.
    upload_2021-12-1_21-31-1.png

    That should get the body events working. We'll get this fixed in future versions of the simulation environments.