Wait until Coroutine finish (Like Thread.Join)

I have a situation with loading asset asynchronously and synchronously.
Functions are belows.

Object LoadAssetSync(string assetPath){
     if(enumer != null){
          while(enumer.MoveNext() == true){}
     }else{
          resultObj = Resources.Load("SomePrefab");
     }
     return resultObj;
}

IEnumerator enumer;
Object resultObj;
void LoadAssetAsync(){
    enumer = WaitForLoad();
    StartCoroutine(enumer);
}

IEnumerator WaitForLoad(){
     var oper = Resources.LoadAssetAsync("SomePrefab");
     while(oper.isDone == false)
        yield return null;
     resultObj = oper.object;
}

When LoadAssetAsync is triggered, assume it takes 10 seconds. When I trigger LoadAssetSync at 3 sec, LoadAssetSync has to wait for Coroutine to finish, and get the resources. And I’ve tried with enumer.MoveNext to do the job but it doesn’t work.
I know it works when you just call Resources.LoadAsset while Resources.LoadAssetAsync. But this is just an example.

I’m doing this kinda thing not Resources.LoadAsset, but WWW.LoadCacheOrDownload assetbundle. When assetbundle is loading asynchronously, and sync assetbundle kicks in, then it shows ‘you have loaded another assetbundle. dont do it’ kind of message.

So my question is this.
How can I do something Thread.Join thingy with coroutines?
I think while(enumer.MoveNext()) blocks the thread and Resources.LoadAssetAsync is blocked also.
Is there any way I can achieve this?

I dont know if this is what you need but within an IEnumerator you can

yield return StartCoroutine(SomeCoroutineMethod());

And it will wait for that to finish before continuing execution.

1 Like