做骨骼动画动画时,想要让动画之间的过渡变得更好,
想做一个新的AnimationClip
将两个动画先后衔接起来。
这里试了很久没法直接将两个不同动画间的关键帧进行复制,
就写了一个脚本帮忙直接在物体上复制姿态,然后可以更好的操作进行动画。
原理
原理就很简单,深度优先搜索遍历物体的儿子们,将被复制的和复制的两个物体所有儿子的Transform
设置相同即可。
也可以自己改改,用来复制别的东西的参数。
脚本源代码
新建脚本复制进去即可,会自动编译
using System.Linq;
using UnityEngine;
using UnityEditor;
using System.Collections;
using System.Collections.Generic;
public class CopyObjectPos : EditorWindow
{
static GameObject copiedGO;
[MenuItem("GameObject/MyTools/Copy Object Pos #&C")]
static void Copy()
{
copiedGO = GameObject.Instantiate(Selection.activeGameObject);
}
[MenuItem("GameObject/MyTools/Paste Object Pos #&P")]
static void Paste()
{
GameObject targetGO = Selection.activeGameObject;
DFS(copiedGO.GetComponentsInChildren<Transform>(), targetGO.GetComponentsInChildren<Transform>());
DestroyImmediate(copiedGO);
}
static void DFS(Transform[] copiedTf, Transform[] targetTf){
for(int i=1;i<copiedTf.Length;i++){
Debug.Log(""+targetTf[i].name+"="+copiedTf[i].name);
targetTf[i].transform.position = copiedTf[i].transform.position;
targetTf[i].transform.localRotation = copiedTf[i].transform.localRotation;
targetTf[i].transform.localScale = copiedTf[i].transform.localScale;
DFS(copiedTf[i].GetComponentsInChildren<Transform>(), targetTf[i].GetComponentsInChildren<Transform>());
}
}
}
使用方式
这里以我的动画制作需求为例
在场景中把需要复制的人物姿态调出来
选中复制命令
此时会有一个临时物体被复制出来
打开需要制作过渡的动画Clip,点小红圈开始录制
选中粘贴命令
复制完了
随后点击设置关键帧,将当前姿势设置为动画关键帧