Search code examples
unity-game-engine

Suspending Unity Temporarily to do some math processing at the background


I am using unity with a custom library on iOS. The custom library is a mathematical lib with a few threads and calls to DSP functions of the iOS. If I use the library with a native iOS app the performance of the library is 2-3X better compared to running under unity. I suspect, unity is using a lot of CPU cycles and as a consequence the libraries execution time is longer.

Is there a way I can stop/pause/suspend Unity functions for a very limited time so that I can prioritize the library execution? I am ok to make the system unresponsive to user input and possibly stop frame rerest as well. If this is NOT possible, what other approaches I can take the enhance my performance.

Thx,


Solution

  • You can call UnityPause(true) from iOS to pause the unity player but this will literally "pause" your game and trigger OnApplicationPause(true) up in the Unity layer. We do this for a few functions that call native View Controllers.

    The declaration looks like this in Objective-C:

        // imports
        void UnityPause(bool pause);
    
        @implementation ClassName
        // Implementation stuff here
    

    You can call UnityPause from anywhere as long as it's declared. But keep in mind that if you have any functionality tied to the OnApplicationPause event in Unity, it will be carried out.

    If you need to then pass data back to Unity, you can create a callback from Native to Unity that sends the data when fired, but the player will obviously have to be running for the callback to be "heard".

    The other option (which we have used as a sort of SDK) is to create a Bridge whose job is to get calls and push callbacks to a C# class. The C# class would be a MonoBehaviour whose job it is to receive the data and get it to Unity on the Update Loop or a coroutine somehow. I cannot share the code we use to do this, but I can tell you that it is possible.