Unity中使用AssetPostprocessor对模型动画处理

在游戏开发的过程中,会遇到模型动作同事频繁的修改模型动画,比如可能某个动作不对了等等。而程序使用的动画clip是从模型中拷贝一份出来的,而在AssetPostprocessor中就可以做一些处理这样模型同事频繁修改动画的话,只需要重新导入一下就可以了。

static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths, bool didDomainReload)
    {
        bool processed_flag = false;
        foreach (string asset_path in importedAssets) {
            Debug.LogError("导入资源   asset_path:"+asset_path);
            string dir = Path.GetDirectoryName(asset_path);
            Object main_asset = AssetDatabase.LoadMainAssetAtPath(asset_path);
            if (main_asset.GetType() == typeof(GameObject) && Path.GetExtension(asset_path).ToLower() == ".fbx") 
            {
                Object[] assets = AssetDatabase.LoadAllAssetsAtPath(asset_path);
                List<Object> animation_clip_list = new List<Object>();
                foreach (Object asset in assets)
                {
                    if (asset.GetType() == typeof(AnimationClip))
                    {
                        if (!asset.name.StartsWith("__preview__"))
                        {
                            animation_clip_list.Add(asset);
                        }
                    }
                }
                foreach (AnimationClip animation_clip in animation_clip_list)
                {
                    Object new_animation_clip = new AnimationClip();
                    EditorUtility.CopySerialized(animation_clip, new_animation_clip);
                    if(GetIsContains(new_animation_clip.name))
                    {
                        var clip = new_animation_clip as AnimationClip;
                        AnimationClipSettings clipSetting = AnimationUtility.GetAnimationClipSettings(clip);
                        clipSetting.loopTime = true;
                        AnimationUtility.SetAnimationClipSettings(clip, clipSetting);
                    }
                    string animation_path = Path.Combine(copyPath, new_animation_clip.name + ".anim");
                    AssetDatabase.CreateAsset(new_animation_clip, animation_path);
                }
                processed_flag = true;
            }
        }
        if (processed_flag) 
        {
            AssetDatabase.Refresh();
        }
    }

相关推荐

  1. Unity使用AssetPostprocessor模型动画处理

    2024-03-26 14:14:01       21 阅读
  2. LOD1-Unity模型LOD技术原理以及使用

    2024-03-26 14:14:01       14 阅读
  3. 如何使用 Promises 处理 C# 和 Unity 的异步事件

    2024-03-26 14:14:01       17 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-03-26 14:14:01       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-03-26 14:14:01       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-03-26 14:14:01       18 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-03-26 14:14:01       20 阅读

热门阅读

  1. Redis 教程系列之Redis 客户端连接(八)

    2024-03-26 14:14:01       15 阅读
  2. Redis 安装

    2024-03-26 14:14:01       19 阅读
  3. 设计模式概念、分类和原则

    2024-03-26 14:14:01       16 阅读
  4. ThreadLocal的主要特点:

    2024-03-26 14:14:01       19 阅读
  5. B+ 树和B树有什么区别,数据库索引为什么用B+树

    2024-03-26 14:14:01       19 阅读
  6. FastAPI+React全栈开发07 MongoDB数据库的结构

    2024-03-26 14:14:01       17 阅读