Hi, I’m making a IAP function in my game, however I’m having problems with my PowerUp function. You see I have a PowerUp button that appears, once I click on a my purchase button. The problem I’m facing with the PowerUp button is that I only want my PowerUp button to be available to however many PowerUps the user has purchase. So say like if the user purchase about 5 PowerUps (this is an example, the user may want to purchase however many he or she may wants) then the user has about 5 PowerUps to use whenever they want only after when the user uses all of their PowerUps then the button is disabled again. Is it possible?
This is my purchase code:
private static IStoreController m_StoreController;
private static IExtensionProvider m_StoreExtensionProvider;
public static string PRODUCT_FORCESHIELD = "forceshield";
private static string kProductNameAppleSubscription = "com.unity3d.subscription.new";
private static string kProductNameGooglePlaySubscription = "com.unity3d.subscription.original";
private void Start()
{
if (m_StoreController == null)
{
InitializePurchasing();
}
}
public void InitializePurchasing()
{
if (IsInitialized())
{
return;
}
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(PRODUCT_FORCESHIELD, ProductType.Consumable);
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
public void BuyForceShield()
{
BuyProductID(PRODUCT_FORCESHIELD);
}
private 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");
}
}
else
{
Debug.Log("BuyProductID FAIL. Not initialized.");
}
}
public void RestorePurchases()
{
if (!IsInitialized())
{
Debug.Log("RestorePurchases FAIL. Not initialized.");
return;
}
if (Application.platform == RuntimePlatform.IPhonePlayer ||
Application.platform == RuntimePlatform.OSXPlayer)
{
// ... begin restoring purchases
Debug.Log("RestorePurchases started ...");
var apple = m_StoreExtensionProvider.GetExtension<IAppleExtensions>();
apple.RestoreTransactions((result) => {
Debug.Log("RestorePurchases continuing: " + result + ". If no further messages, no purchases available to restore.");
});
}
else
{
Debug.Log("RestorePurchases FAIL. Not supported on this platform. Current = " + Application.platform);
}
}
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 (String.Equals(args.purchasedProduct.definition.id, PRODUCT_FORCESHIELD, StringComparison.Ordinal))
{
Debug.Log("Purchase Successfull");
//PowerUpScript.PurchaseForceShield += 1;
PowerUpScript.ForceShield = PowerUpScript.ForceShield +1;
}
else
{
Debug.Log(string.Format("ProcessPurchase: FAIL. Unrecognized product: '{0}'", args.purchasedProduct.definition.id));
}
return PurchaseProcessingResult.Complete;
}
public void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}
This is my PowerUp button script that activates my players forceshield using onClick:
public Button ActivateForceShield;
public Text PurchaseForceShield;
public static int ForceShield;
// Use this for initialization
void Start () {
ForceShield = 0;
SetScoreText ();
}
public void ActivatePowerUp()
{
ActivateForceShield.gameObject.SetActive(true);
}
// Update is called once per frame
void Update()
{
SetScoreText ();
}
void SetScoreText ()
{
PurchaseForceShield.text = " " + ForceShield.ToString ();
}
Lastly this is my ForceShield script, this just create the forceshield around my player:
public bool forceActive = false;
bool isCoroutineStarted = false;
public GameObject LeftUp, RightUp, LeftDown, RightDown, ForceSphere;
public void ActivateForceShield ()
{
if (! isCoroutineStarted) {
StartCoroutine (CreateForceShield ());
forceActive = true;
}
}
IEnumerator CreateForceShield ()
{
//if (forceActive) { yield break;}
isCoroutineStarted = true;
LeftUp.SetActive (true);
RightUp.SetActive (true);
yield return new WaitForSeconds (1.0f);
LeftDown.SetActive (true);
RightDown.SetActive (true);
yield return new WaitForSeconds (1.0f);
LeftUp.SetActive (false);
RightUp.SetActive (false);
yield return new WaitForSeconds (1.0f);
LeftDown.SetActive (false);
RightDown.SetActive (false);
//yield return new WaitForSeconds (1.0f);
ForceSphere.SetActive (true);
yield return new WaitForSeconds (10.0f);
ForceSphere.SetActive (false);
forceActive = false;
}
Thank you.