BulletHell/Assets/BulletHellTemplate/Core/MonetizationManager.cs

68 lines
2.6 KiB
C#
Raw Permalink Normal View History

2025-09-19 19:43:49 +05:00
// BulletHellTemplate/Monetization/MonetizationManager.cs
using Cysharp.Threading.Tasks;
using System;
2025-09-19 14:56:58 +05:00
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
namespace BulletHellTemplate
{
/// <summary>
2025-09-19 19:43:49 +05:00
/// Holds currency cache & thin wrappers that forward purchase calls
/// to <see cref="BackendManager.Service"/>.
2025-09-19 14:56:58 +05:00
/// </summary>
2025-09-19 19:43:49 +05:00
[DisallowMultipleComponent]
public sealed class MonetizationManager : MonoBehaviour
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
public static MonetizationManager Singleton { get; private set; }
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
/// <summary>Raised every time a local currency value is updated.</summary>
public static event Action<string, int> OnCurrencyChanged;
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
/*──────────────────── Unity ────────────────────*/
2025-09-19 14:56:58 +05:00
private void Awake()
{
if (Singleton == null)
{
Singleton = this;
2025-09-19 19:43:49 +05:00
DontDestroyOnLoad(gameObject);
2025-09-19 14:56:58 +05:00
}
else
{
2025-09-19 19:43:49 +05:00
Destroy(gameObject);
2025-09-19 14:56:58 +05:00
}
}
2025-09-19 19:43:49 +05:00
/*───────────────── Purchases (wrappers) ─────────────────*/
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
public UniTask<RequestResult> PurchaseShopItemAsync(ShopItem item) =>
BackendManager.Service.PurchaseShopItemAsync(item);
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
public UniTask<RequestResult> ClaimBattlePassRewardAsync(BattlePassItem reward) =>
BackendManager.Service.ClaimBattlePassRewardAsync(reward);
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
/*──────────────────── Currency API ────────────────────*/
2025-09-19 14:56:58 +05:00
2025-09-19 19:43:49 +05:00
/// <summary>Returns current amount stored in <c>PlayerPrefs</c>.</summary>
public static int GetCurrency(string currencyKey) => PlayerSave.GetCurrency(currencyKey);
2025-09-19 14:56:58 +05:00
/// <summary>
2025-09-19 19:43:49 +05:00
/// Sets the amount locally, fires <see cref="OnCurrencyChanged"/>,
/// and (optionally) pushes the change to the active backend.
2025-09-19 14:56:58 +05:00
/// </summary>
2025-09-19 19:43:49 +05:00
public static void SetCurrency(string currencyKey, int amount, bool pushToBackend = true)
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
PlayerSave.SetCurrency(currencyKey, amount);
OnCurrencyChanged?.Invoke(currencyKey, amount);
2025-09-19 14:56:58 +05:00
}
2025-09-19 19:43:49 +05:00
/// <summary>Returns the sprite associated with a currency (or <c>null</c>).</summary>
public Sprite GetCurrencyIcon(string currencyKey)
2025-09-19 14:56:58 +05:00
{
2025-09-19 19:43:49 +05:00
var entry = GameInstance.Singleton.currencyData.Find(c => c.coinID == currencyKey);
return entry != null ? entry.icon : null;
2025-09-19 14:56:58 +05:00
}
}
}