So i followed a tutorial online to add the iap and ended up with this script.
using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Purchasing;
public class IAPManager : MonoBehaviour, IStoreListener
{
PlayMakerFSM PurchaseFSM;
public static IAPManager Instance{ set;get;}
private static IStoreController m_StoreController; // The Unity Purchasing system.
private static IExtensionProvider m_StoreExtensionProvider; // The store-specific Purchasing subsystems.
public static string PRODUCT_50000_GOLD = "50000_gold";
public static string PRODUCT_150000_GOLD = "150000_gold";
public static string PRODUCT_400000_GOLD = "400000_gold";
private void Awake()
{
Instance = this;
}
void Start()
{
PurchaseFSM = GameObject.Find("Purchase").GetComponent<PlayMakerFSM>();
// If we haven't set up the Unity Purchasing reference
if (m_StoreController == null)
{
// Begin to configure our connection to Purchasing
InitializePurchasing();
}
}
public void InitializePurchasing()
{
// If we have already connected to Purchasing ...
if (IsInitialized())
{
// ... we are done here.
return;
}
// Create a builder, first passing in a suite of Unity provided stores.
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(PRODUCT_50000_GOLD, ProductType.Consumable);
builder.AddProduct(PRODUCT_150000_GOLD, ProductType.Consumable);
builder.AddProduct(PRODUCT_400000_GOLD, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
// Only say we are initialized if both the Purchasing references are set.
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void buy_50000_Gold()
{
// Buy the consumable product using its general identifier. Expect a response either
// through ProcessPurchase or OnPurchaseFailed asynchronously.
BuyProductID(PRODUCT_50000_GOLD);
}
public void buy_150000_Gold()
{
// Buy the consumable product using its general identifier. Expect a response either
// through ProcessPurchase or OnPurchaseFailed asynchronously.
BuyProductID(PRODUCT_150000_GOLD);
}
public void buy_400000_Gold()
{
// Buy the consumable product using its general identifier. Expect a response either
// through ProcessPurchase or OnPurchaseFailed asynchronously.
BuyProductID(PRODUCT_400000_GOLD);
}
void BuyProductID(string productId)
{
// If Purchasing has been initialized ...
if (IsInitialized())
{
// ... look up the Product reference with the general product identifier and the Purchasing
// system's products collection.
Product product = m_StoreController.products.WithID(productId);
// If the look up found a product for this device's store and that product is ready to be sold ...
if (product != null && product.availableToPurchase)
{
Debug.Log(string.Format("Purchasing product asychronously: '{0}'", product.definition.id));
// ... buy the product. Expect a response either through ProcessPurchase or OnPurchaseFailed
// asynchronously.
m_StoreController.InitiatePurchase(product);
}
// Otherwise ...
else
{
// ... report the product look-up failure situation
Debug.Log("BuyProductID: FAIL. Not purchasing product, either is not found or is not available for purchase");
}
}
// Otherwise ...
else
{
// ... report the fact Purchasing has not succeeded initializing yet. Consider waiting longer or
// retrying initiailization.
Debug.Log("BuyProductID FAIL. Not initialized.");
}
}
public void OnInitialized(IStoreController controller, IExtensionProvider extensions)
{
// Purchasing has succeeded initializing. Collect our Purchasing references.
Debug.Log("OnInitialized: PASS");
// Overall Purchasing system, configured with products for this application.
m_StoreController = controller;
// Store specific subsystem, for accessing device-specific store features.
m_StoreExtensionProvider = extensions;
}
public void OnInitializeFailed(InitializationFailureReason error)
{
// Purchasing set-up has not succeeded. Check error for reason. Consider sharing this reason with the user.
Debug.Log("OnInitializeFailed InitializationFailureReason:" + error);
}
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
// A consumable product has been purchased by this user.
if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_50000_GOLD, StringComparison.Ordinal))
{
PurchaseFSM.Fsm.Event("Bought50000");
}
else if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_150000_GOLD , StringComparison.Ordinal))
{
PurchaseFSM.Fsm.Event("Bought150000");
}
else if (String.Equals(args.purchasedProduct.definition.id, PRODUCT_400000_GOLD , StringComparison.Ordinal))
{
PurchaseFSM.Fsm.Event("Bought400000");
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
// A product purchase attempt did not succeed. Check failureReason for more detail. Consider sharing
// this reason with the user to guide their troubleshooting actions.
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}
everything works fine til i start a game and go back to main menu, sudenly the script dont work, iff i add dont destroy in the awake, it works but then i creates a duplicate and if i try delete duplicate i end up deleteing the one i need to make the google servive work, do anyone now how to fix this, dont want a lot of duplicated elements to slow down the game?
Thank You