「Live2D Unity 文档翻译」从脚本播放动作

本文翻译自:https://docs.live2d.com/cubism-sdk-tutorials/motion-unity-ow/

译者注:注意!这并不是一篇严谨的翻译,本人并不是翻译行业从业者,也根本不会日文。官网的中文翻译会连带代码一起翻译,而且还不如机翻日文,官网的英语翻译版本有的语法很奇怪,看起来也是机翻。本文主要来自日文机翻,然后再结合实际开发经验调整到通顺,修改不该翻译的东西并润色。

[最后更新日期: 2020/01/30] 译者注:这是这个日文原文的更新日期

这篇文章将阐述为 Cubism 模型配置 MotionController 的步骤。

[ 导入 SDK-放置模型 ] 假设模型已经放置到项目中。

摘要

如果要在不使用 Unity Mecanim 的情况下从脚本播放动画,则可以使用 「Cubism SDK for Unity」 中名为 CubismMotionController 的组件来实现。

单击此处以获取有关 CubismMotionContoller 的更多信息。

Animator 组件是 CubismMotionController 正常运行所必需的。

但是,CubismMotionController 无法与使用 AnimatorAnimatorController 共存。

因此,在使用 CubismMotionController 播放动作时,请勿在 Animator 中设置 AnimatorController 。

要为 Cubism 模型进行以上设置,请按照以下步骤操作。

  1. 附加 CubismMotionController
  2. 准备一个调用命令来播放 AnimationClip 的组件
  3. 创建一个按钮来播放动作

附加 CubismMotionController

CubismMotionController 附加到模型所在的 GameObject 上。

img

img

CubismMotionController 有一个设置项。

  • Layer Count:由 CubismMotionController 管理的动画层的总数。这个值应该大于等于 1。

附加 CubismMotionController 时,如果未将 AnimatorCubismFadeController 附加到 GameObject,则将同时附加它。

CubismMotionController 需要 Animator,因为它使用Playable来播放动作。

如果在 Animator 组件的 Controller 中设置了AnimatorController,则会优先处理。

如果您使用 CubismMotionController,请不要在此处进行任何设置。

另外,由于 CubismMotionController 使用 MotionFade 将运动与运动混合,因此需要 CubismFadeController

如果未设置 CubismFadeController.CubismFadeMotionList,则使用(译者注:Unity Importer)生成的 .cadeMotionList。

关于设置 .fadeMotionList 请参阅这里

准备一个调用命令来播放 AnimationClip 的组件

创建一个名为 Motion Player 的 C# 脚本,并按如下所示重写代码。

在这里,将根据参数播放 AnimationClip

  1. using Live2D.Cubism.Framework.Motion;
  2. using UnityEngine;
  3. public class MotionPlayer : MonoBehaviour
  4. {
  5. CubismMotionController _motionController;
  6. private void Start()
  7. {
  8. _motionController = GetComponent<CubismMotionController>();
  9. }
  10. public void PlayMotion(AnimationClip animation)
  11. {
  12. if ((_motionController == null) || (animation == null))
  13. {
  14. return;
  15. }
  16. _motionController.PlayAnimation(animation, isLoop: false);
  17. }
  18. }

将创建的 MotionPlayer 附加到「在第一步中附加了 CubismMotionControllerGameObject」上。

img

这样就完成了从脚本播放动作的设置。

您可以通过从外部脚本调用 MotionPlayer.PlayMotion() 并传递要播放的 AnimationClip 作为参数来播放动画。

创建一个按钮来播放动作

右击 “Hierarchy/层次” 窗口或点击 “Hierarchy/层次” 窗口顶部的 “create/创建” 按钮,然后单击 “UI”>”按钮” 以在场景中创建一个按钮。

将创建的 Button 放置在任何地方。

img

选中创建的按钮后,从 “Inspector/检查器” 窗口中单击 Button 「On Click()」底部的 “+”。

将 “Inspector/检查器” 中模型的 GameObject 拖放到添加的列表中,然后从右侧的下拉菜单中选择 MotionPlayer/PlayMotion()

img

img

img

最后,在参数中设置要播放的 AnimationClip

img

这样就完成了设置。

如果现在运行这个场景,则在 “game/游戏” 视图中单击该按钮时将播放该动作。

img