unity动画片段分割

using UnityEngine;
using UnityEditor;
using System.Collections.Generic;

public class AnimationSplitter : MonoBehaviour
{
    public AnimationClip originalClip; // 要分割的原始动画剪辑
    public List<int> keyFrames; // 关键帧列表


    public void SplitAnimation()
    {
        if (originalClip == null || keyFrames == null || keyFrames.Count == 0)
        {
            Debug.LogError("原始动画剪辑为空或关键帧列表无效");
            return;
        }

        float frameRate = originalClip.frameRate;
        keyFrames.Sort(); // 确保关键帧列表按升序排列

        List<int> segments = new List<int> { 0 }; // 添加起始帧
        segments.AddRange(keyFrames);
        segments.Add(Mathf.FloorToInt(originalClip.length * frameRate)); // 添加结束帧

        for (int i = 0; i < segments.Count - 1; i++)
        {
            int startFrame = segments[i];
            int endFrame = segments[i + 1];

            AnimationClip newClip = new AnimationClip();
            newClip.frameRate = frameRate;

            foreach (var binding in AnimationUtility.GetCurveBindings(originalClip))
            {
                AnimationCurve curve = AnimationUtility.GetEditorCurve(originalClip, binding);
                AnimationCurve newCurve = new AnimationCurve();

                bool startKeyAdded = false;
                bool endKeyAdded = false;

                foreach (Keyframe key in curve.keys)
                {
                    float keyTime = key.time * frameRate;
                    if (keyTime >= startFrame && keyTime <= endFrame)
                    {
                        newCurve.AddKey(new Keyframe(key.time - (startFrame / frameRate), key.value, key.inTangent, key.outTangent));
                    }

                    if (keyTime == startFrame)
                    {
                        startKeyAdded = true;
                    }

                    if (keyTime == endFrame)
                    {
                        endKeyAdded = true;
                    }
                }

                // 在开始帧和结束帧插入关键帧
                if (!startKeyAdded)
                {
                    float startTime = startFrame / frameRate;
                    float startValue = curve.Evaluate(startTime);
                    newCurve.AddKey(new Keyframe(0, startValue, 0, 0));
                }

                if (!endKeyAdded)
                {
                    float endTime = endFrame / frameRate;
                    float endValue = curve.Evaluate(endTime);
                    newCurve.AddKey(new Keyframe((endFrame - startFrame) / frameRate, endValue, 0, 0));
                }

                newClip.SetCurve(binding.path, binding.type, binding.propertyName, newCurve);
            }

            AssetDatabase.CreateAsset(newClip, $"Assets/{originalClip.name}_Part{i + 1}.anim");
            Debug.Log($"Created clip: {originalClip.name}_Part{i + 1}");
        }

        AssetDatabase.SaveAssets();
    
    }
}

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一分之三

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值