Search Unity

  1. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Question How to resume MARS face tracking when app moves from background to foreground

Discussion in 'Unity MARS' started by SpillYerLungs, Apr 13, 2022.

  1. SpillYerLungs

    SpillYerLungs

    Joined:
    May 13, 2020
    Posts:
    4
    Hello! I'm developing a mobile game that uses MARS face tracking, allowing users to tilt their head and close their eyes as inputs.

    I'm running into an issue testing on device:
    If the app moves to the background (the user locks their device screen, switches between apps, takes a phone call), upon returning to the app, the MARS Face Mask seems to get stuck, with all transform values frozen where they were when the app became inactive.
    However, the EyeClosed/EyeOpened actions are still being triggered as expected.

    I've tried utilizing OnApplicationPause with various combinations of solutions from other forum posts (https://forum.unity.com/threads/turn-off-mars-camera.1044880/, https://forum.unity.com/threads/reset-mars.979788/), but it seems like the only reliable solution is to totally reload the scene, which is not desirable.

    Is there a proper way to resume a MARS face tracking session after the app returns to the foreground, without having to reload the scene?

    Here's my code with various attempts commented out:

    Code (CSharp):
    1. public class MarsSessionController : Singleton<MarsSessionController>, IUsesFunctionalityInjection, IUsesSessionControl//, IUsesPointCloud, IUsesPlaneFinding, IUsesMarkerTracking, IUsesFaceTracking
    2.     {
    3.         public bool IsActive { get { return !MARSCore.instance.paused; } }
    4.         public Transform FaceRoot { get { return faceRoot; } }
    5.         public Transform MarsCameraTransform { get { return marsCam.transform; } }
    6.  
    7.         [SerializeField] private GameObject marsSystems = null;
    8.         [SerializeField] private Transform faceRoot = null;
    9.         [SerializeField] private MARSCamera marsCam = null;
    10.  
    11.         IProvidesFunctionalityInjection IFunctionalitySubscriber<IProvidesFunctionalityInjection>.provider { get; set; }
    12.         IProvidesSessionControl IFunctionalitySubscriber<IProvidesSessionControl>.provider { get; set; }
    13.         //IProvidesPointCloud IFunctionalitySubscriber<IProvidesPointCloud>.provider { get; set; }
    14.         //IProvidesPlaneFinding IFunctionalitySubscriber<IProvidesPlaneFinding>.provider { get; set; }
    15.         //IProvidesMarkerTracking IFunctionalitySubscriber<IProvidesMarkerTracking>.provider { get; set; }
    16.         //IProvidesFaceTracking IFunctionalitySubscriber<IProvidesFaceTracking>.provider { get; set; }
    17.  
    18.         protected override void Awake()
    19.         {
    20.             base.Awake();
    21.  
    22.             // Only necessary if this script isn't already in the scene when you press Play
    23.             this.EnsureFunctionalityInjected();
    24.         }
    25.  
    26.         public void InitializeMars()
    27.         {
    28.             if (marsSystems.activeSelf == false)
    29.             {
    30.                 marsSystems.SetActive(true);
    31.             }
    32.         }
    33.  
    34.         private void OnApplicationPause(bool pause)
    35.         {
    36.             if (pause)
    37.             {
    38.                 PauseMars();
    39.             }
    40.             else
    41.             {
    42.                 ResumeMars();
    43.             }
    44.         }
    45.  
    46.         public void PauseMars()
    47.         {
    48.             if (MARSCore.instance.paused == false)
    49.             {
    50.                 MARSCore.instance.paused = true;
    51.  
    52.                 //if (this.HasProvider<IProvidesPointCloud>())
    53.                 //{
    54.                 //    this.StopDetectingPoints();
    55.                 //}
    56.                 //if (this.HasProvider<IProvidesPlaneFinding>())
    57.                 //{
    58.                 //    this.StopDetectingPlanes();
    59.                 //}
    60.                 //if (this.HasProvider<IProvidesMarkerTracking>())
    61.                 //{
    62.                 //    this.StopTrackingMarkers();
    63.                 //}
    64.  
    65.                 //if (this.HasProvider<IProvidesFaceTracking>())
    66.                 //{
    67.                 //    // ???
    68.                 //}
    69.  
    70.                 this.PauseSession();
    71.             }
    72.         }
    73.  
    74.         public void ResumeMars()
    75.         {
    76.             if (MARSCore.instance.paused)
    77.             {
    78.                 MARSCore.instance.paused = false;
    79.  
    80.                 //if (this.HasProvider<IProvidesPointCloud>())
    81.                 //{
    82.                 //    this.StartDetectingPoints();
    83.                 //}
    84.                 //if (this.HasProvider<IProvidesPlaneFinding>())
    85.                 //{
    86.                 //    this.StartDetectingPlanes();
    87.                 //}
    88.                 //if (this.HasProvider<IProvidesMarkerTracking>())
    89.                 //{
    90.                 //    this.StartTrackingMarkers();
    91.                 //}
    92.  
    93.                 //this.ResumeSession();
    94.  
    95.                 this.ResetSession();
    96.             }
    97.         }
    98.     }
    99.  
     
    Last edited: Apr 13, 2022
  2. CiaranWills

    CiaranWills

    Unity Technologies

    Joined:
    Apr 24, 2020
    Posts:
    198
    Which platform is this on (Android/iOS)?
     
  3. SpillYerLungs

    SpillYerLungs

    Joined:
    May 13, 2020
    Posts:
    4
    It's happening on iOS only; seems to be fine on Android.
     
  4. lukesionkirax

    lukesionkirax

    Joined:
    Jul 6, 2022
    Posts:
    1
    Fixed it by setting the maximum number of faces tracked.
     
  5. vinaymeena

    vinaymeena

    Joined:
    Mar 4, 2022
    Posts:
    2
    @SpillYerLungs Did you find any solution for the issue?

    @CiaranWills Do you have any solution for below scenario?

    Actually, I'm also facing the same issue. Face Tracking is not working.

    Scenario:

    Working on a TryOn Feature using MARS.
    1. Captured a screenshot by placing some jewels on Face and shared the screenshot using some 3rd party share plugin.
    2. Chose email to share and it brings my app in background and moves to email app.
    3. When coming back to my app, Face tracking is not working or Jewels are not tracking the face Whereas it was tracking before moving to background.
     
    Last edited: Sep 29, 2023
  6. vinaymeena

    vinaymeena

    Joined:
    Mar 4, 2022
    Posts:
    2
    Is there any specific number?
    I've tried "-1" for platform maximum and only "1" also but facing the same.