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:
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:
public void OnInitialized (IStoreController controller, IExtensionProvider extensions) {
// Purchasing has succeeded initializing. Collect our Purchasing references.
Debug.Log ("OnInitialized: PASS");
// Overall Purchasing system, configured with products for this application.
m_StoreController = controller;
// Store specific subsystem, for accessing device-specific store features.
m_StoreExtensionProvider = extensions;
StartCoroutine (FindShopCanvasToSetUpLocalPrice ());
}
IEnumerator FindShopCanvasToSetUpLocalPrice () {
while (!IsInitialized () || ShopCanvasScript.instance == null) {
yield return null;
}
priceProduct1 = m_StoreController.products.WithID ("product1").metadata.localizedPriceString;
priceProduct2 = m_StoreController.products.WithID ("product2").metadata.localizedPriceString;
ShopCanvasScript.instance.SetUpBuyButton1 ();
ShopCanvasScript.instance.SetUpBuyButton2 ();
}
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.
ShopCanvasScript.instance.SetUpBuyButton1 ();
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…
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.
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