unity 编辑器工具 之 获得fbx文件内的动画片段

using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEditor;
using UnityEngine;
namespace Assets.Editor.Tools
{
    public class GetAnimationClip
    {
        [MenuItem("Tools/3D资源/CopyAnimationClip")]
        public static void AnimationClipsCopy()
        {
            Object[] go = Selection.objects;

            string Path = AssetDatabase.GetAssetPath(go[0]);

            string parentPath = GetAnimationClip.getParentPathForAsset(Path);

            for (int i = 0; i < go.Length; i++)
            {
                string fbxPath = AssetDatabase.GetAssetPath(go[i]);

                AnimCopy(fbxPath, parentPath, go[i].name.Remove(0, 4));
            }
        }


        static void AnimCopy(string fbxPath, string parentPath, string name)
        {
            Object[] objs = AssetDatabase.LoadAllAssetsAtPath(fbxPath);

            string animationPath = "";

            AnimationClipSettings setting;

            AnimationClip srcClip;//源AnimationClip

            AnimationClip newClip;//新AnimationClip

            foreach (Object o in objs)
            {
                if (o.GetType() == typeof(AnimationClip))
                {
                    srcClip = o as AnimationClip;

                    newClip = new AnimationClip();

                    newClip.name = name;//设置新clip的名字

                    if (!Directory.Exists(parentPath + @"/anim/"))

                        Directory.CreateDirectory(parentPath + @"/anim/");

                    animationPath = parentPath + @"/anim/" + newClip.name + ".anim";

                    setting = AnimationUtility.GetAnimationClipSettings(srcClip);//获取AnimationClipSettings

                    AnimationUtility.SetAnimationClipSettings(newClip, setting);//设置新clip的AnimationClipSettings

                    newClip.frameRate = srcClip.frameRate;//设置新clip的帧率

                    EditorCurveBinding[] curveBindings = AnimationUtility.GetCurveBindings(srcClip);//获取clip的curveBinds

                    for (int i = 0; i < curveBindings.Length; i++)
                    {
                        AnimationUtility.SetEditorCurve(newClip, curveBindings[i], AnimationUtility.GetEditorCurve(srcClip, curveBindings[i]));//设置新clip的curve
                    }

                    AssetDatabase.CreateAsset(newClip, animationPath); //AssetDatabase中的路径都是相对Asset的  如果指定路径已存在asset则会被删除,然后创建新的asset

                    AssetDatabase.SaveAssets();//保存修改

                    AssetDatabase.Refresh();

                }
            }
        }
        /// <summary>
        /// 返回传入目录的父目录(相对于asset)
        /// </summary>
        /// <param name="assetPath"></param>
        /// <returns></returns>
        public static string getParentPathForAsset(string assetPath)
        {
            string[] pathName = assetPath.Split('/');
            string parentPath = "";

            if (pathName.Length < 2 || pathName[pathName.Length - 1] == "")
            {
                Debug.Log(assetPath + @"没有父目录!");
                return parentPath;
            }

            for (int i = 0; i < pathName.Length - 1; i++)
            {

                if (i != pathName.Length - 2)
                    parentPath += pathName[i] + @"/";
                else
                    parentPath += pathName[i];
            }

            return parentPath;
        }

    }

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值