-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathRewarded.cs
More file actions
101 lines (87 loc) · 2.63 KB
/
Rewarded.cs
File metadata and controls
101 lines (87 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
using System;
using GoogleMobileAds.Api;
using UnityEngine;
using UnityEngine.UI;
using GoogleMobileAds.Common;
public class Rewarded : MonoBehaviour
{
RewardedAd rewardAd;
public Button watchAd;
string rewardId;
void Start()
{
RequestRewardedAd();
}
//make button interactable if video ad is ready
/*void Update()
{
if (rewardAd.IsLoaded())
{
watchAd.interactable = true;
}
}*/
void RequestRewardedAd()
{
#if UNITY_ANDROID
rewardId = "ca-app-pub-3940256099942544/5224354917";
#elif UNITY_IPHONE
rewardId = "ca-app-pub-3940256099942544/5224354917";
#else
rewardId = null;
#endif
rewardAd = new RewardedAd(rewardId);
//call events
rewardAd.OnAdLoaded += HandleRewardAdLoaded;
rewardAd.OnAdFailedToLoad += HandleRewardAdFailedToLoad;
rewardAd.OnAdOpening += HandleRewardAdOpening;
rewardAd.OnAdFailedToShow += HandleRewardAdFailedToShow;
rewardAd.OnUserEarnedReward += HandleUserEarnedReward;
rewardAd.OnAdClosed += HandleRewardAdClosed;
//create and ad request
if (PlayerPrefs.HasKey("Consent"))
{
AdRequest request = new AdRequest.Builder().Build();
rewardAd.LoadAd(request); //load & show the banner ad
} else
{
AdRequest request = new AdRequest.Builder().AddExtra("npa", "1").Build();
rewardAd.LoadAd(request); //load & show the banner ad (non-personalised)
}
}
//attach to a button that plays ad if ready
public void ShowRewardedAd()
{
if (rewardAd.IsLoaded())
{
rewardAd.Show();
}
}
//call events
public void HandleRewardAdLoaded(object sender, EventArgs args)
{
//do this when ad loads
}
public void HandleRewardAdFailedToLoad(object sender, AdErrorEventArgs args)
{
//do this when ad fails to loads
Debug.Log("Ad failed to load" + args.Message);
}
public void HandleRewardAdOpening(object sender, EventArgs args)
{
//do this when ad is opening
}
public void HandleRewardAdFailedToShow(object sender, AdErrorEventArgs args)
{
//do this when ad fails to show
}
public void HandleUserEarnedReward(object sender, EventArgs args)
{
//reward the player here
//RevivePlayer();
}
public void HandleRewardAdClosed(object sender, EventArgs args)
{
//do this when ad is closed
RequestRewardedAd();
}
}