赞
踩
如图,fbx模型自带动画是不能用Unity自带的Animation工具编辑的,因为它是只读的。fbx里的动画作为SubAsset(子资产)存在。要想对这些动画做编辑或优化动画文件大小,就需要把这些动画从模型中提取出来。
步骤非常简单:
1.使用AssetDatabase.LoadAllAssetRepresentationsAtPath获取fbx的所有Sub Assets(子资产):
var subAssets = AssetDatabase.LoadAllAssetRepresentationsAtPath("Assets/fbxFile.fbx");
2.从Sub Assets中找到所有AnimationClip,并把模型里AnimationClip数据拷贝到新建AnimationClip,并把AnimationClip对象保存成文件:
- foreach (var item in subAssets)
- {
- if (item is AnimationClip animClip)
- {
- AnimationClip newClip = new AnimationClip();
- EditorUtility.CopySerialized(animClip, newClip);
- string animFile = string.Format("{0}/{1}.anim", outputDir, animClip.name);
- AssetDatabase.CreateAsset(newClip, animFile);
- }
- }
这样就把fbx里的AnimationClip提取成了独立的资产文件,就可以编辑修改了:
完整代码:
- [MenuItem("Assets/提取动画")]
- private static void ExtractFbxAnimationClips()
- {
- GameObject[] selectedObjects = Selection.gameObjects;
- foreach (GameObject obj in selectedObjects)
- {
- var subAssets = AssetDatabase.LoadAllAssetRepresentationsAtPath(AssetDatabase.GetAssetPath(obj));
- var outputDir = Path.GetDirectoryName(AssetDatabase.GetAssetPath(obj));
- foreach (var item in subAssets)
- {
- if (item is AnimationClip animClip)
- {
- AnimationClip newClip = new AnimationClip();
- EditorUtility.CopySerialized(animClip, newClip);
- string animFile = string.Format("{0}/{1}.anim", outputDir, animClip.name);
- AssetDatabase.CreateAsset(newClip, animFile);
- }
- }
- }
- AssetDatabase.Refresh();
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。