Demigiant / dotween

A Unity C# animation engine. HOTween v2
http://dotween.demigiant.com
Other
2.37k stars 352 forks source link

Create tween in editor and copy it at runtime to different targets(PRO) #234

Open metamorphling opened 6 years ago

metamorphling commented 6 years ago

I feel like I stumbled upon a bug, but I'm not sure since I'm new to dotween. 1) Create "TemplateAnimation" GameObject, add DOTween Animation component to it. Set tween up, give it ID "1". 2) Create "AnimatableObject" GameObject, add DOTween Animation component to it and script

var animations = DOTween.TweensById("1");
var anim = GetComponent<DOTweenAnimation>();
anim.CreateTween();
anim.tween.SetAs(animations[0]);

Basically, I've tried every possible combination: 1) Trying to change animations[0].target to transform of "AnimatableObject" 2) Trying to create Tween in runtime and copy values from tween of "TemplateAnimation" 3) Trying to copy DOTween Animation itself and etc.

Is there any possible way to copy tween created from UI script "DOTween Animation" to a runtime tweens created in script? Basically, that was the only reason I bought PRO version. Creating a bunch of animations with "DOTween Animation" and then just applying them at runtime by animation ID sounds like a good approach. Turned out into a wasted half of a day :( Unity 2018.2.0f2 DOTween v1.1.640 DOTweenPro v0.9.690

Demigiant commented 6 years ago

Ooops, sorry, I replied via mail but apparently nothing got written here :B

Anyway, I'm making some tests right now. There is no direct way to copy one tween onto another (never even thought about it), but I have an idea I want to try.

metamorphling commented 6 years ago

Cool thanks! Pooling animations is such an awesome feature, it takes too much time to copy and setup DO Tween Animation component to every gameobject I want to animate. Now that got me thinking, what if DOTweenAnimation can have an "export" button? Like, setting with script all the needed stuff, pressing "export" and user gets to save an autogenerated animation script. Or just getting shown a template in text field of what should be copied into a script. That way one can easily create pool of needed animation scripts, also this way kills an overhead of setting tweens at runtime.

metamorphling commented 6 years ago

Congrats on v1.0.041 release! It's really cool now that I can pool my animations through manager. A few notes from me: 1) If you set target for DOTweenAnimation at runtime, you should run the same sequence as "Commit changes and restart".

        private void TweenCommitAndRestart()
        {
//animeManager is a singleton which owns gameobjects containing animations, 
//so when I ask for ID, manager runs through its library of premade animations 
//and returns DOTweenAnimation
            anim = animeManager.GetAnimation(id);

            anim.targetIsSelf = false;
            anim.targetGO = gameObject;
            anim.tween.Rewind();
            anim.tween.Kill();
            if (anim.isValid)
            {
                anim.CreateTween();
            }
        }

2) Check for "targetGO == null" in inspector script turns off animation settings. I believe it assumes that GameObject can not be set at runtime, which is not true. To negate it's behaviour I set a dummy object from the scene as its target, but this is not a proper way. 3) I have not yet tried to run animation from one script for few gameobjects, but at the moment I believe it's possible to play script for one gameobject only. It would be nice to set as many targets as user wants. Not an easy task I guess, knowing how hard things get under the hood. Newbie idea here, but maybe gameobject registers itself as a target to animator script(with ID maybe?) and when gameobject asks to Play(), script deploys tweener to gameobject who asked for it.

Demigiant commented 6 years ago

Ahoy! :)

These are all very good tips, and my next update planned (now that I also added animation previews and it's ready for the next submission) is exactly this. The new "other target gameObject" option should help there, though I'm still undecided how to do it, because the calculations to determine the type of tween (for example, in case of Color, if it should be a Material.DOColor or SpriteRenderer.DOColor, depending on the type of components on the target gameObject) are done in-editor and not at runtime. Gonna experiment a little (though slowly because next days will be pretty busy with other things).

metamorphling commented 6 years ago

First things first, sorry for necroing the thread.

in case of Color, if it should be a Material.DOColor or SpriteRenderer.DOColor

I think user who wants to target some specific object already have in mind what kind of Component is going to be tweened. So, if Target==Other and we choose Color tweening, script gives an option to choose what Component is it going to be, Material or SpriteRenderer. Adding an interface like DeployTween() with a bunch of overloads will cover most cases. Something like:

DeployTween(GameObject target, DOTweenAnimation animation) DeployTween(GameObject target, DOTweenAnimation animation, Type type)

Again, the whole point of it is to have a pool of premade by script animations, so that they can be deployed at the runtime to different targets. This allows for faster development iterations and overall generalization of animation's workflow. I know this is a very specific stuff and not sure if DOTweenPro even supposed to have this tween deployment functionality. The fact that I can write a bunch of DOTween behaviours and pool them, but can't do the same stuff with PRO script just bugs me to no end :)

alaslipknot commented 3 years ago

Reviving this thread,

@Demigiant , has this been fixed in any way please ? I am creating a bunch of DoTweenAnimation object in the editor that i wanna save as prefab, and then assign them at runtime to different objects when needed, what is the best approach ?

Thanks!