-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathUnityAds.cs
More file actions
104 lines (90 loc) · 2.61 KB
/
UnityAds.cs
File metadata and controls
104 lines (90 loc) · 2.61 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
102
103
104
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;
public class UnityAds : MonoBehaviour, IUnityAdsListener
{
private string gameID = "";
private string bannerID = "";
private string interstitialID = "";
private string rewardedVideoID = "rewardedVideo";
public bool TestMode;
public Button showInterstitial;
public Button watchRewardAd;
public Text gemsAmt;
void Start()
{
Advertisement.Initialize(gameID, TestMode);
showInterstitial.interactable = Advertisement.IsReady(interstitialID);
watchRewardAd.interactable = Advertisement.IsReady(rewardedVideoID);
Advertisement.AddListener(this);
}
public void ShowInterstitial()
{
if (Advertisement.IsReady(interstitialID))
{
Advertisement.Show(interstitialID);
}
}
public void ShowRewardedVideo()
{
Advertisement.Show(rewardedVideoID);
}
public void HideBanner()
{
Advertisement.Banner.Hide();
}
public void OnUnityAdsReady(string placementID)
{
if (placementID == rewardedVideoID)
{
watchRewardAd.interactable = true;
}
if (placementID == interstitialID)
{
showInterstitial.interactable = true;
}
if (placementID == bannerID)
{
Advertisement.Banner.SetPosition(BannerPosition.BOTTOM_CENTER);
Advertisement.Banner.Show(bannerID);
}
}
public void OnUnityAdsDidFinish(string placementID, ShowResult showResult)
{
if (placementID == rewardedVideoID)
{
if (showResult == ShowResult.Finished)
{
GetReward();
}
else if (showResult == ShowResult.Skipped)
{
//Do nothing
}
else if (showResult == ShowResult.Failed)
{
//tell player ads failed
}
}
}
public void OnUnityAdsDidError(string message)
{
//Show or log the error here
}
public void OnUnityAdsDidStart(string placementID)
{
//Do this if ads starts
}
public void GetReward()
{
if (PlayerPrefs.HasKey("gems"))
{
int gemAmount = PlayerPrefs.GetInt("gems");
PlayerPrefs.SetInt("gems", gemAmount + 50);
} else
{
PlayerPrefs.SetInt("gems", 50);
}
gemsAmt.text = "Gems: " + PlayerPrefs.GetInt("gems").ToString();
}
}