Unity Tween

Hey Guys,

I expend some time to do one unity tween class. That is a very simple class and
I’m not so good with c#. But may be that can help with some things.

If you try UnityTween let me know your opinion about class and new improvements.

Cheers

http://www.pedroleonardo.com/blog/?p=189

Considering that your scripts can be as useful for others, as it was for me, it has been posted on the unify wiki at http://www.unifycommunity.com/wiki/index.php?title=Tween

Thanks for your contribution Pedro Sousa!

Can’t believe this got so little attention here, because this is something that I love as a Flash developer!

I’ve been thinking about porting Tweener over from Flash to C# for use in Unity, as I’m a big Tweener fan. I use this in almost every Flash project where I work on.

Kudos for sharing this!

Oh, bring it on! :wink:

… I’m working on AniMate, another tweening library for Unity with a little different approach, it seems. I’ve used TweenLite as inspiration for its interface.

Hey Guys,

Chips thanks for post the class on wiki. Adrian i saw in AniMate documentation you use almost the same syntax of tweener and TweenMax , i was trying do the same in c# but i have not the same result because it’s impossible (i don’t have sure ) instantiate a new monobehaviour and use the update method and i have problems to create a gereic object in c#. You guys think that is possible use a static method to start the engine like in tweener ?

Thanks for comments

Hey Pedro, nice work man!

