Skip to content

nikodemgrz/AsyncAwaitUtilAsPackage

 
 

Repository files navigation

Forked toolkit for Unity3D & C# to be compatible with the package manager.

Package conversion:
Install this package via the package manager by adding the following git url https://github.com/nikodemgrz/AsyncAwaitUtilAsPackage.git.
Afterwards make sure to add the ModestTree.AsyncAwaitUtil.asmdef to your assembly definition file.

Examples Using 'async' and 'Task<>' instead of Coroutines and IEnumerators:

public override async Task<TObject> CreateInstanceAsync(Transform parent = null, bool worldPositionStays = false)
{
    AsyncOperationHandle<GameObject> handle = assetReference.InstantiateAsync(parent, worldPositionStays);
    await handle;
    if (_cancellationTokenSource.IsCancellationRequested)
        return null;

    Object asset = handle.Result;
    AssetReferenceInstances.Add((GameObject)asset);
    return (TObject) asset;
}
public async Task<bool> Insert(float waitTime)
{
    if (IsReady)
        return false;
    
    gameObject.SetActive(true);
    OnInsert.Invoke();
    
    if (_playerWeaponFeelModule != null)
        _playerWeaponFeelModule.OnReload();
    
    await new WaitForSeconds(waitTime);

    return true;
}
    await new WaitForUpdate();
    if (CancellationTokenSource.IsCancellationRequested)
        return;
    await new WaitUntil(() => !_microVerse.IsModifyingTerrain);
    if (CancellationTokenSource.IsCancellationRequested)
        return;

    foreach (Stamp stamp in stamps)
        Destroy(stamp);
    Destroy(_microVerse);
    
    await new WaitForUpdate();
    if (CancellationTokenSource.IsCancellationRequested)
        return;

Calling async Task methods from a non async context require the '.WrapErrors()' to be exexuted in order for the logging to work properly in Unity:

    public override void Dispose()
    {
        foreach (GameObject assetInstance in AssetReferenceInstances)
        {
            if (assetInstance == null)
                continue;
            
            ReleaseInstanceAsync(assetInstance).WrapErrors();
        }
        AssetReferenceInstances.Clear();

        UnloadAssetReference().WrapErrors();
    }

    public override async Task UnloadAssetReference()
    {
        await Task.CompletedTask;

        if (assetReference.Asset != null)
            assetReference.ReleaseAsset();
    }

About

A bunch of code to make using async-await easier in Unity3D as a package for the package manager

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

 
 
 

Contributors

Languages

  • C# 100.0%