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:

public class MyBehaviour : MonoBehaviour, IRewardedVideoAdListener
{
	bool videoFinished = false;
	double rewardAmount;
	string rewardName;
	public void onRewardedVideoFinished(double amount, string name) 
	{ 
		rewardAmount = amount;
		rewardName = name;
		videoFinished = true; //it's important to set flag to true only  after all required parameters
	}

	//update method always performs in the main Unity thread
	void Update()
	{
		if(videoFinished){
			//do something with rewardAmount and rewardName
			videoFinished = false; //don't forget to set flag to false
		}
	}
	…
}

Other, maybe more comfortable way is to use UnityMainThreadDispatcher. To use it:

  1. Download script and prefab
  2. Import downloaded files to your project
  3. Add UnityMainThreadDispatcher.prefab to your scene (or to all scenes, where you want to make UI changes after Appodeal callbacks)
  4. Use UnityMainThreadDispatcher.Instance().Enqueue() method to perform changes:

public void onRewardedVideoFinished(double amount, string name) 
{ 
	UnityMainThreadDispatcher.Instance().Enqueue(()=>{
		print("Appodeal. Video Finished: " + amount + " " + name);
		
	});
}

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.