A
A
Alexander Pianov2021-06-28 15:25:10
C++ / C#
Alexander Pianov, 2021-06-28 15:25:10

Is it possible to use "async ValueTask" in Unity?

Unity Addressables suggests creating items asynchronously. Wrote the following code:

public async void SpawnItem(ItemInfo itemInfo, Transform parentPos)
    {
        GameObject item = await SpawnAsset(itemInfo.itemName, parentPos);
        //действия с item
    }

    public async ValueTask<GameObject> SpawnAsset(string itemName, Transform spawnPoint)
    {
        for (int i = 0; i < addressableAssets.Count; i++)
        {
            string storeName = LocationShortName(addressableAssets[i].InternalId);

            if (itemName != storeName) continue;

            GameObject spawnedObject = await Addressables.InstantiateAsync
                (addressableAssets[i], spawnPoint.position, spawnPoint.rotation, spawnPoint).Task;
            
            spawnedObjects.Add(spawnedObject);
            return spawnedObject;
        }

        return null;
    }


By default, the ValueTask type is not available. To use it, I install the System.Threading.Tasks.Extensions package via NuGet and ValueTask becomes available.

However, after saving the code and switching to the Unity editor, errors appear in the console.
60d9be0665040720511344.png

After that, the ValueTask package becomes unavailable again. After removing it via NuGet and reinstalling it, the picture repeats itself: everything works in vs, but as soon as the script is opened in unity, the package becomes unavailable. What could be the reason?

60d9bf4195b16285427716.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily Bannikov, 2021-06-28
@pr9niks

You can't do it in a unit, because ValueTask is too new a feature.
But you probably do n't need it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question