-
Notifications
You must be signed in to change notification settings - Fork 0
/
AdsController.cs
195 lines (159 loc) · 5.38 KB
/
AdsController.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.SceneManagement;
using ChartboostSDK;
public class AdsController : MonoBehaviour {
public static AdsController instance;
private const string app_id = "1533311";
public bool canShowChartboostInterstitial;
public bool canShowChartboostVideo;
void Awake() {
MakeSingleton();
if(!canShowChartboostInterstitial) {
LoadChartboostInterstitialAds();
}
if(!canShowChartboostVideo) {
LoadChartboostVideoAds();
}
LoadUnityAds();
}
void OnLevelWasLoaded() {
if(SceneManager.GetActiveScene().name == "Gameplay" || SceneManager.GetActiveScene().name == "Challenges Gameplay") {
MasterControl.instance.numberOfAds++;
int x = MasterControl.instance.numberOfAds % 4;
//Debug.Log("Modulus operator is: " + x);
if(MasterControl.instance.numberOfAds == 1 || x == 0) {
if(MasterControl.instance.canShowAds > 0) {
//Show ad
//Debug.Log("You will see an ad here.");
if(canShowChartboostInterstitial) {
ShowChartboostInterstitial();
} else {
LoadChartboostInterstitialAds();
}
} else {
//Debug.Log("No ad shown. Make sure the no-ad option is bought. if so, THANK YOU FOR YOUR PURCHASE! :D");
}
}
}
}
void MakeSingleton() {
if(instance != null) {
Destroy(gameObject);
} else {
instance = this;
DontDestroyOnLoad(gameObject);
}
}
void OnEnable() {
Chartboost.didCompleteRewardedVideo += VideoCompleted;
Chartboost.didCacheInterstitial += DidCacheInterstital;
Chartboost.didDismissInterstitial += DidDismissInterstitial;
Chartboost.didCloseInterstitial += DidCloseInterstitial;
Chartboost.didCacheRewardedVideo += DidCacheVideo;
Chartboost.didFailToLoadInterstitial += FailedToLoadInterstitial;
Chartboost.didFailToLoadRewardedVideo += FailedToLoadVideo;
}
void OnDisable() {
Chartboost.didCompleteRewardedVideo -= VideoCompleted;
Chartboost.didCacheInterstitial -= DidCacheInterstital;
Chartboost.didDismissInterstitial -= DidDismissInterstitial;
Chartboost.didCloseInterstitial -= DidCloseInterstitial;
Chartboost.didCacheRewardedVideo -= DidCacheVideo;
Chartboost.didFailToLoadInterstitial -= FailedToLoadInterstitial;
Chartboost.didFailToLoadRewardedVideo -= FailedToLoadVideo;
}
public void VideoCompleted(CBLocation location, int reward) {
canShowChartboostVideo = false;
GameObject g = GameObject.Find("Store Controller");
if(g != null) {
g.GetComponent<StoreController>().WatchedVideoGiveAReward();
}
LoadChartboostVideoAds();
}
public void DidCacheInterstital(CBLocation location) {
canShowChartboostInterstitial = true;
}
public void DidDismissInterstitial(CBLocation arg1) {
canShowChartboostInterstitial = false;
LoadChartboostVideoAds();
}
public void DidCloseInterstitial(CBLocation arg1) {
canShowChartboostInterstitial = false;
LoadChartboostVideoAds();
}
public void DidCacheVideo(CBLocation location) {
canShowChartboostVideo = true;
}
void FailedToLoadInterstitial(CBLocation arg1, CBImpressionError arg2) {
canShowChartboostInterstitial = false;
LoadChartboostInterstitialAds();
}
void FailedToLoadVideo(CBLocation arg1, CBImpressionError arg2) {
canShowChartboostVideo = false;
GameObject g = GameObject.Find("Store Controller");
if(g != null) {
g.GetComponent<StoreController>().FailedToLoadTheVideo();
}
LoadChartboostVideoAds();
}
public void LoadChartboostVideoAds() {
Chartboost.cacheRewardedVideo(CBLocation.Default);
}
public void LoadChartboostInterstitialAds() {
Chartboost.cacheInterstitial(CBLocation.Default);
}
public void ShowChartboostInterstitial() {
if(canShowChartboostInterstitial) {
Chartboost.showInterstitial(CBLocation.Default);
} else {
LoadChartboostInterstitialAds();
}
}
public void ShowChartboostChartboostVideo() {
if(canShowChartboostVideo) {
Chartboost.showRewardedVideo(CBLocation.Default);
} else {
LoadChartboostVideoAds();
}
}
public void LoadUnityAds() {
if(Advertisement.isSupported) {
//Advertisement.allowPrecache = true;
Advertisement.Initialize(app_id, false); //Change to false when game is in production!
}
}
//Temporary -- use this in the game (Gameplay/Challenges Gameplay) itself to give extra lives or coins?
public void ShowUnityAds() {
if(Advertisement.IsReady()) {
GameObject g = GameObject.Find("Emergency Store Controller");
Advertisement.Show(null, new ShowOptions() {
//pause = true,
resultCallback = result => {
switch(result) {
case ShowResult.Finished:
g.GetComponent<EmergencyStoreController>().VideoWatchedGiveBonusCoins();
LoadUnityAds();
break;
case ShowResult.Failed:
g.GetComponent<EmergencyStoreController>().VideoNotLoadedOrUserSkippedTheVideo();
LoadUnityAds();
break;
case ShowResult.Skipped:
g.GetComponent<EmergencyStoreController>().VideoNotLoadedOrUserSkippedTheVideo();
LoadUnityAds();
break;
}
}
});
} else {
GameObject g = GameObject.Find("Emergency Store Controller");
if(g != null) {
g.GetComponent<EmergencyStoreController>().VideoNotLoadedOrUserSkippedTheVideo();
}
LoadUnityAds();
}
}
}