AsyncBehaviour

c# unity async

I've played a bit with alexzzzz C#6 Support for Unity and found that there was one essential thing missing: A class extending MonoBehaviour providing access to several async method similar to Xenkos SyncScript and AsyncScript.

Unfortunately I've modified alexzzzz AsyncTools. So not everything is of my extended script is here. But I hope someone might learn with it.

AsyncBehaviour.cs

[Serializable]
public partial class AsyncBehaviour : MonoBehaviour
{
    public T AddComponent<T>()
        where T : Component
    {
        return gameObject.AddComponent<T>();
    }

    public Component AddComponent(Type type)
    {
        return gameObject.AddComponent(type);
    }

    public async Task<Component> AddComponentAsync(Type type)
    {
        await MainThread;
        return gameObject.AddComponent(type);
    }

    public async Task<T> AddComponentAsync<T>()
        where T : Component
    {
        await MainThread;
        return gameObject.AddComponent<T>();
    }

    public async Task<T> GetComponentAsync<T>()
    {
        await MainThread;
        return GetComponent<T>();
    }

    public async Task<T[]> GetComponentsAsync<T>()
    {
        await MainThread;
        return GetComponents<T>();
    }

AsyncBehaviour.Properties.cs

public partial class AsyncBehaviour
{
    public static AsyncTools.Awaiter ToFixedUpdate => AsyncTools.ToFixedUpdate();
    public static TaskScheduler FixedUpdateScheduler => UnityScheduler.FixedUpdateScheduler;
    public static bool IsMainThread => AsyncTools.IsMainThread();
    public static AsyncTools.Awaiter ToLateUpdate => AsyncTools.ToLateUpdate();
    public static TaskScheduler LateUpdateScheduler => UnityScheduler.LateUpdateScheduler;
    public static AsyncTools.Awaiter ToThreadPool => AsyncTools.ToThreadPool();
    public static TaskScheduler ThreadPoolScheduler => UnityScheduler.ThreadPoolScheduler;
    public static AsyncTools.Awaiter ToUpdate => AsyncTools.ToUpdate();
    public static TaskScheduler UpdateScheduler => UnityScheduler.UpdateScheduler;

    public static Task RunMainThread(Action action)
    {
        return Task.Factory.StartNew(action, CancellationToken.None, TaskCreationOptions.None, UpdateScheduler);
    }

    public static Task<T> RunMainThread<T>(Func<T> func)
    {
        return Task.Factory.StartNew(func, CancellationToken.None, TaskCreationOptions.None, UpdateScheduler);
    }

    public static Task RunMainThread(Action action, CancellationToken cancellationToken)
    {
        return Task.Factory.StartNew(action, cancellationToken, TaskCreationOptions.None, UpdateScheduler);
    }

    public static Task<T> RunMainThread<T>(Func<T> func, CancellationToken cancellationToken)
    {
        return Task.Factory.StartNew(func, cancellationToken, TaskCreationOptions.None, UpdateScheduler);
    }
}   

This enables every script inheriting from AsyncBehaviour to use some of Unitys Methods while beeing in a async task. It shortens lines of codes dramatically and reduces redundant calls.

So much for today.

Previous Post Next Post