- Home /
Question by
Conqueror1 · May 13, 2020 at 08:44 AM ·
adsdisableiap
Disable unity ads upon Purchasing.
Hello! This seems to be a growing question. I am not sure how to disable the advertisements once the purchase of ad removal is made. using System; using UnityEngine; using UnityEngine.Advertisements; using UnityEngine.Purchasing;
public class IAPManager : MonoBehaviour, IStoreListener
{
public static IAPManager instance;
private static IStoreController m_StoreController;
private static IExtensionProvider m_StoreExtensionProvider;
public string no_Ads = "no_ads";
//Step 1 create your products
//************************** Adjust these methods **************************************
public void InitializePurchasing()
{
if (IsInitialized()) { return; }
var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
builder.AddProduct(no_Ads, ProductType.NonConsumable);
//Step 2 choose if your product is a consumable or non consumable
UnityPurchasing.Initialize(this, builder);
}
private bool IsInitialized()
{
return m_StoreController != null && m_StoreExtensionProvider != null;
}
//Step 3 Create methods
public void BuyNoAds()
{
//No Ads call from button
BuyProductID(no_Ads);
}
//Step 4 modify purchasing
public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
{
if (String.Equals(args.purchasedProduct.definition.id, no_Ads, StringComparison.Ordinal))
{
Debug.Log("You've just purchased NO ADS");
}
else
{
Debug.Log("Purchase Failed");
}
return PurchaseProcessingResult.Complete;
}
//**************************** Dont worry about these methods ***********************************
private void Awake()
{
TestSingleton();
}
void Start()
{
if (m_StoreController == null) { InitializePurchasing(); }
}
private void TestSingleton()
{
if (instance != null) { Destroy(gameObject); return; }
instance = this;
DontDestroyOnLoad(gameObject);
}
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)
{
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 void OnPurchaseFailed(Product product, PurchaseFailureReason failureReason)
{
Debug.Log(string.Format("OnPurchaseFailed: FAIL. Product: '{0}', PurchaseFailureReason: {1}", product.definition.storeSpecificId, failureReason));
}
}
Comment
Your answer
![](https://koobas.hobune.stream/wayback/20220612234123im_/https://answers.unity.com/themes/thub/images/avi.jpg)