Run callbacks in Main Unity Thread
Callbacks in Appodeal plugin are executed in the main Android or iOS thread (not in the main Unity thread). What does it mean for you? It’s not recommended to perform any UI changes (change colours, positions, sizes, texts and so on) directly in our callback functions.
It's important to understand that it's not possible to receive callbacks in main thread while fullscreen advertising is shown. This is because Unity pause main thread if game scene is out of screen. That’s why your script will receive some callbacks (like onInterstitialShown or onRewardedVideoFinished) in main thread only after closing fullscreen advertising. But the same callbacks work without such delays in Android and iOS main thread.
So, how to react on Appodeal events to prevent multithreading problems? The simplest way is to use flags and Update method of MonoBehaviour class:
Other, maybe more comfortable way is to use UnityMainThreadDispatcher. To use it:
- Download script and prefab
- Import downloaded files to your project
- Add UnityMainThreadDispatcher.prefab to your scene (or to all scenes, where you want to make UI changes after Appodeal callbacks)
- Use UnityMainThreadDispatcher.Instance().Enqueue() method to perform changes:
And finally, the official way to send message to Unity Main thread is UnitySendMessage. It’s platform dependent, so it’s required to make changes in Android native code and iOS native code. You can find more information in the official unity documentation for Android and iOS.