Giter VIP home page Giter VIP logo

Comments (8)

drew-512 avatar drew-512 commented on May 27, 2024 1

I'm shooting for the "ProtoPromise pure certified" logo seal thing. I hear there's a promo merch gift bag thing.

from protopromise.

timcassell avatar timcassell commented on May 27, 2024

There are multiple ways you can do it. You could keep using UnityAsync and just wrap that await in an async Promise<T> function. Or you can do it just with Promises:

public static class AssetBundleHelper
{
    public static async Promise<T> LoadAssetAsync<T>(AssetBundle bundle, string nameId) where T : Object
    {
        var request = bundle.LoadAssetAsync<T>(nameId);
        await PromiseYielder.WaitFor(request);
        return request.asset as T;
    }

    public static async Promise<T> LoadAssetAsyncWithProgress<T>(AssetBundle bundle, string nameId) where T : Object
    {
        var request = bundle.LoadAssetAsync<T>(nameId);
        while (!request.isDone)
        {
            await PromiseYielder.WaitOneFrame().AwaitWithProgress(request.progress);
        }
        return request.asset as T;
    }
}

I didn't write any Promise wrappers for Unity-specific async operations, except I wrote a helper to convert any yield instruction (in a coroutine) to a Promise via PromiseYielder.WaitFor. Since AssetBundleRequest is a yield instruction, it works for it.

I wanted to show an example for canceling the async operation, but it appears that API does not support aborts. For abortable operations, you can take a look at the Demo for an example.

from protopromise.

timcassell avatar timcassell commented on May 27, 2024

I took a look at the UnityAsync library, and while it looks abandoned, it should work well with ProtoPromise. They do some optimization tricks that I didn't do in my PromiseYielder (I pool some objects, but I call StartCoroutine under the hood, which allocates). I think that should be fine to use until Unity has updated their APIs for async/await goodness (2023).

from protopromise.

drew-512 avatar drew-512 commented on May 27, 2024

Ok great -- and like that you totally checked it out and looked hard at it. Your comments are super helpful thank you.

from protopromise.

drew-512 avatar drew-512 commented on May 27, 2024

I think that should be fine to use until Unity has updated their APIs for async/await goodness (2023).

Have they said that's coming? They seem to be behind on a lot and the internal downsizing can't be helping.

Sad they are not dialing up engineering investment more considering that signs point to in ~5 years the platforms of choice will be Unity and Unreal (or what becomes of Unreal when part of Tangent is sold or otherwise acquired to make it "safe" for U.S. consumption) -- not the native OSes.

I digress, but I do think that makes a ProtoPromise a great candidate for long term success.

Best,
Drew

from protopromise.

timcassell avatar timcassell commented on May 27, 2024

I think that should be fine to use until Unity has updated their APIs for async/await goodness (2023).

Have they said that's coming?

It's already done in the pre-release. https://docs.unity3d.com/2023.1/Documentation/ScriptReference/Awaitable.html

Although, it looks like Unity dropped the ball once again... The Awaitable type is a class, and the static functions like Awaitable.NextFrameAsync() return Awaitable, so they probably allocate. It looks like they copied Task too closely, and then refused to even call it UnityTask. Ugh.

from protopromise.

timcassell avatar timcassell commented on May 27, 2024

Since Unity did such a terrible job of it, I think I'll add more optimized PromiseYielder functions for common Unity awaits (WaitFrames(int), WaitFor(TimeSpan), WaitFor(AsyncOperation), WaitForFixedUpdate, WaitForEndOfFrame, etc). That way you won't have to rely on UnityAsync for optimal efficiency.

from protopromise.

drew-512 avatar drew-512 commented on May 27, 2024

So strange and sad they are so careless about that stuff.

Psyched you're on the case!

from protopromise.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.