-
Notifications
You must be signed in to change notification settings - Fork 217
Description
问题:ILRuntime层调用async方法时,如果方块内没有await代码块,则不会有返回,请问下这个问题如何处理?
复现方式:
1、我们使用的是这个链接下的实例,https://github.com/Ourpalm/ILRuntimeU3D/tree/master/ILRuntimeDemo。
2、在这个实例中,我们加入了一个处理async/await的跨域适配器,该适配器代码已打包成zip。
AsyncStateMachineAdaptor.zip
3、我们在Assets/Samples/ILRuntime/2.1.0/_Scenes/Examples/01_HelloWorld的场景里的nitializeILRuntime()方法中加入跨域注册的代码:appdomain.RegisterCrossBindingAdaptor(new AsyncStateMachineAdaptor());

4、我们在ILRuntime的工程中的StaticFunTest()中加入逻辑代码:
public static async void StaticFunTest()
{
UnityEngine.Debug.Log("!!! InstanceClass.StaticFunTest()");
string s = await SetAsyncTest(false);
UnityEngine.Debug.Log(s);
UnityEngine.Debug.Log("!!! 111111111");
}
public static async Task<string> SetAsyncTest(bool flag)
{
if (flag == true)
{
await Task.Delay(1000);
UnityEngine.Debug.Log("测试有await");
return "测试有awaitReturn";
}
else
{
//如果是执行这段代码,如是没有下面的await Task.Yield()代码块,则不能接收到return的返回,
//当加入await Task.Yield()代码块后,才能正常的返回。
//尝试使用Task.FromResult("");也不行。
//await Task.Yield();
UnityEngine.Debug.Log("测试没有await");
return "测试没有awaitReturn";
}
}

5、我们获取到测试结果:
当以上方法中flag=true的时候,方法中有await Task.Delay代码块,能正常获取到返回值。
当以上方法中flag=false的时候,方法中没有await Task代码块,无法正常获取到返回,也就是说
StaticFunTest方法中的如下代码不会执行。
UnityEngine.Debug.Log(s);
UnityEngine.Debug.Log("!!! 111111111");