Rewarded ads, question popups and few fixes
This commit is contained in:
8
Assets/Game/Scripts/Ads.meta
Normal file
8
Assets/Game/Scripts/Ads.meta
Normal file
@@ -0,0 +1,8 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 72d0f125453db74c38b646da7648a570
|
||||
folderAsset: yes
|
||||
DefaultImporter:
|
||||
externalObjects: {}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
102
Assets/Game/Scripts/Ads/AdsManager.cs
Normal file
102
Assets/Game/Scripts/Ads/AdsManager.cs
Normal file
@@ -0,0 +1,102 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
using GoogleMobileAds.Api;
|
||||
using System.Threading.Tasks;
|
||||
using System;
|
||||
using UnityEngine.Events;
|
||||
|
||||
public class AdsManager : MonoBehaviour
|
||||
{
|
||||
private static AdsManager _instance;
|
||||
public static AdsManager instance => _instance;
|
||||
|
||||
private BannerView bannerView;
|
||||
private InterstitialAd interstitial;
|
||||
private RewardedAd rewardedAd;
|
||||
|
||||
|
||||
|
||||
[Header("Ids")]
|
||||
[SerializeField]private string bannerId;
|
||||
[SerializeField]private string interstitialId;
|
||||
[SerializeField]private string rewardedId;
|
||||
|
||||
public UnityEvent onAwardEarned = new UnityEvent();
|
||||
|
||||
|
||||
public bool dontDestroyOnLoad=true;
|
||||
void Start()
|
||||
{
|
||||
_instance =this;
|
||||
if(dontDestroyOnLoad){DontDestroyOnLoad(gameObject);}
|
||||
|
||||
MobileAds.Initialize(initStatus => { Debug.Log(initStatus); });
|
||||
//RequestBanner();
|
||||
LoadInterestitial();
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
|
||||
void RequestBanner(){
|
||||
bannerView = new BannerView(bannerId, AdSize.Banner, AdPosition.BottomLeft);
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
this.bannerView.LoadAd(request);
|
||||
}
|
||||
|
||||
void LoadInterestitial(){
|
||||
interstitial = new InterstitialAd(interstitialId);
|
||||
// Create an empty ad request.
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
// Load the interstitial with the request.
|
||||
interstitial.LoadAd(request);
|
||||
}
|
||||
|
||||
void LoadRewarded(){
|
||||
rewardedAd = new RewardedAd(rewardedId);
|
||||
rewardedAd.OnUserEarnedReward += OnRewardedComplete;
|
||||
rewardedAd.OnAdFailedToLoad += OnRewardedFailed;
|
||||
rewardedAd.OnAdFailedToShow += OnRewardedFailed;
|
||||
rewardedAd.OnAdClosed += OnRewardCanceled;
|
||||
|
||||
|
||||
|
||||
AdRequest request = new AdRequest.Builder().Build();
|
||||
this.rewardedAd.LoadAd(request);
|
||||
}
|
||||
|
||||
public async void ShowInterestitial(){
|
||||
interstitial.Show();
|
||||
await Task.Delay(10000);
|
||||
LoadInterestitial();
|
||||
}
|
||||
|
||||
public async void ShowRewarded(UnityAction OnAwardEarned){
|
||||
onAwardEarned.RemoveAllListeners();
|
||||
onAwardEarned.AddListener(OnAwardEarned);
|
||||
if(!rewardedAd.IsLoaded()){
|
||||
LoadRewarded();
|
||||
}
|
||||
while(!rewardedAd.IsLoaded()){
|
||||
await Task.Delay(500);
|
||||
}
|
||||
this.rewardedAd.Show();
|
||||
}
|
||||
|
||||
void OnRewardedComplete(object sender, EventArgs args){
|
||||
onAwardEarned.Invoke();
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
void OnRewardedFailed(object sender, EventArgs args){
|
||||
MessageDialog.instance.ShowMessage("Sorry", "We couldn't Load the Ad right now. Please try again later.");
|
||||
LoadRewarded();
|
||||
|
||||
}
|
||||
|
||||
void OnRewardCanceled(object sender, EventArgs args){
|
||||
MessageDialog.instance.ShowMessage("Failed", "You closed the Ad. Please watch the full ad to receive the gift");
|
||||
LoadRewarded();
|
||||
}
|
||||
|
||||
}
|
||||
11
Assets/Game/Scripts/Ads/AdsManager.cs.meta
Normal file
11
Assets/Game/Scripts/Ads/AdsManager.cs.meta
Normal file
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: ef88ff9cfdb829c818860c6313e1e270
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -143,7 +143,7 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
public void BuyChest(ChestButton button){
|
||||
if(DBmanager.Gems < button.Price){
|
||||
MessageDialog.instance.ShowDialog("Failed", "Insufficient Gems to complete the purchase");
|
||||
MessageDialog.instance.ShowMessage("Failed", "Insufficient Gems to complete the purchase");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -153,7 +153,7 @@ public class GameManager : MonoBehaviour
|
||||
|
||||
public void BuyGold(GoldPackButton button){
|
||||
if(DBmanager.Gems < button.Price){
|
||||
MessageDialog.instance.ShowDialog("Failed", "Insufficient Gems to complete the purchase");
|
||||
MessageDialog.instance.ShowMessage("Failed", "Insufficient Gems to complete the purchase");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -39,11 +39,11 @@ public class LoginManager : MonoBehaviour
|
||||
|
||||
public void OnLoginClicked(){
|
||||
if(login_username.text.Length < 2){
|
||||
MessageDialog.instance.ShowDialog("Invalid!", "The username you entered is invalid");
|
||||
MessageDialog.instance.ShowMessage("Invalid!", "The username you entered is invalid");
|
||||
return;
|
||||
}
|
||||
if(login_password.text.Length < 5){
|
||||
MessageDialog.instance.ShowDialog("Invalid!", "The password you entered is invalid");
|
||||
MessageDialog.instance.ShowMessage("Invalid!", "The password you entered is invalid");
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -51,11 +51,11 @@ public class LoginManager : MonoBehaviour
|
||||
}
|
||||
public void OnRegisterClicked(){
|
||||
if(reg_username.text.Length < 2){
|
||||
MessageDialog.instance.ShowDialog("Couldn't Register", "Please use a valid Username (should be more than 2 characters)");
|
||||
MessageDialog.instance.ShowMessage("Couldn't Register", "Please use a valid Username (should be more than 2 characters)");
|
||||
return;
|
||||
}
|
||||
if(reg_password.text.Length < 5){
|
||||
MessageDialog.instance.ShowDialog("Couldn't Register", "Please use a Strong password (should be more than 5 characters)");
|
||||
MessageDialog.instance.ShowMessage("Couldn't Register", "Please use a Strong password (should be more than 5 characters)");
|
||||
return;
|
||||
}
|
||||
Debug.Log("Clicked register");
|
||||
@@ -106,7 +106,7 @@ public class LoginManager : MonoBehaviour
|
||||
else
|
||||
{
|
||||
Debug.Log("User Login failed. Error #" + www.text);
|
||||
MessageDialog.instance.ShowDialog("Error!", "Sorry, We couldn't log you in,\n Error: " + www.text);
|
||||
MessageDialog.instance.ShowMessage("Error!", "Sorry, We couldn't log you in,\n Error: " + www.text);
|
||||
}
|
||||
loginBtn.interactable=true;
|
||||
}
|
||||
@@ -133,7 +133,7 @@ public class LoginManager : MonoBehaviour
|
||||
else
|
||||
{
|
||||
Debug.Log("User creation failed " + www.text);
|
||||
MessageDialog.instance.ShowDialog("Error!", "Sorry, We couldn't Register you,\n Error: " + www.text);
|
||||
MessageDialog.instance.ShowMessage("Error!", "Sorry, We couldn't Register you,\n Error: " + www.text);
|
||||
|
||||
}
|
||||
regBtn.interactable=true;
|
||||
|
||||
@@ -2,6 +2,7 @@ using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
|
||||
[RequireComponent(typeof(CanvasGroup))]
|
||||
@@ -12,42 +13,72 @@ public class MessageDialog : MonoBehaviour
|
||||
public static MessageDialog instance=>m_instance;
|
||||
public TMPro.TMP_Text titleTxt;
|
||||
public TMPro.TMP_Text messageTxt;
|
||||
public Button actionBtn;
|
||||
public Button okayBtn;
|
||||
public Button yesBtn;
|
||||
public Button noBtn;
|
||||
|
||||
public bool showing => GetComponent<CanvasGroup>().blocksRaycasts;
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_instance = this;
|
||||
actionBtn.onClick.AddListener(OnAction);
|
||||
okayBtn.onClick.AddListener(OnOkay);
|
||||
yesBtn.onClick.AddListener(OnClickedYes);
|
||||
noBtn.onClick.AddListener(OnClickedNo);
|
||||
SetActive(false);
|
||||
DontDestroyOnLoad(gameObject);
|
||||
}
|
||||
|
||||
void OnAction(){
|
||||
void OnOkay(){
|
||||
if(showing){
|
||||
SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
public async void ShowDialog(string title, string message){
|
||||
public void ShowMessage(string title, string message){
|
||||
titleTxt.text = title;
|
||||
messageTxt.text = message;
|
||||
|
||||
|
||||
yesBtn.gameObject.SetActive(false);
|
||||
noBtn.gameObject.SetActive(false);
|
||||
okayBtn.gameObject.SetActive(true);
|
||||
SetActive(true);
|
||||
Canvas.ForceUpdateCanvases();
|
||||
GetComponentInChildren<VerticalLayoutGroup>().enabled=false;
|
||||
GetComponentInChildren<VerticalLayoutGroup>().enabled=true;
|
||||
|
||||
|
||||
|
||||
Refresh();
|
||||
// messageTxt.gameObject.SetActive(true);
|
||||
}
|
||||
UnityAction onYes = ()=>{};
|
||||
UnityAction onNo = ()=>{};
|
||||
|
||||
public void ShowQuestion(string title, string message, UnityAction OnYes, UnityAction OnNo){
|
||||
titleTxt.text = title;
|
||||
messageTxt.text = message;
|
||||
onYes = OnYes;
|
||||
onNo = OnNo;
|
||||
|
||||
yesBtn.gameObject.SetActive(true);
|
||||
noBtn.gameObject.SetActive(true);
|
||||
okayBtn.gameObject.SetActive(false);
|
||||
SetActive(true);
|
||||
Refresh();
|
||||
}
|
||||
void OnClickedYes(){
|
||||
if(onYes!=null){onYes();}
|
||||
OnOkay();
|
||||
}
|
||||
void OnClickedNo(){
|
||||
if(onNo!=null){onNo();}
|
||||
OnOkay();
|
||||
|
||||
}
|
||||
public void SetActive(bool value){
|
||||
GetComponent<CanvasGroup>().blocksRaycasts= value;
|
||||
GetComponent<CanvasGroup>().interactable= value;
|
||||
GetComponent<CanvasGroup>().alpha= (value) ? 1: 0;
|
||||
}
|
||||
|
||||
void Refresh(){
|
||||
Canvas.ForceUpdateCanvases();
|
||||
GetComponentInChildren<VerticalLayoutGroup>().enabled=false;
|
||||
GetComponentInChildren<VerticalLayoutGroup>().enabled=true;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -29,6 +29,7 @@ public class SceneDataHolder : MonoBehaviour
|
||||
}
|
||||
|
||||
public void ShowDeadscreen(int xpEarned, int metalEarned, double survivalTime){
|
||||
AdsManager.instance.ShowInterestitial();
|
||||
deadScreen.SetActive(true);
|
||||
xpEarnings.SetActive(xpEarned > 0); metalEarnings.SetActive(metalEarned > 0);
|
||||
xpEarnings.GetComponentInChildren<TMP_Text>().text = xpEarned.ToString();
|
||||
|
||||
@@ -14,6 +14,7 @@ public class ChestButton : MonoBehaviour
|
||||
public float minLuck = 0;
|
||||
public float maxLuck = 100;
|
||||
public Button btnInfo;
|
||||
public Button adButton;
|
||||
[TextArea]
|
||||
public string infoTxt;
|
||||
|
||||
@@ -21,17 +22,26 @@ public class ChestButton : MonoBehaviour
|
||||
{
|
||||
btnInfo.onClick.AddListener(OnClickedInfo);
|
||||
|
||||
if(!IsSpecial)GetComponent<Button>().onClick.AddListener(OnClick);
|
||||
if(!IsSpecial){GetComponent<Button>().onClick.AddListener(OnClick);adButton.onClick.AddListener(OnAdClicked);}
|
||||
}
|
||||
|
||||
void OnClickedInfo()
|
||||
{
|
||||
MessageDialog.instance.ShowDialog(ChestName, $"This chest will drop following items.\n\n{getItemsProbability()}");
|
||||
MessageDialog.instance.ShowMessage(ChestName, $"This chest will drop following items.\n\n{getItemsProbability()}");
|
||||
}
|
||||
|
||||
void OnClick(){
|
||||
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase {ChestName} for {Price} Gems?", Buy,null);
|
||||
}
|
||||
|
||||
void OnAdClicked(){
|
||||
Debug.Log("Showing ad");
|
||||
AdsManager.instance.ShowRewarded(Buy);
|
||||
}
|
||||
|
||||
void Buy(){
|
||||
if(DBmanager.Gems < Price){
|
||||
MessageDialog.instance.ShowDialog("Failed","Insufficient Gems to complete the Purchase!");
|
||||
MessageDialog.instance.ShowMessage("Failed","Insufficient Gems to complete the Purchase!");
|
||||
return;
|
||||
}
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@ public class GoldPackButton : MonoBehaviour
|
||||
public int Price;
|
||||
void Start()
|
||||
{
|
||||
GetComponent<Button>().onClick.AddListener(()=>{GameManager.instance.BuyGold(this);});
|
||||
GetComponent<Button>().onClick.AddListener(OnClicked);
|
||||
}
|
||||
|
||||
// Update is called once per frame
|
||||
@@ -22,6 +22,14 @@ public class GoldPackButton : MonoBehaviour
|
||||
|
||||
}
|
||||
|
||||
void OnClicked(){
|
||||
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase {Amount} Gold for {Price} Gems?", Buy,null);
|
||||
}
|
||||
|
||||
void Buy(){
|
||||
GameManager.instance.BuyGold(this);
|
||||
}
|
||||
|
||||
void OnValidate() {
|
||||
if(!autoReadFromText){return;}
|
||||
if(txtAmount!=null){
|
||||
|
||||
@@ -62,8 +62,15 @@ public class SpecialChest : MonoBehaviour
|
||||
|
||||
void OnClicked(){
|
||||
// gameObject.SetActive(false);
|
||||
//AdsManager.instance.ShowRewarded(Buy);
|
||||
// Buy();
|
||||
MessageDialog.instance.ShowQuestion("Are you sure?", $"Are you sure to purchase This special offer for {chestButton.Price} Gems?", Buy,null);
|
||||
|
||||
}
|
||||
|
||||
void Buy(){
|
||||
if(DBmanager.Gems < chestButton.Price){
|
||||
MessageDialog.instance.ShowDialog("Failed","Insufficient Gems to complete the Purchase!");
|
||||
MessageDialog.instance.ShowMessage("Failed","Insufficient Gems to complete the Purchase!");
|
||||
return;
|
||||
}
|
||||
DBmanager.SetGems(DBmanager.Gems - chestButton.Price);
|
||||
|
||||
Reference in New Issue
Block a user