Search Unity

[Solved] Purchasing works but can't get localprice from iOS and Android

Discussion in 'Unity IAP' started by vorora, Jul 26, 2017.

Thread Status:
Not open for further replies.
  1. vorora

    vorora

    Joined:
    Mar 17, 2015
    Posts:
    34
    I tried to do as this thread (https://forum.unity3d.com/threads/unity-iap-get-item-price.391448/) but it didn't work no matter how many times I tried. I have no idea what's wrong and can't go on.

    Here is my code (mostly from https://unity3d.com/learn/tutorials/topics/ads-analytics/integrating-unity-iap-your-game):

    On PurchasingManager script (singleton, has DontDestroyOnLoad in pre-load scene(1st scene)), I create string variables to get currency and price of the products in the stores:

    Code (CSharp):
    1. public void OnInitialized (IStoreController controller, IExtensionProvider extensions)
    2. {
    3.         Debug.Log ("OnInitialized: PASS");
    4.         m_StoreController = controller;
    5.         m_StoreExtensionProvider = extensions;
    6.  
    7.         testPriceProduct1 = m_StoreController.products.WithID ("product1").metadata.isoCurrencyCode + " " + m_StoreController.products.WithID ("product1").metadata.localizedPrice.ToString ();
    8.         testPriceProduct2 = m_StoreController.products.WithID ("product2").metadata.isoCurrencyCode + " " + m_StoreController.products.WithID ("product2").metadata.localizedPrice.ToString ();
    9. }

    On ShopCanvas script (in menu scene (2nd scene)), I change texts on the button using those variables.

    Code (CSharp):
    1. void Start ()
    2. {
    3.         product1Price.text = PurchasingManager.instance.testPriceProduct1;
    4.         product2Price.text = PurchasingManager.instance.testPriceProduct2;
    5.  }
    In Editor both texts say 'USD 0.01'. But when I test on devices (iOS and Android), the texts are empty. However I can still purchase successfully.

    Any suggestions would be appreciated. Thank you very much.
     
  2. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @vorora

    Is the text correct in the OnInitialized function? If so, then maybe the Start method is called before the Purchasing system is initialized.
     
  3. vorora

    vorora

    Joined:
    Mar 17, 2015
    Posts:
    34
    It works now (at least on Android). The reason the prices didn't show up before is like @ap-unity suggested. So I use a coroutine and while loop to loop until it's initialized and found ShopCanvas. (The PurchaserScript is in scene 0 and ShopCanvasScript scene 1).

    I'm not sure though that I did the right way (I've never written something this complicate before). I'm concerned that this may affect the performance or have some other problems. If anyone have suggestion to make it better, that'd be great. Thank you.


    PurchaserScript:
    Code (CSharp):
    1. public void OnInitialized (IStoreController controller, IExtensionProvider extensions) {
    2.         // Purchasing has succeeded initializing. Collect our Purchasing references.
    3.         Debug.Log ("OnInitialized: PASS");
    4.  
    5.         // Overall Purchasing system, configured with products for this application.
    6.         m_StoreController = controller;
    7.         // Store specific subsystem, for accessing device-specific store features.
    8.         m_StoreExtensionProvider = extensions;
    9.  
    10.         StartCoroutine (FindShopCanvasToSetUpLocalPrice ());
    11.     }
    12.  
    13.  
    14. IEnumerator FindShopCanvasToSetUpLocalPrice () {
    15.         while (!IsInitialized () || ShopCanvasScript.instance == null) {
    16.             yield return null;
    17.         }
    18.  
    19.         priceProduct1 = m_StoreController.products.WithID ("product1").metadata.localizedPriceString;
    20.         priceProduct2 = m_StoreController.products.WithID ("product2").metadata.localizedPriceString;
    21.  
    22.         ShopCanvasScript.instance.SetUpBuyButton1 ();
    23.         ShopCanvasScript.instance.SetUpBuyButton2 ();
    24.     }
     
  4. vorora

    vorora

    Joined:
    Mar 17, 2015
    Posts:
    34
    Unfortunately, it doesn't show local prices on the buttons every time (works around 70-80% when I open the app). Sometimes the following functions are never called.
    1. ShopCanvasScript.instance.SetUpBuyButton1 ();
    2. ShopCanvasScript.instance.SetUpBuyButton2 ();
    I think I did the structure of the code wrong.

    How can I call SetUpBuyButton1 () and SetUpBuyButton2 () only after m_StoreController is finished initializing? I'm so confused right now...
     
  5. ap-unity

    ap-unity

    Unity Technologies

    Joined:
    Aug 3, 2016
    Posts:
    1,519
    @vorora,

    Without seeing your ShopCanvasScript script, I won't be able to give a specific answer. However, it is most likely an issue with the asynchronous nature of the Purchasing system.

    The Purchasing system is asynchronous, which means the rest of your code can run without being blocked while waiting for the Purchase system to initialize. So while that is happening, your ShopCanvasScript script is running and it does not have the prices yet.

    It looks like the Coroutine you wrote was in the Purchasing script. However, that code would probably be best in the ShopCanvasScript script. You would want ShopCanvasScript to wait until the Purchasing system is initialized and then set the price strings.
     
  6. vorora

    vorora

    Joined:
    Mar 17, 2015
    Posts:
    34
    I moved PurchaserScript to the same scene as ShopCanvasScript's scene and only set the prices on the buttons only after m_StoreController is initialized. I tested on both devices and it seemed to work now. Thank you. @ap-unity
     
    ap-unity likes this.
Thread Status:
Not open for further replies.