Question by
joe_jai001 · Jul 06, 2020 at 10:35 AM ·
c#unity 5script.admob
May I ask why scrpt cannot be triggered after watching the advertisement?,May I ask why [HandleOnAdRewarded] cannot be triggered after watching the advertisement?
May I ask why [HandleOnAdRewarded] cannot be triggered after watching the advertisement?
this is script using UnityEngine; using GoogleMobileAds.Api; using System; using UnityEngine.UI; //reward ad
public class ad : MonoBehaviour { private RewardBasedVideoAd adReward;
private string idApp, idReward;
[SerializeField] Button BtnReward;
[SerializeField] Text TxtPoints;
public Text text2;
void Start ()
{
idApp = "ca-app-pub-3940256099942544~3347511713";
idReward = "ca-app-pub-3940256099942544/5224354917";
adReward = RewardBasedVideoAd.Instance;
MobileAds.Initialize (idApp);
}
#region Reward video methods ---------------------------------------------
public void RequestRewardAd ()
{
AdRequest request = AdRequestBuild ();
adReward.LoadAd (request, idReward);
adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded += this.HandleOnAdRewarded;
adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
}
public void ShowRewardAd ()
{
if (adReward.IsLoaded ())
adReward.Show ();
}
//events
public void HandleOnRewardedAdLoaded (object sender, EventArgs args)
{//ad loaded
ShowRewardAd ();
}
public void HandleOnAdRewarded (object sender, EventArgs args)
{//user finished watching ad
int points = int.Parse (TxtPoints.text);
points += 50; //add 50 points
text2.text = ("5");
TxtPoints.text = points.ToString ();
var coint = GameObject.Find("CoinManager").GetComponent<CoinManager>();
coint.AddCoint(coint.scord);
}
public void HandleOnRewardedAdClosed (object sender, EventArgs args)
{//ad closed (even if not finished watching)
BtnReward.interactable = true;
BtnReward.GetComponentInChildren <Text> ().text = "More Points";
adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded -= this.HandleOnAdRewarded;
adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
#endregion
//other functions
//btn (more points) clicked
public void OnGetMorePointsClicked ()
{
BtnReward.interactable = false;
BtnReward.GetComponentInChildren <Text> ().text = "Loading...";
RequestRewardAd ();
}
//------------------------------------------------------------------------
AdRequest AdRequestBuild ()
{
return new AdRequest.Builder ().Build ();
}
void OnDestroy ()
{
adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded -= this.HandleOnAdRewarded;
adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
} , May I ask why [HandleOnAdRewarded] cannot be triggered after watching the advertisement? this is script
//reward ad public class ad : MonoBehaviour { private RewardBasedVideoAd adReward;
private string idApp, idReward;
[SerializeField] Button BtnReward;
[SerializeField] Text TxtPoints;
public Text text2;
void Start ()
{
idApp = "";
idReward = "ca-app-pub-3940256099942544/5224354917";
adReward = RewardBasedVideoAd.Instance;
MobileAds.Initialize (idApp);
}
#region Reward video methods ---------------------------------------------
public void RequestRewardAd ()
{
AdRequest request = AdRequestBuild ();
adReward.LoadAd (request, idReward);
adReward.OnAdLoaded += this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded += this.HandleOnAdRewarded;
adReward.OnAdClosed += this.HandleOnRewardedAdClosed;
}
public void ShowRewardAd ()
{
if (adReward.IsLoaded ())
adReward.Show ();
}
//events
public void HandleOnRewardedAdLoaded (object sender, EventArgs args)
{//ad loaded
ShowRewardAd ();
}
public void HandleOnAdRewarded (object sender, EventArgs args)
{//user finished watching ad
int points = int.Parse (TxtPoints.text);
points += 50; //add 50 points
text2.text = ("5");
TxtPoints.text = points.ToString ();
var coint = GameObject.Find("CoinManager").GetComponent<CoinManager>();
coint.AddCoint(coint.scord);
}
public void HandleOnRewardedAdClosed (object sender, EventArgs args)
{//ad closed (even if not finished watching)
BtnReward.interactable = true;
BtnReward.GetComponentInChildren <Text> ().text = "More Points";
adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded -= this.HandleOnAdRewarded;
adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
#endregion
//other functions
//btn (more points) clicked
public void OnGetMorePointsClicked ()
{
BtnReward.interactable = false;
BtnReward.GetComponentInChildren <Text> ().text = "Loading...";
RequestRewardAd ();
}
//------------------------------------------------------------------------
AdRequest AdRequestBuild ()
{
return new AdRequest.Builder ().Build ();
}
void OnDestroy ()
{
adReward.OnAdLoaded -= this.HandleOnRewardedAdLoaded;
adReward.OnAdRewarded -= this.HandleOnAdRewarded;
adReward.OnAdClosed -= this.HandleOnRewardedAdClosed;
}
}
Comment