-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUnityAdsManager.cs
154 lines (127 loc) · 3.53 KB
/
UnityAdsManager.cs
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
using UnityEngine;
using UnityEngine.Advertisements;
using System.Collections;
public class UnityAdsManager : MonoBehaviour, IUnityAdsListener
{
public string appId;
public string bannerId;
public string interstitialId;
public string rewardedVideoId;
public bool testMode;
public bool IsRewardedVideoReady { get; set; }
public bool statusAds = true;
private int countInterstitial;
public static UnityAdsManager Instance;
private void Awake()
{
if (Instance != null)
Destroy(gameObject);
else
Instance = this;
DontDestroyOnLoad(gameObject);
}
private void Start()
{
Advertisement.Initialize(appId, testMode);
Advertisement.AddListener(this);
}
IEnumerator WaitForAd()
{
while (!Advertisement.IsReady(appId))
{
yield return null;
}
}
public void RequestBanner()
{
if (statusAds)
{
ShowBanner();
}
}
public void RequestInterstitial()
{
if (Advertisement.IsReady(interstitialId))
{
Advertisement.Show(interstitialId);
}
}
public void RequestRewardBasedVideo()
{
if (Advertisement.IsReady(rewardedVideoId))
{
IsRewardedVideoReady = true;
}
}
IEnumerator ShowBannerWhenReady()
{
while (!Advertisement.IsReady("banner"))
{
yield return new WaitForSeconds(0.5f);
}
Advertisement.Banner.Show(bannerId);
}
public void ShowRewardBasedAd()
{
if (statusAds)
{
Advertisement.Show(rewardedVideoId);
}
}
public void ShowBanner()
{
if (statusAds)
{
StartCoroutine(ShowBannerWhenReady());
}
}
public void DestroyBanner()
{
Advertisement.Banner.Hide();
}
public void HideBanner()
{
Advertisement.Banner.Hide();
}
public void OnUnityAdsDidFinish(string placementId, ShowResult showResult)
{
// Define conditional logic for each ad completion status:
if (showResult == ShowResult.Finished)
{
// Reward the user for watching the ad to completion.
IsRewardedVideoReady = false;
RequestRewardBasedVideo();
}
else if (showResult == ShowResult.Skipped)
{
// Do not reward the user for skipping the ad.
IsRewardedVideoReady = false;
this.RequestRewardBasedVideo();
}
else if (showResult == ShowResult.Failed)
{
Debug.LogWarning("The ad did not finish due to an error.");
}
}
public void OnUnityAdsReady(string placementId)
{
// If the ready Placement is rewarded, show the ad:
if (placementId == rewardedVideoId)
{
// Optional actions to take when the placement becomes ready(For example, enable the rewarded ads button)
}
}
public void OnUnityAdsDidError(string message)
{
// Log the error.
}
public void OnUnityAdsDidStart(string placementId)
{
// Optional actions to take when the end-users triggers an ad.
}
// When the object that subscribes to ad events is destroyed, remove the listener:
public void OnDestroy()
{
Advertisement.RemoveListener(this);
}
}