Home > database >  how can I wait for the Initialize() finish then execute the code?
how can I wait for the Initialize() finish then execute the code?

Time:09-28

My code needs to wait until the Initialize() then can be execute. I use WaitForSeconds(1.0f) until Initialize() like below , but it saw no good solution.

    IEnumerator waiuntilInit()
    {
        Initialize();
        yield return new WaitForSeconds(1.0f);
        dosomeThing();
    }

how can I wait for the Initialize() finish then execute the code?

void waitInit(){
  wait  Initialize()
  dosomeThing();
}
Initialize()
{
     var builder = ConfigurationBuilder.Instance(StandardPurchasingModule.Instance());
     builder.Configure<IGooglePlayConfiguration>().SetDeferredPurchaseListener(OnDeferredPurchase);
     builder.Configure<IGooglePlayConfiguration>().SetQueryProductDetailsFailedListener(MyAction);
     builder.AddProduct(productId, ProductType.Subscription);
     UnityPurchasing.Initialize(this, builder);
};

CodePudding user response:

You could use WaitUntil inside your coroutine.

The code sample for Purchaser script for unity purchasing comes with a method (at least I don't remember writting it myself), that checks if it was initialized:

public bool IsInitialized()
{
    // Only say we are initialized if both the Purchasing references are set.
    return m_StoreController != null && m_StoreExtensionProvider != null;
}

You now got a condition, that you should wait for, now it's time for writting a corotuine:

IEnumerator waiuntilInit()
{
    Initialize();
    yield return new WaitUntil( () => IsInitialized());
    dosomeThing();
}

If you don't like corotutines, you can apply the same idea of waiting with task execution until both m_StoreController and m_StoreExtensionProvider are not null using async/await or doing it in update (with appropriate flags and timers to ensure you do it once) if you wish.

CodePudding user response:

  1. using async/await not coroutine.
  • Related