I`m a flash developer, and newbie to Unity and C#.
I´ve made a small script to call Unitytween by code.
I think isn´t the best way to do it, but solves my problem.

Good to see another brazilian here!

Tweener.cs

using UnityEngine;
using System.Collections;

public class Tweener : MonoBehaviour {
   
    public void tween(GameObject obj, string opt, Vector3 value, float duration, float delay, Ease ease_opt) {

        UnityTween v_tween;

            if (obj.GetComponents<UnityTween>().Length > 2)
            {
                for (int i = 0; i < obj.GetComponents<UnityTween>().Length-1; i++)
                {
                    if (obj.GetComponents<UnityTween>()[i].rotations.Length > 0)
                    {
                        Destroy(obj.GetComponents<UnityTween>()[i]);
                    }
                    if (obj.GetComponents<UnityTween>()[i].positions.Length > 0)
                    {
                        Destroy(obj.GetComponents<UnityTween>()[i]);
                    }
                }

            };

        v_tween = obj.AddComponent<UnityTween>();

        if (opt == "rotation"){
            v_tween.rotations = new TweenRotationObject[1];
            TweenRotationObject[] valor_tween = new TweenRotationObject[1];
            valor_tween[0] = new TweenRotationObject();
            valor_tween[0].delay = delay;
            valor_tween[0].totalTime = duration;
            valor_tween[0].ease = ease_opt;
            valor_tween[0].tweenValue = value;
            v_tween.rotations[0] = valor_tween[0];
        }

        if (opt == "position") {
            v_tween.positions = new TweenPositionObject[1];
            TweenPositionObject[] valor_tween = new TweenPositionObject[1];
            valor_tween[0] = new TweenPositionObject();
            valor_tween[0].delay = delay;
            valor_tween[0].totalTime = duration;
            valor_tween[0].ease = ease_opt;
            valor_tween[0].tweenValue = value;
            v_tween.positions[0] = valor_tween[0];        
        }

    }

}

So I call Unitytween everywhere I wan´t (a button click for example) this way:

Tweener teste = new Tweener();

teste.tween(GameObject.Find("object_name"), "position", new Vector3(2,4,2), 1.0f, 0.0f, Ease.EaseInOutSine);

teste.tween(GameObject.Find("object_name"), "rotation", new Vector3(30,20,90), 1.0f, 0.0f, Ease.EaseInOutSine);

Great!!

A lot of time better than my way.
I was thinking than will be better create a static method and do the call. Let me know what you think is better ?

TweenCall.cs

//Original class from Rodrigo Pegorari
//Tks

using UnityEngine;
using System.Collections;

public class TweenCall : MonoBehaviour {
   
    public static void Tween(GameObject obj, string opt, Vector3 value, float duration, float delay, Ease ease_opt) 
    {
        RemoveFromGameObject (obj);
        switch(opt)
        {
        	case  "position":
        		TranslatePosition(obj, opt, value, duration, delay, ease_opt);
        		break;
        	case  "rotation":
        		TranslateRotation(obj, opt, value, duration, delay, ease_opt);
        		break;
        }
    }
    
    public static void Tween(GameObject obj, string opt, float value, float duration, float delay, Ease ease_opt) 
    {
        RemoveFromGameObject (obj);
        switch(opt)
        {
        	case  "alpha":
        		TranslateAlpha(obj, opt, value, duration, delay, ease_opt);
        		break;
        }
    }
    
    private static void RemoveFromGameObject (GameObject obj)
    {
    	if (obj.GetComponents<UnityTween>().Length > 2)
        {
            for (int i = 0; i < obj.GetComponents<UnityTween>().Length-1; i++)
            {
                if (obj.GetComponents<UnityTween>()[i].rotations.Length > 0)
                {
                    Destroy(obj.GetComponents<UnityTween>()[i]);
                }
                if (obj.GetComponents<UnityTween>()[i].positions.Length > 0)
                {
                    Destroy(obj.GetComponents<UnityTween>()[i]);
                }
            }
        };
    }
    
    private static void TranslateRotation(GameObject obj, string opt, Vector3 value, float duration, float delay, Ease ease_opt)
    {
    	UnityTween v_tween;
    	v_tween = obj.AddComponent<UnityTween>();
  	 	v_tween.rotations = new TweenRotationObject[1];
        TweenRotationObject[] valor_tween = new TweenRotationObject[1];
        valor_tween[0] = new TweenRotationObject();
        valor_tween[0].delay = delay;
        valor_tween[0].totalTime = duration;
        valor_tween[0].ease = ease_opt;
        valor_tween[0].tweenValue = value;
        v_tween.rotations[0] = valor_tween[0]; 
    }
    
    private static void TranslatePosition(GameObject obj, string opt, Vector3 value, float duration, float delay, Ease ease_opt)
    {
    	UnityTween v_tween;
    	v_tween = obj.AddComponent<UnityTween>();
  	 	v_tween.positions = new TweenPositionObject[1];
        TweenPositionObject[] valor_tween = new TweenPositionObject[1];
        valor_tween[0] = new TweenPositionObject();
        valor_tween[0].delay = delay;
        valor_tween[0].totalTime = duration;
        valor_tween[0].ease = ease_opt;
        valor_tween[0].tweenValue = value;
        v_tween.positions[0] = valor_tween[0]; 
    }
    
    private static void TranslateAlpha(GameObject obj, string opt, float value, float duration, float delay, Ease ease_opt)
    {
    	UnityTween v_tween;
    	v_tween = obj.AddComponent<UnityTween>();
  	 	v_tween.alphas = new TweenAlphaObject[1];
        TweenAlphaObject[] valor_tween = new TweenAlphaObject[1];
        valor_tween[0] = new TweenAlphaObject();
        valor_tween[0].delay = delay;
        valor_tween[0].totalTime = duration;
        valor_tween[0].ease = ease_opt;
        valor_tween[0].tweenValue = value;
        v_tween.alphas[0] = valor_tween[0]; 
    }
}

And i call that usinc static method:

TweenCall.Tween(this.gameObject, "position", new Vector3(10f,0f,0f) , 1f , 0f, Ease.Linear);

Cheers

Sure!!! This way is better to use.

I tried to do this but i´m newbie on c# hehehe

Thnx man, this component will be very usefull!

Hey guys
I did some updates in my class. i had problems using that in iphone but now is working fine.

http://www.pedroleonardo.com/blog/?p=216

Cheers

Just wanted to share my new tween engine iTween with you guys in case it could help you out. Let me know what you think! http://www.pixelplacement.com/iTween/

Im having some trouble with the iTween class in C#. It is not available. Although I installed it in the script folder of my project.

Any suggestions?

Thanks,
Oliver

Well there is only a JavaScript version right now. But a C# version is underway. Not sure I know what you mean 100%. Describe a little further and I’ll try to lend a hand.

Oliver, just wanted to let you know that iTween is now available for C#: http://www.pixelplacement.com/iTween/

Just as an update iTween now has bezier movement!
http://www.pixelplacement.com/iTween/

Hi,
is it possible to use iTween to Fade a GUI.Window? …I did that on a cube but I dont know how I can do that for a GUI.Window…this is my GUI.window script:

and this is the script of my object:

thanks
bye

Hey :slight_smile:

My turn to post about a new Tween engine: HOTween.

It is fully object-oriented, built for speed and code-safety/assist, and has many features. It’s quite different from iTween, and they can both work well together.

I would like to share my contribution as well: LeanTween (a super efficient tweening engine for Unity). Speaking of Flash Developers LeanTween now supports Flash Publishing as well (Unity 3.5.x and 4.x). It’s bizarre that as a former Flash developer part of my impetus to create this tweening engine was so that I could do animate things as I did in Flash, using TweenLite. Now things have sort of come full circle where I am doing things in Unity that i wish to support in Flash!

This is not a port of the original engine but the whole engine itself, every feature should work as expected. You can see a demo here:

1219439--50156--$leantweenflash-play1.png

C# is also fully supported with Flash publishing.

Found a bug!!! Do i get a prize!?

In UpdateRotation method in the main class, you set the finish vector3 to the position of the object, not the rotation. This will cause rotation tweens to set position, and not end at their proper rotation value.

Simple fix to replace by setting rotation instead.

Hope this helped someone else!

Have some more feedback for UnityTween:

  • It doesn’t take into account Time.deltaTime properly, so the effect of the tween varies depending on the framerate.
    EDIT: I may be mistaken here after looking at the code. It uses Time.time directly, so it shouldn’t be a problem, and yet I am getting quite different results on the iPad vs the Editor… Herm…

  • I added scale to all the posted scripts, and it seems to work nicely :slight_smile:

  • I don’t understand why it adds a new UnityTween script every time I add a tween. This is fine if it is by design, but why don’t they remove themselves when their tweens are complete?

  • Why does the TweenCall script impose an arbitrary limit of 3 tweens before it removes them all? The above solution of auto-removes when complete sounds best.

  • It’d be nice if I could set the start value.

  • It’d be nice if I could add multiple tweens of the same type that apply additively.