下载资产

在Package Manager中下载DOTween Pro,然后导入,在弹出的窗口中选择“Setup DOTween”

Setup

然后点击Apply

Apply

DOTween的资产自动放在Plugins文件夹里,Dotween Utility Panel在Tools——Demigiant——Dotween Utility Panel窗口中打开。

引入命名空间

1
using DG.Tweening;

初始化DOTween(可选)

1
DOTween.Init(false, false, LogBehaviour.ErrorsOnly); 

初始化DOTween只能在执行Tween功能前调用一次,否则会使用默认初始化

可随时初始化以更改选项,如果无参调用DOTween.Init()则是使用默认值,默认值在Dotween Utility Panel——Preferences里面更改

DOTween - Initialization (demigiant.com)

使用DOTween

以通用方式使用DOTween

DOTween通用公式

第一个参数getter基于TResult Func<out TResult>()第二个参数Setter基于Action<in T>(T t)具体参见

C# 中的协变和逆变

1
2
3
4
void Start()
{
DOTween.To(() => transform.position, x => transform.position = x, new Vector3(3, 8, 5), 1f);
}

以快捷方式使用DOTween

快捷方式

DOTween - Shotcuts (demigiant.com)

不论使用哪种方式,每当创建一个Tween都会返回一个Tweener或者Sequence,可以将其存储为一个引用,也可以直接返回Tween,而不用区别它们

  • Tweener:控制一个值并为其设置动画的补间。
  • Sequence:一个特殊的补间,不是控制一个值,而是控制其他补间并将它们作为一个组进行动画处理。
  • Tween:表示 TweenerSequence 的通用词。
  • Nested tween:包含在 Sequence 中的补间。

可以有很多方式控制tween,比如下面的tweener.Pause

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
using UnityEngine;
using DG.Tweening;

public class DOTweenTest : MonoBehaviour
{
Tweener tweener;
void Start()
{
//DOTween.To(() => transform.position, x => transform.position = x, new Vector3(3, 8, 5), 1f);
tweener = transform.DOMoveX(10f, 3f);
}
private void OnGUI()
{
if (GUILayout.Button("Pause"))
tweener.Pause();
}
}

DOTween - Controls (demigiant.com)

From Tween

您可以通过将 From 设置链式启用它来使几乎任何补间从给定值播放到其当前值(而不是默认情况下从当前值到给定值):

1
2
3
4
5
6
// 标准 TO tween
transform.DOMoveX(2, 1);
// 标准 TO tween 的From版本
transform.DOMoveX(2, 1).From();
// 标准 TO tween 的From版本, true表示使用相对坐标而不是世界坐标
transform.DOMoveX(2, 1).From(true);

全局设置和特殊设置

您可以设置将应用于所有新创建的补间的全局设置,或者为您创建的每个补间设置特定的设置。

全局设置

全局设置允许您设置默认的 autoPlay 和 autoKill 行为、缓动类型、全局 timeScale 等等。

DOTween - globalSettings (demigiant.com)

全局通用设置

  • static LogBehaviour DOTween.logBehaviour :根据选择的模式,DOTween 将仅记录错误、错误和警告,或所有内容以及附加信息。
  • static bool DOTween.maxSmoothUnscaledTime:如果 useSmoothDeltaTime 为 TRUE,则表示在时间无关补间的情况下将被视为已过去的最大时间。
  • static bool DOTween.useSmoothDeltaTime:如果为 TRUE,DOTween 将对 UpdateType.Normal 和 UpdateType.Late 补间使用 Time.smoothDeltaTime 而不是 Time.deltaTime(除非它们设置为 timeScaleIndependent,在这种情况下,将使用最后一个时间步长和 maxSmoothUnscaledTime 之间的值)。将此设置为 TRUE 将导致更流畅的动画。
  • static bool DOTween.nestedTweenFailureBehaviour:NestedTweenFailureBehaviour.TryToPreserveSequence 在安全模式处于活动状态并且嵌套在序列中的补间失败时的行为。
  • static bool DOTween.onWillLog<LogType,object>:用于拦截 DOTween 的日志。如果此方法不为 NULL,DOTween 将在通过 Unity 自己的 Debug 日志方法写入日志之前调用它。 如果您希望 DOTween 继续处理日志,则返回 TRUE,否则返回 FALSE。 该方法必须返回一个布尔值并接受两个参数:LogType(DOTween 试图记录的 Unity 日志的类型)、object(DOTween 想要记录的消息)。
  • static bool DOTween.showUnityEditorReport:如果设置为 TRUE,您将在退出播放模式时获得 DOTween 报告(仅在编辑器中)。有助于了解您达到了多少最大 Tweeners 和 Sequences 并相应地优化您的最终项目。 请注意,这会稍微降低您在 Unity Editor 中的性能。
  • static float DOTween.timeScale:全局 timeScale 应用于所有补间,包括常规和独立。
  • static bool DOTween.useSafeMode:如果设置为 TRUE 补间将稍慢但更安全,允许 DOTween 自动处理诸如在补间运行时破坏目标等事情。 将其设置为 FALSE 意味着您必须亲自处理在其目标被破坏或以某种方式使其无效之前杀死一个补间。 警告:在 iOS 上,safeMode 仅在剥离级别设置为“Strip Assemblies”或脚本调用优化设置为“Slow and Safe”时才有效,而在 Windows 10 WSA 上,如果选择了 Master Configuration 和 .NET,它将不起作用。
  • static DOTween.SetTweensCapacity(int maxTweeners, int maxSequences):为了更快,DOTween 限制了您可以拥有的活动补间的最大数量。如果您超出该限制,请不要担心:它会自动增加。尽管如此,如果您已经知道您需要比默认的最大 Tweeners/Sequences(即 200 个 Tweeners 和 50 个序列)更多(或更少),您可以在启动时设置 DOTween 的容量,并避免在自动提升时出现打嗝。例如:DOTween.SetTweensCapacity(2000, 100);

