[DOTween]使用过程中的一些注意事项记录
代码环境:
unity:version5.3.4.f1
DOTween:v1.0.312
IDE:Microsoft visual studio Community 2015
DOTween中几个需要注意的变量和函数功能说明:
1 tweens对象完成后会自动销毁,如此我们基本不用关心DOTween的内存销毁问题。
// // Summary: // Default autoKillOnComplete behaviour for new tweens. // Default: TRUE public static bool defaultAutoKill;
2 构建新的tweens后,会自动play。保持该值,我们则无需要手动调用play
// // Summary: // Default autoPlay behaviour for new tweens. // Default: AutoPlay.All public static AutoPlay defaultAutoPlay;
3 默认的运动方式,主要表现是开始执行时,快速,在后期会逐步减速。该算法在执行时间长度比较短时看着比较合理舒适,但是如果出现类似距离较长,时间也相对较长时,就容易发现在后期有点很不好接受的缓慢移动。此时就需要考虑更改运动方式。
// // Summary: // Default ease applied to all new Tweeners (not to Sequences which always have // Ease.Linear as default). // Default: Ease.InOutQuad public static Ease defaultEaseType; // // Summary: // Sets the ease of the tween. // If applied to Sequences eases the whole sequence animation public static T SetEase<T>(this T t, Ease ease) where T : Tween;
4 DOTween中有两套不同的调用方式=》
shortcuts way,但是需要针对不同对象,需要调用不一样对象:
// // Summary: // Tweens a Transform's localPosition to the given value. Also stores the transform // as the tween's target so it can be used for filtered operations // // Parameters: // endValue: // The end value to reach // // duration: // The duration of the tween // // snapping: // If TRUE the tween will smoothly snap all values to integers public static Tweener DOLocalMove(this Transform target, Vector3 endValue, float duration, bool snapping = false); // // Summary: // Tweens a Transform's localRotation to the given value. Also stores the transform // as the tween's target so it can be used for filtered operations // // Parameters: // endValue: // The end value to reach // // duration: // The duration of the tween // // mode: // Rotation mode public static Tweener DOLocalRotate(this Transform target, Vector3 endValue, float duration, RotateMode mode = RotateMode.Fast); // // Summary: // Tweens a Transform's localScale to the given value. Also stores the transform // as the tween's target so it can be used for filtered operations // // Parameters: // endValue: // The end value to reach // // duration: // The duration of the tween public static Tweener DOScale(this Transform target, Vector3 endValue, float duration);
generic way,大部分的执行都可以用同类型的接口。
如下函数,只要是float类型数值,都可以符合调用要求。我个人使用最多的就是应用在alpha上,处理渐隐渐现效果很好。
// // Summary: // Tweens a virtual property from the given start to the given end value and implements // a setter that allows to use that value with an external method or a lambda // Example: // To(MyMethod, 0, 12, 0.5f); // Where MyMethod is a function that accepts a float parameter (which will be the // result of the virtual tween) // // Parameters: // setter: // The action to perform with the tweened value // // startValue: // The value to start from // // endValue: // The end value to reach // // duration: // The duration of the virtual tween public static Tweener To(DOSetter<float> setter, float startValue, float endValue, float duration);
5 关于sequence
Sequences are like Tweeners, but instead of animating a property or value they animate other Tweeners or Sequences as a group.
比较简单的Sequence,可以直接用如下函数增加Append / Join:
// // Summary: // Adds the given tween to the end of the Sequence. Has no effect if the Sequence // has already started // // Parameters: // t: // The tween to append public static Sequence Append(this Sequence s, Tween t); // // Summary: // Inserts the given tween at the same time position of the last tween added to // the Sequence. Has no effect if the Sequence has already started public static Sequence Join(this Sequence s, Tween t);
如果是比较复杂的,建议直接使用insert,如此可以随意的控制加入tween的时间节点
// // Summary: // Inserts the given tween at the given time position in the Sequence, automatically // adding an interval if needed. Has no effect if the Sequence has already started // // Parameters: // atPosition: // The time position where the tween will be placed // // t: // The tween to insert public static Sequence Insert(this Sequence s, float atPosition, Tween t); // // Summary: // Inserts the given callback at the given time position in the Sequence, automatically // adding an interval if needed. Has no effect if the Sequence has already started // // Parameters: // atPosition: // The time position where the callback will be placed // // callback: // The callback to insert public static Sequence InsertCallback(this Sequence s, float atPosition, TweenCallback callback);
6 关于Kill函数:特意提出该接口,是因为针对于Sequence,调用DOTween.Kill("Sequence", true),并不能在kill之前complete,不确定是其本身的bug还是我对接口的理解不对。
// // Summary: // Kills all tweens with the given ID or target and returns the number of actual // tweens killed // // Parameters: // complete: // If TRUE completes the tweens before killing them public static int Kill(object targetOrId, bool complete = false);
以下是一些测试例子和执行后的效果图。
[ContextMenu("DoTweenAlpha")] void DoTweenAlpha() { Debug.Log("DoTweenAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { DOTween.To(x => uiRect.alpha = x, 1.0f, 0.0f, 5.0f).SetId("Tween"); } } [ContextMenu("DoTweenKillCompleteAlpha")] void DoTweenKillCompleteAlpha() { Debug.Log("DoTweenKillCompleteAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { DOTween.Kill("Tween", true); } } [ContextMenu("DoTweenKillAlpha")] void DoTweenKillAlpha() { Debug.Log("DoTweenKillAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { DOTween.Kill("Tween", false); } } [ContextMenu("DoSequenceAlpha")] void DoSequenceAlpha() { Debug.Log("DoSequenceAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { Sequence sequence = DOTween.Sequence(); sequence.Append(DOTween.To(x => uiRect.alpha = x, 1.0f, 0.0f, 5.0f)); sequence.SetId("Sequence"); } } [ContextMenu("DoSequenceKillCompleteAlpha")] void DoSequenceKillCompleteAlpha() { Debug.Log("DoSequenceKillCompleteAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { DOTween.Kill("Sequence", true); } } [ContextMenu("DoSequenceKillAlpha")] void DoSequenceKillAlpha() { Debug.Log("DoSequenceKillAlpha"); UIRect uiRect = m_uiRectAni; if (uiRect != null) { DOTween.Kill("Sequence", false); } }
执行顺序:
DoTweenAlpha->DoTweenKillAlpha(正确kill,并没有complete);DoTweenAlpha->DoTweenKillCompleteAlpha(正确kill,并成功complete)
DoSequenceAlpha->DoSequenceKillAlpha(正确kill,并没有complete);DoSequenceAlpha->DoSequenceKillCompleteAlpha(正确kill,并没有成功complete)
附上一张DoSequenceAlpha->DoSequenceKillCompleteAlpha执行后的效果图:
![](https://images2015.cnblogs.com/blog/471370/201606/471370-20160629162719562-922914710.png)