[DOTween]使用过程中的一些注意事项记录
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
[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执⾏后的效果图:。