есть игра(в игре кнопка купить за 49 рублей все уровни),есть 2 скрипта.В первом скрипте только Покупки,а во втором основной скрипт для игры.Так вот я решил проверить как работает и сделать вывод текста в консоль при запуске в Editor и он почему-то не выводится,т.е. код не работает!
вот код основного скрипта:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.Purchasing;
public class game : MonoBehaviour
{
public GameObject one;
public GameObject two;
public GameObject three;
public GameObject four;
public GameObject five;
public GameObject six;
public GameObject buy_panel;
private void Start()
{
PurchaseManager.OnPurchaseNonConsumable += PurchaseManager_OnPurchaseNonConsumable;
one.SetActive(false);
two.SetActive(false);
three.SetActive(false);
four.SetActive(false);
five.SetActive(false);
six.SetActive(false);
buy_panel.SetActive(false);
}
//play
public void Play_1()
{
one.SetActive(true);
}
public void Play_2()
{
two.SetActive(true);
}
//buy
public void Buy_click()
{
buy_panel.SetActive(true);
}
public void Close()
{
buy_panel.SetActive(false);
one.SetActive(false);
two.SetActive(false);
three.SetActive(false);
four.SetActive(false);
five.SetActive(false);
six.SetActive(false);
}
private void PurchaseManager_OnPurchaseNonConsumable(PurchaseEventArgs args)
{
Debug.Log("Вы купили:" + args.purchasedProduct.definition.id+"- NonConsumable");
}
вот код 2-ого скрипт,который только с покупками в приложении:
using System;
using UnityEngine;
using UnityEngine.Purchasing;
public class PurchaseManager : MonoBehaviour, IStoreListener
{
private static IStoreController m_StoreController;
private static IExtensionProvider m_StoreExtensionProvider;
private int currentProductIndex;
[Tooltip("Не многоразовые товары. Больше подходит для отключения рекламы и т.п.")]
public string[] NC_PRODUCTS;
[Tooltip("Многоразовые товары. Больше подходит для покупки игровой валюты и т.п.")]
public string[] C_PRODUCTS;
/// <summary>
/// Событие, которое запускается при удачной покупке многоразового товара.
/// </summary>
public static event OnSuccessConsumable OnPurchaseConsumable;
/// <summary>
/// Событие, которое запускается при удачной покупке не многоразового товара.
/// </summary>
public static event OnSuccessNonConsumable OnPurchaseNonConsumable;
/// <summary>
/// Событие, которое запускается при неудачной покупке какого-либо товара.
/// </summary>
public static event OnFailedPurchase PurchaseFailed;
private void Awake()
{
InitializePurchasing();
}
/// <summary>
/// Проверить, куплен ли товар.
/// </summary>
/// <param name="id">Индекс товара в списке.</param>
/// <returns></returns>
public static bool CheckBuyState(string id)
{
Product product = m_StoreController.products.WithID(id);
if (product.hasReceipt) { return true; }
else { return false; }
}
public void InitializePurchasing()
{
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
foreach (string s in C_PRODUCTS) builder.AddProduct(s, ProductType.Consumable);
foreach (string s in NC_PRODUCTS) builder.AddProduct(s, ProductType.NonConsumable);
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyConsumable(int index)
{
currentProductIndex = index;
BuyProductID(C_PRODUCTS[index]);
}
public void BuyNonConsumable(int index)
{
currentProductIndex = index;
BuyProductID(NC_PRODUCTS[index]);
}
void BuyProductID(string productId)
{
if (IsInitialized())
{
Product product = m_StoreController.products.WithID(productId);
if (product != null && product.availableToPurchase)
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
m_StoreController.InitiatePurchase(product);
}
else
{
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
OnPurchaseFailed(product, PurchaseFailureReason.ProductUnavailable);
}
}
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
Debug.Log("OnInitialized: PASS");
m_StoreController = controller;
m_StoreExtensionProvider = extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
if (C_PRODUCTS.Length > 0 && String.Equals(args.purchasedProduct.definition.id, C_PRODUCTS[currentProductIndex], StringComparison.Ordinal))
OnSuccessC(args);
else if (NC_PRODUCTS.Length > 0 && String.Equals(args.purchasedProduct.definition.id, NC_PRODUCTS[currentProductIndex], StringComparison.Ordinal))
OnSuccessNC(args);
else Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
return PurchaseProcessingResult.Complete;
}
public delegate void OnSuccessConsumable(PurchaseEventArgs args);
protected virtual void OnSuccessC(PurchaseEventArgs args)
{
if (OnPurchaseConsumable != null) OnPurchaseConsumable(args);
Debug.Log(C_PRODUCTS[currentProductIndex] + " Buyed!");
}
public delegate void OnSuccessNonConsumable(PurchaseEventArgs args);
protected virtual void OnSuccessNC(PurchaseEventArgs args)
{
if (OnPurchaseNonConsumable != null) OnPurchaseNonConsumable(args);
Debug.Log(NC_PRODUCTS[currentProductIndex] + " Buyed!");
}
public delegate void OnFailedPurchase(Product product, PurchaseFailureReason failureReason);
protected virtual void OnFailedP(Product product, PurchaseFailureReason failureReason)
{
if (PurchaseFailed != null) PurchaseFailed(product, failureReason);
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
OnFailedP(product, failureReason);
}
}