-
Notifications
You must be signed in to change notification settings - Fork 1
/
AdManager.cs
153 lines (133 loc) · 4.29 KB
/
AdManager.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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System;
using AudienceNetwork.Utility;
using AudienceNetwork;
using UnityEngine.UI;
public class AdManager : MonoBehaviour
{
public Transform coffee_table;
public GameObject prefab_coffee;
public GameObject window_coffee;
public GameObject window_ordering;
private RewardedVideoAd rewardedVideoAd;
private bool isLoaded;
private bool didClose;
// UI elements in scene
public Text statusLabel;
private void Start()
{
if (!AdUtility.IsInitialized())
{
AdUtility.Initialize();
}
}
public void ShowAd()
{
LoadRewardedVideo();
}
private void Update()
{
if (window_coffee.activeSelf && Input.GetKeyDown(KeyCode.Escape)) BTN_Leave();
}
public void LoadRewardedVideo()
{
statusLabel.text = "Loading rewardedVideo ad...";
rewardedVideoAd = new RewardedVideoAd("378297562859739_378307472858748");
rewardedVideoAd.Register(gameObject);
rewardedVideoAd.RewardedVideoAdDidLoad = delegate ()
{
Debug.Log("RewardedVideo ad loaded.");
isLoaded = true;
didClose = false;
string isAdValid = rewardedVideoAd.IsValid() ? "valid" : "invalid";
statusLabel.text = "Ad loaded and is " + isAdValid + ". Click show to present!";
rewardedVideoAd.Show();
};
rewardedVideoAd.RewardedVideoAdDidFailWithError = delegate (string error)
{
Debug.Log("RewardedVideo ad failed to load with error: " + error);
SetStatusOrderingWindow(false);
};
rewardedVideoAd.RewardedVideoAdWillLogImpression = delegate ()
{
AdGetReward();
};
rewardedVideoAd.RewardedVideoAdDidClick = delegate ()
{
AdGetReward();
};
rewardedVideoAd.RewardedVideoAdDidFail = delegate ()
{
Debug.Log("Rewarded video ad not validated, or no response from server");
SetStatusOrderingWindow(false);
};
rewardedVideoAd.RewardedVideoAdDidClose = delegate ()
{
Debug.Log("Rewarded video ad did close.");
didClose = true;
SetStatusOrderingWindow(false);
};
if (Application.platform == RuntimePlatform.Android)
{
rewardedVideoAd.RewardedVideoAdActivityDestroyed = delegate ()
{
if (!didClose)
{
Debug.Log("Rewarded video activity destroyed without being closed first.");
Debug.Log("Game should resume. User should not get a reward.");
}
};
}
rewardedVideoAd.LoadAd();
}
private void SetStatusOrderingWindow(bool status)
{
if (!status && rewardedVideoAd != null) rewardedVideoAd.Dispose();
window_ordering.SetActive(status);
}
public void AdGetReward()
{
PlayerPrefs.SetInt("Coffee", PlayerPrefs.GetInt("Coffee") + 1);
SetStatusOrderingWindow(false);
RefreshCoffees();
}
public void RefreshCoffees()
{
if (coffee_table.childCount > 0) foreach (Transform t in coffee_table) Destroy(t.gameObject);
StartCoroutine(PutCoffee());
}
private IEnumerator PutCoffee()
{
var coffeeN = PlayerPrefs.GetInt("Coffee");
for (var x = 0; x < coffeeN; x++)
{
if (!window_coffee.activeSelf) break; //Parar de colocar tazas si cierras la ventana.
var go_t = Instantiate(prefab_coffee).transform;
go_t.SetParent(coffee_table);
int fila = x / 6;
go_t.localPosition = new Vector3(-180 + 70 * (x - fila * 6), 100 - 60 * fila, 0);
yield return new WaitForSecondsRealtime(0.2f);
}
}
public void BTN_OpenCoffeeWindow()
{
window_coffee.SetActive(true);
RefreshCoffees();
}
public void CancelMyCoffee()
{
SetStatusOrderingWindow(false);
}
public void BTN_TakeOne()
{
SetStatusOrderingWindow(true);
ShowAd();
}
public void BTN_Leave()
{
if (window_ordering.activeSelf) SetStatusOrderingWindow(false);
window_coffee.SetActive(false);
}
}