How do I pass in a callback delegate from another class?
I'm trying to implement Unity Rewarded video ads. I watched the video by Mike Geig, and he mentions that it would be a good approach to pass in a delegate to be called by the Advertisement.Show callback method, which makes sense to me. Unfortunately I don't have much experience with delegates and I can't understand the error this throws. I'm able to pass in a delegate method from the same "AdShower" class, but not from my external class.
The error is: Cannot implicitly convert type 'AdShower.ResultDelegate' to 'System.Action< UnityEngine.Advertisements.ShowResult>'
using UnityEngine;
using System.Collections;
using UnityEngine.Advertisements;
public class AdShower : MonoBehaviour
{
private string gameID = "";
private string rewardedPlacementID = "rewardedVideo";
public delegate void ResultDelegate (ShowResult result);
void Awake()
{
Advertisement.Initialize (gameID, true);
}
public bool RewardedAdIsReady()
{
return Advertisement.IsReady (rewardedPlacementID);
}
public void TryShowRewardedAd(ResultDelegate theDelegate)
{
ShowOptions options = new ShowOptions ();
options.resultCallback = theDelegate;
if (Advertisement.IsReady (rewardedPlacementID))
Advertisement.Show (rewardedPlacementID, options);
}
}
Here is a snippet from the other class I want to call the "TryShowRewardedAd" Method:
public void VideoContinueButtonPressed()
{
// already confirmed a video is ready
r.adShower.TryShowRewardedAd(HandleVideoResult);
}
private void HandleVideoResult(ShowResult result)
{
if (result == ShowResult.Finished)
{
Continue();
}
}
Answer by maciejhd · Oct 27, 2020 at 11:55 AM
class A {
public void Show(string placementId, Action<ShowResult> callback)
{
var options = new ShowOptions { resultCallback = callback };
Advertisement.Show(placementId, options);
}
}
class B {
public void WatchAd()
{
A.Show('placement_id', HandleAdResult);
}
private void HandleAdResult(ShowResult showResult)
{
switch (showResult)
{
case ShowResult.Finished:
break;
case ShowResult.Skipped:
break;
case ShowResult.Failed:
break;
}
}
}
Your answer
Follow this Question
Related Questions
HoloLens Callbacks with Native Library 1 Answer
ISerializationCallbackReceiver - total confusion or simply unstable? 0 Answers
Trying to loop through a generic list -- need help. 0 Answers
Unity IAP Help, Cannot Figure Out :( 0 Answers
My Advertisement is not show in my Apps - Only the "Personalized Placement Test" 2 Answers