Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Question iOS plugin: How to call into Unity from a background service plugin?

Discussion in 'iOS and tvOS' started by waldgeist, Sep 12, 2023.

  1. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    357
    Hi, I am working on a Unity plugin that handles background uploads of photos and videos, so the app does not have to be active while the files are being transferred.

    The upload to S3 works fine, and iOS successfully calls my app delegate once the background tasks are finished. I assumed that I could call UnitySendMessage now, to perform follow-up tasks in the managed C# (Unity) code. This would keep the native part as slim as possible (I have to support Android, too).

    However, the UnitySendMessage call seems to be delayed until the user brings the app into the foreground again. Why is this so, and is there a way to get around this limitation?
     
  2. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    You can use reverse P/Invoke for that (search for MonoPInvokeCallback).
     
    waldgeist likes this.
  3. waldgeist

    waldgeist

    Joined:
    May 6, 2017
    Posts:
    357
    Thanks. Does it work with IL2CPP?

    And if I call into C# instances, how long can their C# methods run before they will be suspended?
     
  4. Aurimas-Cernius

    Aurimas-Cernius

    Unity Technologies

    Joined:
    Jul 31, 2013
    Posts:
    3,632
    Yes, just with limitation that the method you call must be static.

    Nothing from Unity side here, it's your Objective-C/Swift code calling C++ code.
    One more note - Unity is suspended, so don't expect anything Unity-related to work. But pure C# should work fine.
     
  5. r618

    r618

    Joined:
    Jan 19, 2009
    Posts:
    1,271
    you can/should also call
    UnityBatchPlayerLoop()
    on iOS which is exposed on ObjC++ side - this is useful for stuff like advancing coroutines (so the whole scenario is bit easier to handle)
    alas, i have no idea if something similar is available/possible on Android @Aurimas-Cernius