You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
PlumberUltimateAds/Assets/ShopManager.cs

691 lines
20 KiB
C#

using System.Collections;
using UnityEngine;
using TMPro;
using System;
using UnityEngine.Purchasing;
using System.Collections.Generic;
using static UnityEngine.Networking.UnityWebRequest;
using UnityEngine.Purchasing.Extension;
using UnityEngine.UI;
1 month ago
//using UnityEngine.WSA;
using DG.Tweening;
[Serializable]
public class ConsumableItem
{
public string Name;
public string Id;
public string desc;
public float price;
}
[Serializable]
public class NonConsumableItem
{
public string Name;
public string Id;
public string desc;
public float price;
}
[Serializable]
public class SubscriptionItem
{
public string Name;
public string Id;
public string desc;
public float price;
public int timeDuration;// in Days
}
public class ShopManager : MonoBehaviour, IDetailedStoreListener
{
IStoreController m_StoreController;
public List<ConsumableItem> cItem;
1 month ago
public List<ConsumableItem> pItem;
public List<ConsumableItem> eItem;
public List<int> coinsRewards;
//public NonConsumableItem ncItem;
//public SubscriptionItem sItem;
public TMP_InputField inp;
public Data data;
public Payload payload;
public PayloadData payloadData;
public List<ButtonState> PipeButtons;
public List<ButtonState> EffectButtons;
//public List<Button> PurchaseButtons;
public GameObject NotEnoughCoinsObj;
1 week ago
public GameObject LoadingScreen;
[Serializable]
public class ButtonState
{
public bool isPurchased;
public bool isEquipped;
public Button PurchaseButton;
public Button EquipButton;
public TextMeshProUGUI equipButtonText;
}
private void Start()
{
1 week ago
EffectDefaulter();
1 month ago
int coins = PlayerPrefs.GetInt("Coin");
coinTxt.text = coins.ToString();
SetupBuilder();
PipePrefGetter();
EffectPrefGetter();
}
1 week ago
void EffectDefaulter()
{
if(PlayerPrefs.HasKey("EffectButtonIsEquipped")==false)
{
PlayerPrefs.SetInt("Coin", PlayerPrefs.GetInt("Coin") + (int)eItem[0].price);
Debug.Log("EffectDefaulter");
AddEffects(0);
EquipUnEquipEffect(0);
}
}
public bool IsProductPurchased(string productId)
{
Product product = m_StoreController.products.WithID(productId);
bool itemBought = false;
if (product != null && product.hasReceipt)
{
// Owned Non Consumables and Subscriptions should always have receipts.
// So here the Non Consumable product has already been bought.
itemBought = true;
}
//var product = m_StoreController?.catalog.allProducts.FirstOrDefault(p => p.definition.id == productId);
//if (product != null)
//{
// return m_StoreController.playerReceipt.ContainsProduct(product);
//}
Debug.Log("IsProductPurchased Called for: " + productId);
return itemBought;
}
// Call this function when you want to check if a product is purchased
public void CheckIfProductPurchased()
{
for (int i = 1; i < pItem.Count; i++)
{
if (IsProductPurchased(pItem[i].Id))
{
//PlayerPrefs.SetInt("PipeButtonIsEquipped" + 0, 1);
PlayerPrefs.SetInt("PipeButtonIsPurchased" + i, 1);
Debug.Log("Pipe " + i + " is purchased already!");
3 weeks ago
AddPipes(i);
// Product is purchased, grant access to features
}
//else
//{
// // Product is not purchased, handle accordingly
//}
}
}
void PipePrefGetter()
{
if (!PlayerPrefs.HasKey("PipeButtonIsEquipped" + 0))
{
PlayerPrefs.SetInt("PipeButtonIsEquipped" + 0, 1);
PlayerPrefs.SetInt("PipeButtonIsPurchased" + 0, 1);
}
for (int i = 0; i < PipeButtons.Count; i++)
{
PipeButtons[i].isEquipped = PlayerPrefs.GetInt("PipeButtonIsEquipped" + i) == 1 ? true : false;
PipeButtons[i].isPurchased = PlayerPrefs.GetInt("PipeButtonIsPurchased" + i) == 1 ? true : false;
if (PipeButtons[i].isPurchased == false)
{
PipeButtons[i].PurchaseButton.gameObject.SetActive(true);
PipeButtons[i].EquipButton.gameObject.SetActive(false);
}
else
{
PipeButtons[i].EquipButton.gameObject.SetActive(true);
PipeButtons[i].PurchaseButton.gameObject.SetActive(false);
if (PipeButtons[i].isEquipped == true)
{
PipeButtons[i].equipButtonText.text = "Equipped";
}
else
{
PipeButtons[i].equipButtonText.text = "Equip";
}
}
}
}
void EffectPrefGetter()
{
for (int i = 0; i < EffectButtons.Count; i++)
{
EffectButtons[i].isEquipped = PlayerPrefs.GetInt("EffectButtonIsEquipped" + i) == 1 ? true : false;
EffectButtons[i].isPurchased = PlayerPrefs.GetInt("EffectButtonIsPurchased" + i) == 1 ? true : false;
if (EffectButtons[i].isPurchased == false)
{
EffectButtons[i].PurchaseButton.gameObject.SetActive(true);
EffectButtons[i].EquipButton.gameObject.SetActive(false);
}
else
{
EffectButtons[i].EquipButton.gameObject.SetActive(true);
EffectButtons[i].PurchaseButton.gameObject.SetActive(false);
if (EffectButtons[i].isEquipped == true)
{
EffectButtons[i].equipButtonText.text = "Equipped";
}
else
{
EffectButtons[i].equipButtonText.text = "Equip";
}
}
}
}
#region setup and initialize
void SetupBuilder()
{
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
for (int i = 0; i < cItem.Count; i++)
{
builder.AddProduct(cItem[i].Id, ProductType.Consumable);
}
4 weeks ago
for (int i = 1; i < pItem.Count; i++)//Starting from 1 because 1 set of pipe is already bought by default.
{
builder.AddProduct(pItem[i].Id, ProductType.NonConsumable);
}
//for (int i = 0; i < eItem.Count; i++)//Starting from 1 because 1 set of pipe is already bought by default.
//{
// builder.AddProduct(eItem[i].Id, ProductType.NonConsumable);
//}
//builder.AddProduct(ncItem.Id, ProductType.NonConsumable);
//builder.AddProduct(sItem.Id, ProductType.Subscription);
UnityPurchasing.Initialize(this, builder);
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
print("Success");
m_StoreController = controller;
3 weeks ago
CheckIfProductPurchased();
//CheckNonConsumable(ncItem.Id);
//CheckSubscription(sItem.Id);
}
#endregion
#region button clicks
public void Consumable_Btn_Pressed(int index)
{
//AddCoins(50);
1 month ago
currentPurchaseType = PurchaseType.Coins;
1 week ago
LoadingScreen.SetActive(true);
m_StoreController.InitiatePurchase(cItem[index].Id);
1 month ago
}
public void Consumable_Btn_Pressed_Pipes(int index)
{
//AddCoins(50);
currentPurchaseType = PurchaseType.Pipes;
Debug.Log("Purchasing pipes1... : " + pItem[index].Id);
1 week ago
LoadingScreen.SetActive(true);
m_StoreController.InitiatePurchase(pItem[index].Id);
//Debug.Log("Purchasing pipes2... : "+ pItem[index].Id);
}
public void Consumable_Btn_Pressed_Effects(int index)
{
currentPurchaseType = PurchaseType.Effects;
AddEffects(index);
}
#endregion
1 month ago
public PurchaseType currentPurchaseType;
public enum PurchaseType
{
Coins, Pipes, Effects,
1 month ago
}
#region main
//processing purchase
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs purchaseEvent)
{
//Retrive the purchased product
Debug.Log("Process Purchase");
var product = purchaseEvent.purchasedProduct;
print("Purchase Complete" + product.definition.id + " Purchase Type: " + currentPurchaseType.ToString());
1 week ago
LoadingScreen.SetActive(false);
1 month ago
if (currentPurchaseType == PurchaseType.Coins)
{
1 month ago
for (int h = 0; h < cItem.Count; h++)
{
1 month ago
if (product.definition.id == cItem[h].Id)//consumable item is pressed
{
//string receipt = product.receipt;
//data = JsonUtility.FromJson<Data>(receipt);
//payload = JsonUtility.FromJson<Payload>(data.Payload);
//payloadData = JsonUtility.FromJson<PayloadData>(payload.json);
//int quantity = payloadData.quantity;
//for (int i = 0; i < quantity; i++)
//{
AddCoins(h);
//}
1 month ago
}
}
}
else if (currentPurchaseType == PurchaseType.Pipes)
{
for (int h = 0; h < pItem.Count; h++)
{
Debug.Log("product.definition.id : " + product.definition.id);
Debug.Log("pItem[h].Id : " + pItem[h].Id);
1 month ago
if (product.definition.id == pItem[h].Id)//consumable item is pressed
{
//string receipt = product.receipt;
//data = JsonUtility.FromJson<Data>(receipt);
//payload = JsonUtility.FromJson<Payload>(data.Payload);
//payloadData = JsonUtility.FromJson<PayloadData>(payload.json);
1 month ago
//int quantity = payloadData.quantity;
1 month ago
//for (int i = 0; i < quantity; i++)
//{
AddPipes(h);
//}
}
}
}
//else if (currentPurchaseType == PurchaseType.Effects)
//{
// for (int h = 0; h < eItem.Count; h++)
// {
// Debug.Log("product.definition.id : " + product.definition.id);
// Debug.Log("pItem[h].Id : " + eItem[h].Id);
// if (product.definition.id == eItem[h].Id)//consumable item is pressed
// {
// //string receipt = product.receipt;
// //data = JsonUtility.FromJson<Data>(receipt);
// //payload = JsonUtility.FromJson<Payload>(data.Payload);
// //payloadData = JsonUtility.FromJson<PayloadData>(payload.json);
// //int quantity = payloadData.quantity;
// //for (int i = 0; i < quantity; i++)
// //{
// AddEffects(h);
// //}
// }
// }
//}
1 month ago
//else if (product.definition.id == ncItem.Id)//non consumable
//{
// RemoveAds();
//}
//else if (product.definition.id == sItem.Id)//subscribed
//{
// ActivateElitePass();
//}
return PurchaseProcessingResult.Complete;
}
#endregion
void CheckNonConsumable(string id)
{
if (m_StoreController != null)
{
var product = m_StoreController.products.WithID(id);
if (product != null)
{
if (product.hasReceipt)//purchased
{
RemoveAds();
}
else
{
ShowAds();
}
}
}
}
void CheckSubscription(string id)
{
var subProduct = m_StoreController.products.WithID(id);
if (subProduct != null)
{
try
{
if (subProduct.hasReceipt)
{
var subManager = new SubscriptionManager(subProduct, null);
var info = subManager.getSubscriptionInfo();
/*print(info.getCancelDate());
print(info.getExpireDate());
print(info.getFreeTrialPeriod());
print(info.getIntroductoryPrice());
print(info.getProductId());
print(info.getPurchaseDate());
print(info.getRemainingTime());
print(info.getSkuDetails());
print(info.getSubscriptionPeriod());
print(info.isAutoRenewing());
print(info.isCancelled());
print(info.isExpired());
print(info.isFreeTrial());
print(info.isSubscribed());*/
if (info.isSubscribed() == UnityEngine.Purchasing.Result.True)
{
print("We are subscribed");
ActivateElitePass();
}
else
{
print("Un subscribed");
DeActivateElitePass();
}
}
else
{
print("receipt not found !!");
}
}
catch (Exception)
{
print("It only work for Google store, app store, amazon store, you are using fake store!!");
}
}
else
{
print("product not found !!");
}
}
#region error handeling
public void OnInitializeFailed(InitializationFailureReason error)
{
print("failed" + error);
1 week ago
LoadingScreen.SetActive(false);
}
public void OnInitializeFailed(InitializationFailureReason error, string message)
{
1 week ago
LoadingScreen.SetActive(false);
print("initialize failed" + error + message);
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
1 week ago
LoadingScreen.SetActive(false);
print("purchase failed" + failureReason);
}
public void OnPurchaseFailed(Product product, PurchaseFailureDescription failureDescription)
{
1 week ago
LoadingScreen.SetActive(false);
print("purchase failed" + failureDescription);
}
#endregion
#region extra
[Header("Consumable")]
public Text coinTxt;
void AddCoins(int index)
{
int toAdd = coinsRewards[index];
int coins = PlayerPrefs.GetInt("Coin");
1 month ago
Debug.Log(toAdd + " Coins purchased");
coins += toAdd;
PlayerPrefs.SetInt("Coin", coins);
StartCoroutine(startCoinShakeEffect(coins - toAdd, coins, .5f));
}
void SubtractCoins(int coinsToSubtract)
{
1 month ago
int coins = PlayerPrefs.GetInt("Coin");
coins -= coinsToSubtract;
PlayerPrefs.SetInt("Coin", coins);
StartCoroutine(startCoinShakeEffect(coins + coinsToSubtract, coins, .5f));
1 month ago
}
public void EquipUnEquipPipe(int buttonIndex)
{
if (PipeButtons[buttonIndex].isEquipped)
{
PlayerPrefs.SetInt("PipeButtonIsEquipped" + buttonIndex, 0);
PlayerPrefs.SetInt("PipeButtonIsEquipped" + 0, 1);
PipeIndexSetter(0);
}
else
{
for (int i = 0; i < PipeButtons.Count; i++)
{
if (i != buttonIndex)
{
PipeButtons[i].isEquipped = false;
PlayerPrefs.SetInt("PipeButtonIsEquipped" + i, 0);
PipeButtons[i].equipButtonText.text = "Equip";
}
else
{
PipeButtons[i].isEquipped = true;
PlayerPrefs.SetInt("PipeButtonIsEquipped" + i, 1);
PipeButtons[i].equipButtonText.text = "Equipped";
PipeIndexSetter(buttonIndex);
}
}
}
}
public void EquipUnEquipEffect(int buttonIndex)
{
if (EffectButtons[buttonIndex].isEquipped)
{
PlayerPrefs.SetInt("EffectButtonIsEquipped" + buttonIndex, 0);
PlayerPrefs.SetInt("EffectButtonIsEquipped-1", 1);
EffectIndexSetter(-1);
}
else
{
for (int i = 0; i < EffectButtons.Count; i++)
{
if (i != buttonIndex)
{
EffectButtons[i].isEquipped = false;
PlayerPrefs.SetInt("EffectButtonIsEquipped" + i, 0);
EffectButtons[i].equipButtonText.text = "Equip";
}
else
{
EffectButtons[i].isEquipped = true;
PlayerPrefs.SetInt("EffectButtonIsEquipped" + i, 1);
EffectButtons[i].equipButtonText.text = "Equipped";
EffectIndexSetter(buttonIndex);
}
}
}
}
public void PipeIndexSetter(int index)
{
GameManager.PipeIndex = index;
}
public void EffectIndexSetter(int index)
{
GameManager.EffectIndex = index;
}
1 month ago
void AddPipes(int index)
{
PlayerPrefs.SetInt("PipeButtonIsPurchased" + index, 1);
PipeButtons[index].isPurchased = true;
PipeButtons[index].EquipButton.gameObject.SetActive(true);
PipeButtons[index].PurchaseButton.gameObject.SetActive(false);
1 month ago
Debug.Log("Pipe " + index + " purchased");
}
void AddEffects(int index)
{
int coins = PlayerPrefs.GetInt("Coin");
1 week ago
if (coins >= eItem[index].price)
{
SubtractCoins((int)eItem[index].price);
PlayerPrefs.SetInt("EffectButtonIsPurchased" + index, 1);
EffectButtons[index].isPurchased = true;
EffectButtons[index].EquipButton.gameObject.SetActive(true);
EffectButtons[index].PurchaseButton.gameObject.SetActive(false);
Debug.Log("Effect " + index + " purchased");
}
else
{
NotEnoughCoinsObj.SetActive(true);
}
}
float val;
IEnumerator startCoinShakeEffect(int oldValue, int newValue, float animTime)
{
float ct = 0;
float nt;
float tot = animTime;
coinTxt.transform.DOShakePosition(0.5f);
// coinTxt.GetComponent<Animation>().Play("textShake");
while (ct < tot)
{
ct += Time.deltaTime;
nt = ct / tot;
val = Mathf.Lerp(oldValue, newValue, nt);
coinTxt.text = ((int)(val)).ToString();
yield return null;
}
//coinTxt.GetComponent<Animation>().Stop();
}
[Header("Non Consumable")]
public GameObject AdsPurchasedWindow;
public GameObject adsBanner;
void RemoveAds()
{
DisplayAds(false);
}
void ShowAds()
{
DisplayAds(true);
}
void DisplayAds(bool x)
{
if (!x)
{
AdsPurchasedWindow.SetActive(true);
adsBanner.SetActive(false);
}
else
{
AdsPurchasedWindow.SetActive(false);
adsBanner.SetActive(true);
}
}
[Header("Subscription")]
public GameObject subActivatedWindow;
public GameObject premiumBanner;
void ActivateElitePass()
{
setupElitePass(true);
}
void DeActivateElitePass()
{
setupElitePass(false);
}
void setupElitePass(bool x)
{
if (x)// active
{
subActivatedWindow.SetActive(true);
premiumBanner.SetActive(true);
}
else
{
subActivatedWindow.SetActive(false);
premiumBanner.SetActive(false);
}
}
#endregion
}
[Serializable]
public class SkuDetails
{
public string productId;
public string type;
public string title;
public string name;
public string iconUrl;
public string description;
public string price;
public long price_amount_micros;
public string price_currency_code;
public string skuDetailsToken;
}
[Serializable]
public class PayloadData
{
public string orderId;
public string packageName;
public string productId;
public long purchaseTime;
public int purchaseState;
public string purchaseToken;
public int quantity;
public bool acknowledged;
}
[Serializable]
public class Payload
{
public string json;
public string signature;
public List<SkuDetails> skuDetails;
public PayloadData payloadData;
}
[Serializable]
public class Data
{
public string Payload;
public string Store;
public string TransactionID;
}