ads added, closes #2
This commit is contained in:
158
Assets/Scripts/AdsManager.cs
Normal file
158
Assets/Scripts/AdsManager.cs
Normal file
@@ -0,0 +1,158 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using GoogleMobileAds.Api;
|
||||
using UnityEngine;
|
||||
|
||||
public class AdsManager : MonoBehaviour
|
||||
{
|
||||
// Start is called before the first frame update
|
||||
|
||||
public static AdsManager instance;
|
||||
void Awake(){
|
||||
if(instance!= null){Destroy(gameObject);}
|
||||
instance = this;
|
||||
}
|
||||
|
||||
const string intAdId = "ca-app-pub-3966734202864173/9071918772";
|
||||
const string rewardedAdId = "ca-app-pub-3966734202864173/7802288880";
|
||||
void Start()
|
||||
{
|
||||
DontDestroyOnLoad(gameObject);
|
||||
// Initialize the Google Mobile Ads SDK.
|
||||
MobileAds.Initialize(initStatus => { Debug.Log("admob Init status : " + initStatus.ToString());});
|
||||
|
||||
StartCoroutine(ReloadAds(true));
|
||||
}
|
||||
|
||||
private InterstitialAd interstitialAd;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the interstitial ad.
|
||||
/// </summary>
|
||||
public void LoadInterstitialAd()
|
||||
{
|
||||
// Clean up the old ad before loading a new one.
|
||||
if (interstitialAd != null)
|
||||
{
|
||||
interstitialAd.Destroy();
|
||||
interstitialAd = null;
|
||||
}
|
||||
|
||||
Debug.Log("Loading the interstitial ad.");
|
||||
|
||||
// create our request used to load the ad.
|
||||
var adRequest = new AdRequest.Builder()
|
||||
.AddKeyword("unity-admob-sample")
|
||||
.Build();
|
||||
|
||||
// send the request to load the ad.
|
||||
InterstitialAd.Load(intAdId, adRequest,
|
||||
(InterstitialAd ad, LoadAdError error) =>
|
||||
{
|
||||
// if error is not null, the load request failed.
|
||||
if (error != null || ad == null)
|
||||
{
|
||||
Debug.LogError("interstitial ad failed to load an ad " +
|
||||
"with error : " + error);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Interstitial ad loaded with response : "
|
||||
+ ad.GetResponseInfo());
|
||||
|
||||
interstitialAd = ad;
|
||||
});
|
||||
|
||||
|
||||
}
|
||||
|
||||
public void ShowIntAd()
|
||||
{
|
||||
if (interstitialAd != null && interstitialAd.CanShowAd())
|
||||
{
|
||||
Debug.Log("Showing interstitial ad.");
|
||||
interstitialAd.Show();
|
||||
}
|
||||
else
|
||||
{
|
||||
Debug.LogError("Interstitial ad is not ready yet.");
|
||||
}
|
||||
StartCoroutine(ReloadAds(false));
|
||||
|
||||
}
|
||||
|
||||
|
||||
private RewardedAd rewardedAd;
|
||||
|
||||
/// <summary>
|
||||
/// Loads the rewarded ad.
|
||||
/// </summary>
|
||||
public void LoadRewardedAd()
|
||||
{
|
||||
// Clean up the old ad before loading a new one.
|
||||
if (rewardedAd != null)
|
||||
{
|
||||
rewardedAd.Destroy();
|
||||
rewardedAd = null;
|
||||
}
|
||||
|
||||
Debug.Log("Loading the rewarded ad.");
|
||||
|
||||
// create our request used to load the ad.
|
||||
var adRequest = new AdRequest.Builder().Build();
|
||||
|
||||
// send the request to load the ad.
|
||||
RewardedAd.Load(rewardedAdId, adRequest,
|
||||
(RewardedAd ad, LoadAdError error) =>
|
||||
{
|
||||
// if error is not null, the load request failed.
|
||||
if (error != null || ad == null)
|
||||
{
|
||||
Debug.LogError("Rewarded ad failed to load an ad " +
|
||||
"with error : " + error);
|
||||
return;
|
||||
}
|
||||
|
||||
Debug.Log("Rewarded ad loaded with response : "
|
||||
+ ad.GetResponseInfo());
|
||||
|
||||
rewardedAd = ad;
|
||||
|
||||
rewardedAd.OnAdPaid += OnRewardSuccess;
|
||||
});
|
||||
}
|
||||
|
||||
private void OnRewardSuccess(AdValue obj)
|
||||
{
|
||||
// GameManager.AdWatched();
|
||||
}
|
||||
|
||||
public void ShowRewardedAd()
|
||||
{
|
||||
const string rewardMsg =
|
||||
"Rewarded ad rewarded the user. Type: {0}, amount: {1}.";
|
||||
|
||||
if (rewardedAd != null && rewardedAd.CanShowAd())
|
||||
{
|
||||
rewardedAd.Show((Reward reward) =>
|
||||
{
|
||||
// TODO: Reward the user.
|
||||
Debug.Log(String.Format(rewardMsg, reward.Type, reward.Amount));
|
||||
});
|
||||
}
|
||||
|
||||
StartCoroutine(ReloadAds(true));
|
||||
}
|
||||
|
||||
|
||||
IEnumerator ReloadAds(bool rewarded){
|
||||
yield return new WaitForSeconds(2);
|
||||
LoadInterstitialAd();
|
||||
if(rewarded){
|
||||
LoadRewardedAd();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
11
Assets/Scripts/AdsManager.cs.meta
Normal file
11
Assets/Scripts/AdsManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 51ce029fa474b514fb11d81ac6802e28
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -11,14 +11,14 @@ public class AudioManager : MonoBehaviour
|
||||
[SerializeField]public LevelAudioCombo[] levels;
|
||||
public static bool isMuteMusic {get{
|
||||
if(PlayerPrefs.HasKey("music")){
|
||||
return PlayerPrefs.GetInt("music") == 1;
|
||||
return PlayerPrefs.GetInt("music") == 0;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
}}
|
||||
public static bool isMuteSFX {get{
|
||||
if(PlayerPrefs.HasKey("sfx")){
|
||||
return PlayerPrefs.GetInt("sfx") == 1;
|
||||
return PlayerPrefs.GetInt("sfx") == 0;
|
||||
}else{
|
||||
return false;
|
||||
}
|
||||
@@ -26,21 +26,30 @@ public class AudioManager : MonoBehaviour
|
||||
public static void ToggleMusic(){
|
||||
if(isMuteMusic){
|
||||
PlayerPrefs.SetInt("music",1);
|
||||
instance.SourceMusic.volume = instance.defVol;
|
||||
}else{
|
||||
PlayerPrefs.SetInt("music",0);
|
||||
instance.SourceMusic.volume = 0;
|
||||
}
|
||||
PlayerPrefs.Save();
|
||||
|
||||
}
|
||||
|
||||
float defVol;
|
||||
public static void ToggleSFX(){
|
||||
if(isMuteMusic){
|
||||
if(isMuteSFX){
|
||||
PlayerPrefs.SetInt("sfx",1);
|
||||
}else{
|
||||
PlayerPrefs.SetInt("sfx",0);
|
||||
}
|
||||
PlayerPrefs.Save();
|
||||
|
||||
}
|
||||
|
||||
void Awake(){
|
||||
instance= this;
|
||||
DontDestroyOnLoad(this);
|
||||
defVol = SourceMusic.volume;
|
||||
}
|
||||
|
||||
public static void ChangeMusicToScene(string scene){
|
||||
|
||||
@@ -8,7 +8,7 @@ using UnityEngine;
|
||||
using UnityEngine.Networking;
|
||||
|
||||
public static class DataManager{
|
||||
public const string API_ENDPOINT = "http://vps.playpoolstudios.com/faucet/ufo/api/";
|
||||
public const string API_ENDPOINT = "https://vps.playpoolstudios.com/faucet/ufo/api/";
|
||||
public const string key = "#2CuV1Bit^S!sW1ZcgRv8BhrO";
|
||||
public static UserData userData{get; private set;}
|
||||
public static List<LeaderboardItemData> Leaderboard{get; private set;}
|
||||
|
||||
@@ -7,6 +7,9 @@ public class MainMenu : MonoBehaviour
|
||||
public Text[] txtStats;
|
||||
public GameObject LinkPanel;
|
||||
public InputField FHIdInput;
|
||||
public Image sfxImage,musicImage;
|
||||
|
||||
public Sprite sprite_sfxOn,sprite_sfxOff,sprite_musicOn,sprite_musicOff;
|
||||
public void OnPlay(){
|
||||
LoadingScreen.LoadLevel("Game");
|
||||
}
|
||||
@@ -22,6 +25,7 @@ public class MainMenu : MonoBehaviour
|
||||
foreach(Text txtStat in txtStats){
|
||||
txtStat.text = txtStat.text.Replace("{score}",DataManager.userData.score).Replace("{top_score}",DataManager.userData.top_score).Replace("{play_time}",Helpers.SecondsToTime(int.Parse(DataManager.userData.play_time),showSeconds:false)).Replace("{asteroids}",DataManager.userData.asteroids).Replace("{near_miss}",DataManager.userData.near_miss).Replace("{total_games}",DataManager.userData.total_games);
|
||||
}
|
||||
UpdateAudioImages();
|
||||
}
|
||||
|
||||
|
||||
@@ -59,4 +63,19 @@ public class MainMenu : MonoBehaviour
|
||||
|
||||
LoadingScreen.LoadLevel("Login");
|
||||
}
|
||||
|
||||
public void ToggleMusic(){
|
||||
AudioManager.ToggleMusic();
|
||||
UpdateAudioImages();
|
||||
}
|
||||
|
||||
public void ToggleSFX(){
|
||||
AudioManager.ToggleSFX();
|
||||
UpdateAudioImages();
|
||||
}
|
||||
|
||||
void UpdateAudioImages(){
|
||||
sfxImage.sprite = (AudioManager.isMuteSFX) ? sprite_sfxOff : sprite_sfxOn;
|
||||
musicImage.sprite = (AudioManager.isMuteMusic) ? sprite_musicOff : sprite_musicOn;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -110,7 +110,7 @@ public class PlayerController : MonoBehaviour
|
||||
public void ActivatePowerup(){
|
||||
powerupTimer = PowerupLife;
|
||||
}
|
||||
|
||||
public static int AdCounter=0;
|
||||
public async void GameOver(){
|
||||
// Application.LoadLevel(0);
|
||||
TalkWithServer();
|
||||
@@ -124,6 +124,12 @@ public class PlayerController : MonoBehaviour
|
||||
|
||||
GameOverUI.Activate();
|
||||
|
||||
AdCounter++;
|
||||
if(AdCounter > 1){
|
||||
AdCounter =0;
|
||||
|
||||
AdsManager.instance.ShowIntAd();
|
||||
}
|
||||
}
|
||||
|
||||
public void SpawnExplosionFX(Vector3 pos){
|
||||
|
||||
Reference in New Issue
Block a user