30 changed files with 517 additions and 53 deletions
Binary file not shown.
Binary file not shown.
@ -0,0 +1,46 @@ |
|||||
|
using Core.Ads; |
||||
|
using Core.IAP; |
||||
|
using Core.Settings; |
||||
|
using UnityEngine; |
||||
|
using UnityEngine.Events; |
||||
|
using UnityEngine.UI; |
||||
|
|
||||
|
namespace Core.Ads |
||||
|
{ |
||||
|
[RequireComponent(typeof(Button))] |
||||
|
public sealed class PurchaseButton : MonoBehaviour |
||||
|
{ |
||||
|
[SerializeField] private string _productId = ""; |
||||
|
[SerializeField] private UnityEvent _onSucces = null; |
||||
|
[SerializeField] private UnityEvent _onFailed = null; |
||||
|
|
||||
|
private void Awake() |
||||
|
{ |
||||
|
Button button = GetComponent<Button>(); |
||||
|
|
||||
|
button.onClick.AddListener(() => |
||||
|
{ |
||||
|
PurchaseManager.Buy(_productId); |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private void OnEnable() |
||||
|
{ |
||||
|
PurchaseManager.OnPurchaseSuccess += CheckAdsEnablingAfterPurchasing; |
||||
|
|
||||
|
if (PurchaseManager.IsProductPurchased(_productId)) |
||||
|
gameObject.SetActive(false); |
||||
|
} |
||||
|
|
||||
|
private void OnDisable() |
||||
|
{ |
||||
|
PurchaseManager.OnPurchaseSuccess -= CheckAdsEnablingAfterPurchasing; |
||||
|
} |
||||
|
|
||||
|
private void CheckAdsEnablingAfterPurchasing(string productId) |
||||
|
{ |
||||
|
if (_productId == productId) |
||||
|
gameObject.SetActive(false); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,72 @@ |
|||||
|
using System; |
||||
|
using UnityEngine; |
||||
|
using UnityEngine.Purchasing; |
||||
|
|
||||
|
namespace Core.IAP |
||||
|
{ |
||||
|
public sealed class PurchaseManager : MonoBehaviour |
||||
|
{ |
||||
|
private static PurchaseManager _instance = null; |
||||
|
private static PurchaseManagerBase _purchaseManager = null; |
||||
|
|
||||
|
public static event Action<string> OnPurchaseSuccess = null; |
||||
|
public static event Action<string, PurchaseFailureReason> OnPurchaseFailed = null; |
||||
|
|
||||
|
internal static void Init() |
||||
|
{ |
||||
|
GameObject gameObject = new GameObject("[SceneLoader]"); |
||||
|
_instance = gameObject.AddComponent<PurchaseManager>(); |
||||
|
|
||||
|
DontDestroyOnLoad(_instance); |
||||
|
|
||||
|
_purchaseManager = new PurchaseManagerBase(); |
||||
|
_purchaseManager.InitializePurchasing(); |
||||
|
|
||||
|
_purchaseManager.OnPurchaseSuccess += (productId) => OnPurchaseSuccess(productId); |
||||
|
_purchaseManager.OnPurchaseFaile += (productId, error) => OnPurchaseFailed(productId, error); |
||||
|
} |
||||
|
|
||||
|
public static bool IsProductPurchased(string productId) => |
||||
|
_purchaseManager.IsProductPurchased(productId); |
||||
|
|
||||
|
public static string GetLocalizedPrice(string productId) => |
||||
|
_purchaseManager.GetLocalizedPrice(productId); |
||||
|
|
||||
|
public static void Buy(string productId) |
||||
|
{ |
||||
|
if (Application.isEditor) |
||||
|
_instance.TryPurchaseInEditor(productId); |
||||
|
else |
||||
|
_purchaseManager.BuyProduct(productId); |
||||
|
} |
||||
|
|
||||
|
private bool _isVisible = false; |
||||
|
private string _productId = ""; |
||||
|
|
||||
|
private void OnGUI() |
||||
|
{ |
||||
|
if (_isVisible && !string.IsNullOrWhiteSpace(_productId)) |
||||
|
{ |
||||
|
if (GUI.Button(new Rect(0f, 0f, Screen.width, Screen.height / 2f), $"Confirm purchasing \"{_productId}\"")) |
||||
|
{ |
||||
|
_purchaseManager.BuyProduct(_productId); |
||||
|
_productId = ""; |
||||
|
_isVisible = false; |
||||
|
} |
||||
|
|
||||
|
if (GUI.Button(new Rect(0f, Screen.height / 2f, Screen.width, Screen.height / 2f), "Cancel purchasing")) |
||||
|
{ |
||||
|
OnPurchaseFailed?.Invoke(_productId, PurchaseFailureReason.UserCancelled); |
||||
|
_productId = ""; |
||||
|
_isVisible = false; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private void TryPurchaseInEditor(string productId) |
||||
|
{ |
||||
|
_productId = productId; |
||||
|
_isVisible = true; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -1 +1 @@ |
|||||
0321155518328e34ad4bff3066618efe2296813f |
f6ca9d77415befe9f9b6e116c90544ba7c334a44 |
||||
|
|||||
Binary file not shown.
@ -0,0 +1,47 @@ |
|||||
|
using Core.Ads; |
||||
|
using Core.Settings; |
||||
|
using System.Collections.Generic; |
||||
|
using UnityEditor; |
||||
|
|
||||
|
namespace CoreEditor.Localization |
||||
|
{ |
||||
|
[CustomEditor(typeof(PurchaseButton))] |
||||
|
public sealed class PurchaseButtonEditor : Editor |
||||
|
{ |
||||
|
public override void OnInspectorGUI() |
||||
|
{ |
||||
|
serializedObject.Update(); |
||||
|
|
||||
|
EditorGUILayout.Space(5); |
||||
|
|
||||
|
SerializedProperty productId = serializedObject.FindProperty("_productId"); |
||||
|
|
||||
|
List<string> products = new List<string>(); |
||||
|
|
||||
|
products.AddRange(CoreSettings.data.consumableProducts); |
||||
|
products.AddRange(CoreSettings.data.nonConsumableProducts); |
||||
|
|
||||
|
int currentIndex = products.IndexOf(productId.stringValue); |
||||
|
|
||||
|
if (currentIndex < 0 || currentIndex >= products.Count) |
||||
|
currentIndex = 0; |
||||
|
|
||||
|
if (products.Count == 0) |
||||
|
products = new List<string> { "None" }; |
||||
|
|
||||
|
currentIndex = EditorGUILayout.Popup("Product ID:", currentIndex, products.ToArray()); |
||||
|
|
||||
|
productId.stringValue = products[currentIndex]; |
||||
|
|
||||
|
EditorGUILayout.Space(10); |
||||
|
|
||||
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("_onSucces")); |
||||
|
|
||||
|
EditorGUILayout.Space(5); |
||||
|
|
||||
|
EditorGUILayout.PropertyField(serializedObject.FindProperty("_onFailed")); |
||||
|
|
||||
|
serializedObject.ApplyModifiedProperties(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Binary file not shown.
@ -1 +1 @@ |
|||||
3e580022314eb2bd79ab49c90af6937d4423889c |
11bcacb5e6198468d31724d89237cbc3f0ee9953 |
||||
|
|||||
Binary file not shown.
Loading…
Reference in new issue