unity – How to run a DOTween tween on float and apply it to a component on each update?


I am trying to use DOTween by tweening a float and then applying that float to the parameters of a gameObject’s components on each update.

In LeanTween, this was done like this:

LeanTween.value(gameObjectIn, 0f, 1f, 3f).setEase(LeanTweenType.easeInOutQuad).setDelay(0f).setOnUpdate((float val) => {
            gameObjectIn.GetComponent<Image>().color = new Color32(255, 255, 255, (byte)(val * 255));
        });

Or alternatively:

var seq = LeanTween.sequence();
        seq.append(LeanTween.value(gameObjectIn, 0f, 1f, 3f).setEase(LeanTweenType.easeInOutQuad).setDelay(0f).setLoopPingPong().setOnUpdate((float val) => {
            gameObjectIn.GetComponent<Image>().color = new Color32(255, 255, 255, (byte)(val * 255));
        }));

I have tried doing something similar with DOTween like this:

float valueToTween = 0f;
DOTween.To(() => valuetoTween, x => valuetoTween = x, 1, 1).onUpdate((valuetoTween) => {
            gameObjectIn.GetComponent<Image>().color = new Color32(255, 255, 255, (byte)(valuetoTween * 255)); }
            );

However, this is not working. It says regarding onUpdate: Delegate TweenCallback does not take 1 arguments.

I am not that good with C# to figure out what exactly is wrong. How would I do this basic task? ie. Tween a float and on each update run a few lines of script using that float without having a second function defined/stored outside of this code?

The syntax for DOTween tweens is listed here: http://dotween.demigiant.com/documentation.php

Thanks


Addendum:

As per the comments I tried also setting a separate function:

private static void callbackTest(float paramIn, GameObject gameObjectIn){
gameObjectIn.GetComponent<Image>().color = new Color32(255, 255, 255, (byte)(paramIn * 255));
}

and then in my other function:

private static void startTween(GameObject gameObjectIn){
float valueToTween = 0f;
DOTween.To(() => valuetoTween, x => valuetoTween = x, 1, 1).onUpdate(() => callbackTest(valueToTween, gameObjectIn));
}

But this similarly gives the same error and is just less efficient because I need to maintain separate functions.



Source link

More To Explore

Share on facebook
Share on twitter
Share on linkedin
Share on email