Cysharp / UniTask

Provides an efficient allocation free async/await integration for Unity.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Func<UniTask<T>> delegate parameter

flowerhouse-dev opened this issue · comments

I'm trying to pass a UniTask<T> method as a parameter using Func.

public static async UniTask<T> Async<T>(Func<UniTask<T>> function)
{
    T returnValue = await function.Invoke();
    return returnValue;
}

But when I call it as follows, it says the argument type does not match.

Argument type 'Cysharp.Threading.Tasks.UniTask<MyType>' is not assignable to parameter type 'System.Func<Cysharp.Threading.Tasks.UniTask<MyType>>'

MyType result = await MyClass.Async<MyType>(UniTaskMethod(myParameter));

private async UniTask<MyType> UniTaskMethod(ParameterType myParameter) 
{
    // async stuff
}

I can't figure out how I can get this to work.

You're calling UniTaskMethod, not passing a delegate to it. Try this.

MyType result = await MyClass.Async<MyType>(() => UniTaskMethod(myParameter));

Duh.. thanks!