luobin82Robin / dotween

Automatically exported from code.google.com/p/dotween
0 stars 0 forks source link

Add GameObject Shortcuts #31

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
Be able to do things such as

gameObject.DOFade(1,2);

and have DOTween find one of the components that DOFade applies to, apply it 
and return the tweener.

I tried to implement this be scanning the gameOject for a component that the 
method would apply to and returning null if no component could be found. This 
caused problems with tweener setters, such as SetEase, since they do not check 
for null values. My work around for the problem was to return a Tweener that 
did nothing and would kill it self after completing using OnStepComplete, to 
catch any infinitely looping tweens. I imagine their is a lot of over head 
doing it this way and that you could come up with a better way of achieving 
this.

Original issue reported on code.google.com by wcraven501@gmail.com on 10 Mar 2015 at 9:38

GoogleCodeExporter commented 9 years ago
Hi,

I really don't want to tie tweens to gameObjects, because I like that the API 
makes it clear that tweens are based on the given target (which might not be 
related to a gameObject at all) instead than strictly to Unity elements.

That said, tell me more about your implementation, and I'll help making your 
custom code more efficient. Why is SetEase a problem? Can't you check if a 
target/Component is null immediately after creation, before setting the 
parameters?

Original comment by daniele....@gmail.com on 11 Mar 2015 at 10:31

GoogleCodeExporter commented 9 years ago
Here is what I'm doing now. Where I return the suicideTweener I use to
return null, which is where the problem with SetEase came up. I could check
for a null tweener before trying to call SetEase on it, but I kind of just
want it to work without having to worrier about checking for nulls. I
coming from iTween and that was one thing I like about it was that it just
worked. Any advice you could give would be appreciated.

    public static Tweener DOColor(this GameObject gameObject, Color to,
float duration)
    {
        if (gameObject)
        {
            if (gameObject.GetComponent<Camera>())
                return gameObject.GetComponent<Camera>().DOColor(to,
duration);
            if (gameObject.GetComponent<Light>())
                return gameObject.GetComponent<Light>().DOColor(to,
duration);
            if (gameObject.GetComponent<SpriteRenderer>())
                return
gameObject.GetComponent<SpriteRenderer>().DOColor(to, duration);
            if (gameObject.GetComponent<Renderer>())
                return
gameObject.GetComponent<Renderer>().material.DOColor(to, duration);
            if (gameObject.GetComponent<Graphic>())
                return gameObject.GetComponent<Graphic>().DOColor(to,
duration);
        }

        return MakeSuicideTweener();
    }

    public static Tweener MakeSuicideTweener()
    {
        float nothing;
        Tweener t = DOTween.To(() => 0, x => nothing = x, 0, 0);
        t.OnStepComplete(() => t.Kill());

        return t;
    }

-William Craven

Original comment by wcraven501@gmail.com on 12 Mar 2015 at 12:24

GoogleCodeExporter commented 9 years ago
Sorry for the late answer. If your only problem is ease, why don't you add it 
as a parameter to the DOColor method? That way you wouldn't need 
MakeSuicideTweener, and you could do something like this:

    public static Tweener DOColor(this GameObject gameObject, Color to,
float duration, Ease ease = Ease.OutQuad)
    {
        if (gameObject)
        {
            if (gameObject.GetComponent<Camera>())
                return gameObject.GetComponent<Camera>().DOColor(to, duration).SetEase(ease);
            if (gameObject.GetComponent<Light>())
                return gameObject.GetComponent<Light>().DOColor(to, duration).SetEase(ease);
            if (gameObject.GetComponent<SpriteRenderer>())
                return gameObject.GetComponent<SpriteRenderer>().DOColor(to, duration).SetEase(ease);
            if (gameObject.GetComponent<Renderer>())
                return gameObject.GetComponent<Renderer>().material.DOColor(to, duration).SetEase(ease);
            if (gameObject.GetComponent<Graphic>())
                return gameObject.GetComponent<Graphic>().DOColor(to, duration).SetEase(ease);
        }

        return null;
    }

Original comment by daniele....@gmail.com on 13 Mar 2015 at 11:19