Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Check if user bought a non consumable

Discussion in 'Unity IAP' started by Mars91, Nov 13, 2018.

  1. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    564
    Hi,
    I'm adding Unity IAP to my game I will use it to allow the user to unlock the full game.
    The idea is to have a single splash screen that check if the user bought the non consumable:
    - If the user bought it just move to the main menu
    - If the user haven't bought it I move to aa "Try/Unlock Full Game" scene

    I know that on Android there's no need to restore a purchase but I still need to check if the product has been bought or not.

    I actually thought about using this code

    Code (CSharp):
    1.  
    2.             if (product != null && product.hasReceipt)
    3.             {
    4.                 // Move to full main menu
    5.             }
    6.             else if(product != null && !product.hasReceipt)
    7.             {
    8.                 // Move to unlock game menu
    9.             }
    Do you think this can fit my needs?
     
  2. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    564
    After investingating it looks like that the best way to handle this issue is to save that in the PlayerPrefs when ProcessPurchase.
    From what I understood ProcessPurchase is called when a purchase is done for the first time and when the game is installed again/for the first time, is that right?
    This mean that unity will automatically call this function and that there's no need to manually call it?
    I just need an object with the IAP handler script assigned.

    Code (CSharp):
    1. public PurchaseProcessingResult ProcessPurchase(PurchaseEventArgs args)
    2.     {
    3.         if (String.Equals(args.purchasedProduct.definition.id, kProductIDNonConsumable, StringComparison.Ordinal))
    4.         {
    5.             PlayerPrefs.SetInt("keyGameUnlocked", 1);
    6.         }
    7.         return PurchaseProcessingResult.Complete;
    8.     }
     
  3. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    That is one solution. Keep in mind that PlayerPrefs can be deleted during reinstall. We are working on an improved restore option for Android, hopefully it will be included in the next release.
     
  4. Mars91

    Mars91

    Joined:
    Mar 6, 2012
    Posts:
    564
    Thank you for your answer can you confirm that ProcessPurchase will be called by Unity and there's no need for me to manually call it right?
     
  5. JeffDUnity3D

    JeffDUnity3D

    Unity Technologies

    Joined:
    May 2, 2017
    Posts:
    14,446
    During a reinstall, yes. You should never call ProcessPurchase directly.
     
    Last edited: Nov 13, 2018