Skip to content

Latest commit

 

History

History
188 lines (150 loc) · 5.9 KB

README.md

File metadata and controls

188 lines (150 loc) · 5.9 KB

Made With Unity License Last Commit Repo Size Last Release

What

In-app-purchase support tool for unity games (Unity 2022.3)

How To Install

Add the line below to Packages/manifest.json

for version 1.0.8

"com.wolf-org.in-app-purchasing":"https://github.com/unity-package/in-app-purchasing-unity.git#1.0.8",

dependency extensions-unity-1.0.5

"com.wolf-org.extensions":"https://github.com/unity-package/extensions-unity.git#1.0.5",

Use

Setup

  • Use via MenuItem Unity-Common > IapSettings or shortcut Ctrl + W / Command + W to open IapSettings

Unity_BVkNMfm6fL

  • If you select Runtime Auto Init in IapSettings then IapManager will be created automatically after loading the scene. Otherwise you will have to attach IapManager to the scene to ensure purchases can be made

  • Add data product to list (enter id and select product type), then click Generate Product

  • Select Validate Product and enter Google Play Store Key, then click Obfuscator Key

  • Don't forget add Define Symbol VIRTUESKY_IAP

  • After clicking Generate Product, an IapProduct.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]);
	    }

	}

Handle purchase product

  • Example 1:
        public Button buttonRemoveAds;
        /// <summary>
        /// refresh ui button remove ads
        /// disable buttonRemoveAds if RemoveAds product has been purchased
        /// </summary>
        void RefreshUI()
        {
            buttonRemoveAds.gameObject.SetActive(!IapProduct.IsPurchasedRemoveads());
        }

        /// <summary>
        /// Buy remove ads iap
        /// </summary>
        public void OnClickRemoveAds()
        {
            IapProduct.PurchaseRemoveads().OnCompleted(() =>
            {
                // handle purchase success
                RefreshUI();
            }).OnFailed(() =>
            {
                // handle purchase failed
            });
        }
  • Example 2: You create a new script similar to the demo below. then attach it to LoadingScene
public class HandlePurchaseIap : MonoBehaviour
{
    private void Awake()
    {
        DontDestroyOnLoad(this.gameObject);
        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

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.RestorePurchase();
#endif
    }