应用于所有新创建的补间的设置

应用于所有新创建的补间的设置

特殊设置

特定设置通过链式编程分配,它们都以“Set”开头(回调除外,以“On”开头),因此 IntelliSense 将帮助您找到它们。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
// Create a transform tween and set its ease, loops and OnComplete callback
transform.DOMove(new Vector3(2,2,2), 2).SetEase(Ease.OutQuint).SetLoops(4).OnComplete(myFunction);

// Same as above but using line breaks to make it more readable
transform.DOMove(new Vector3(2,2,2), 2)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);

// Same as above but storing the tween and applying settings without chaining
Tween myTween = transform.DOMove(new Vector3(2,2,2), 2);
myTween.SetEase(Ease.OutQuint);
myTween.SetLoops(4);
myTween.OnComplete(myFunction);

此外,某些补间类型具有特殊的附加选项,具体取决于补间值的类型,可以通过 SetOptions() 设置。

DOTween - tweenerOptions (demigiant.com)

请记住,SetOptions() 是特殊的,需要在主补间创建方法之后立即链接

1
2
3
4
5
6
7
// Same as the previous examples, but force the transform to
// snap on integer values (very useful for pixel perfect stuff)
transform.DOMove(new Vector3(2,2,2), 2)
.SetOptions(true)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);

您甚至可以使用 SetAs() 将所有设置从一个补间复制到另一个补间:

1
2
3
4
5
6
7
8
// Create a tween with some settings and store it as a Tween
Tween myTween = transform.DOMove(new Vector3(2,2,2), 2)
.SetEase(Ease.OutQuint)
.SetLoops(4)
.OnComplete(myFunction);

// Create another tween and apply the same settings as the previous one
material.DOColor(Color.red, 2).SetAs(myTween);

(两个补间将循环 4 次 OutQuint 缓出,完成后他们将调用相同的 OnComplete 方法,即使他们实际上补间完全不同的对象和类型)

补间生命周期

当您创建补间时,它将自动播放(除非您更改全局 defaultAutoPlay 行为),直到它完成所有循环。

补间完成后会自动终止(除非您更改全局 defaultAutoKill 行为),这意味着您将无法再使用它。

如果您想重用相同的补间,只需将其 autoKill 行为设置为 FALSE(通过更改所有补间的全局 autoKill 设置,或将 SetAutoKill(false) 链接到您的补间)。

如果在补间播放时补间的目标变为 NULL,则可能会发生错误。您必须小心或激活安全模式

缓存和最大补间数

如果您激活回收 DOTween 会缓存您创建的所有补间,因此它可以重用它们而不是创建新的补间。

此外,为了避免使用过多的资源,它会将自己设置为同时运行最多 200 个 Tweeners 和 50 个 Sequences。如果你需要更多,DOTween 会自动增加它的大小,但你也可以直接设置它以避免发生自动调整大小时打嗝:

1
2
// Set max Tweeners to 3000 and max Sequences to 200
DOTween.SetTweensCapacity(3000, 200);

如果在任何时候您想清除 DOTween 的缓存并完全重置它,您可以调用 DOTween.Clear(),这将杀死所有补间并清除所有缓存。

回收补间

您不需要手动回收补间。相反,您可以选择自动回收全部或仅回收特定的。

  • static bool DOTween.defaultRecyclable
    Default: false
    Default recycling behaviour applied to all newly created tweens.
  • SetRecyclable(bool recyclable)
    Sets the recycling behaviour for the tween. If you don’t set it then the default value (set either via DOTween.Init or DOTween.defaultRecyclable) will be used.
    recyclable If TRUE the tween will be recycled after being killed, otherwise it will be destroyed.

不过,您可以随时更改 Tweener 的结束和开始值。

DOTween - additionalMethods (demigiant.com)