-
Notifications
You must be signed in to change notification settings - Fork 2
IAP (In App Purchasing)
- Open tab
In App Purchase
inMagic Panel
-
Add data product to list (enter id and select product type), then click
Generate Product
-
Select
Validate Product
and enterGoogle Play Store Key
, then clickObfuscator Key
-
Don't forget add
Define Symbol
-
After clicking
Generate Product
, anIapProduct.cs
script is generated in the following format
public static class IapProduct
{
public const string ID_REMOVEADS = "com.test.removeads";
public static IapDataProduct PurchaseRemoveads()
{
return IapManager.PurchaseProduct(IapSettings.Instance.IapDataProducts[0]);
}
public static bool IsPurchasedRemoveads()
{
return IapManager.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[0]);
}
public const string ID_1000GEM = "com.test.1000gem";
public static IapDataProduct Purchase1000Gem()
{
return IapManager.PurchaseProduct(IapSettings.Instance.IapDataProducts[1]);
}
public static bool IsPurchased1000Gem()
{
return IapManager.IsPurchasedProduct(IapSettings.Instance.IapDataProducts[1]);
}
}
- Example 1:
public void OnClickRemoveAds()
{
IapProduct.PurchaseRemoveads().OnCompleted(() =>
{
// handle purchase success
}).OnFailed(() =>
{
// handle purchase failed
});
}
public void OnClick1000Gem()
{
IapProduct.Purchase1000Gem().OnCompleted(() =>
{
// handle purchase success
}).OnFailed(() =>
{
// handle purchase failed
});
}
- Example 2: You create a new script similar to the demo below. then attach it to LoadingScene
(From version 1.1.1,
OnPurchaseSuccessEvent
andOnPurchaseFailedEvent
will be called viaIapManager
)
public class HandlePurchaseIap : MonoBehaviour
{
private void Awake()
{
DontDestroyOnLoad(this.gameObject);
IapStatic.OnPurchaseSuccess += HandlePurchaseSuccess;
IapStatic.OnPurchaseFailed += HandlePurchaseFailed;
// From version 1.1.1
IapManager.OnPurchaseSucceedEvent += HandlePurchaseSuccess;
IapManager.OnPurchaseFailedEvent += HandlePurchaseFailed;
}
void HandlePurchaseSuccess(string id)
{
switch (id)
{
case IapProduct.ID_REMOVEADS:
// handle
break;
case IapProduct.ID_1000GEM:
// handle
break;
}
}
void HandlePurchaseFailed(string id)
{
switch (id)
{
case IapProduct.ID_REMOVEADS:
// handle
break;
case IapProduct.ID_1000GEM:
// handle
break;
}
}
}
-
Note: Example 1 is typically used to handle the user interface after a successful or failed purchase and cannot handle restore purchase. Therefore, you should use example 2 to handle logic for tasks such as removing ads, unlocking skins,...
-
Check to see if the product has been purchased (only applies to Non-Consumable items)
public Button buttonRemoveAds;
private void OnEnable()
{
if (IapProduct.IsPurchasedRemoveads())
{
buttonRemoveAds.gameObject.SetActive(false);
}
}
Restore purchase only applies to Non-Consumable items
Restore Purchase is a mandatory feature on iOS to be able to be released to the store.
On Android when you successfully purchased RemoveAds. Then you uninstall your game and reinstall it. If you click buy remove ads again, google play system will report that you already own this item and can't buy it again, now the user has removed ads but the game still displays ads (incorrect). We will need to handle restore purchase of the user's purchased items so that the user avoids this situation.
On Android restore purchase will be called automatically when you reinstall the game via method ConfirmPendingPurchase
call in OnInitialized
. On ios you will need to create a restore purchase button for the user to click
When the restore is successful, it will automatically call the successful purchase callback of each item for further processing for the user
public void OnClickRestorePurchase()
{
#if UNITY_IOS
IapManager.Instance.RestorePurchase();
#endif
}