Пример #1
0
    private static bool _doSplitSelectedActorAnim(string targetName, System.String modelPath, List <AnimSplit> animInfos)
    {
        var targetPath = AnimPath;

        if (!Directory.Exists(targetPath))
        {
            Directory.CreateDirectory(targetPath);
        }


        animInfos.Sort(delegate(AnimSplit a, AnimSplit b) {
            return(a.Compression.CompareTo(b.Compression));
        });

        Dictionary <int, List <AnimSplit> > animByCompression = new Dictionary <int, List <AnimSplit> >();
        Dictionary <string, AnimSplit>      animByName        = new Dictionary <string, AnimSplit>();

        foreach (var animInfo in animInfos)
        {
            List <AnimSplit> target = null;
            if (!animByCompression.TryGetValue(animInfo.Compression, out target))
            {
                target = new List <AnimSplit>();
                animByCompression.Add(animInfo.Compression, target);
            }
            target.Add(animInfo);
            animByName.Add(animInfo.Name, animInfo);
        }

        var keys = animByCompression.Keys.ToList();

        keys.Sort();

        AssetDatabase.RemoveUnusedAssetBundleNames();

        string        _modelPath    = AnimRoot + modelPath;
        ModelImporter modelImporter = (ModelImporter)AssetImporter.GetAtPath(_modelPath);

        if (modelImporter == null)
        {
            EditorUtility.DisplayDialog("拆分动画", "无法加载FBX: " + _modelPath, "继续工作");
            return(false);
        }
        SerializedObject serializedObject = new SerializedObject(modelImporter);

        foreach (var key in keys)
        {
            var animationCompression = serializedObject.FindProperty("m_AnimationCompression");
            animationCompression.intValue = key;

            serializedObject.ApplyModifiedProperties();
            AssetDatabase.WriteImportSettingsIfDirty(_modelPath);
            AssetDatabase.Refresh();

            var prefab = AssetDatabase.LoadAssetAtPath <GameObject>(_modelPath);
            var anim   = prefab.GetComponent <Animation>();
            if (anim == null)
            {
                EditorUtility.DisplayDialog("拆分动画", "无法发现动画对象", "继续工作");
                return(false);
            }

            foreach (AnimationState state in anim)
            {
                AnimSplit info = null;
                if (!animByName.TryGetValue(state.name, out info))
                {
                    continue;
                }
                if (info.Compression != key)
                {
                    continue;
                }

                animByName.Remove(state.name);

                AnimationClip placeClip = new AnimationClip();
                EditorUtility.CopySerialized(state.clip, placeClip);

                var outPath = targetPath + "/" + modelPath;
                if (!Directory.Exists(outPath))
                {
                    Directory.CreateDirectory(outPath);
                }

                outPath = outPath + "/" + state.name + ".anim";
                AssetDatabase.CreateAsset(placeClip, outPath);
            }
        }

        bool result = true;

        foreach (var p in animByName)
        {
            EditorUtility.DisplayDialog("拆分动画", "动画导出失败: " + p.Key + " from " + p.Value.FBXPath, "继续工作");
            result = false;
        }

        AssetDatabase.Refresh();
        return(result);
    }
Пример #2
0
    private static void SplitSelectedActorAnim()
    {
        var selected = Selection.activeObject;

        if (selected == null)
        {
            EditorUtility.DisplayDialog("错误", "请选中对应动画配置表", "继续工作");
            return;
        }
        var path = AssetDatabase.GetAssetPath(selected);

        //Assets/Config/Animation/mengjiang.csv
        if (!path.StartsWith("Assets/Config/Animation") || !path.EndsWith(".csv"))
        {
            EditorUtility.DisplayDialog("错误", "请选中对应动画配置表", "继续工作");
            return;
        }

        string animationName = Path.GetFileNameWithoutExtension(path);
        var    loader        = new CSVLoader();
        var    text          = readAllText(path);

        if (!loader.Load(text))
        {
            EditorUtility.DisplayDialog("错误", "加载动画表失败: " + path, "继续工作");
        }

        var nameCol    = loader.GetColumn("Name");
        var fbxCol     = loader.GetColumn("FBXPath");
        var compCol    = loader.GetColumn("Compression");
        var defaultCol = loader.GetColumn("IsDefault");

        string        defaultAnimName = null;
        List <string> anims           = new List <string>();

        Dictionary <string, List <AnimSplit> > infos = new Dictionary <string, List <AnimSplit> >();

        for (int row = 0; row < loader.RowCount(); row++)
        {
            var animInfo = new AnimSplit();
            animInfo.Name        = nameCol.GetString(row);
            animInfo.FBXPath     = fbxCol.GetString(row);
            animInfo.Compression = compCol.GetInteger32(row);

            List <AnimSplit> subAnimInfos = null;
            if (!infos.TryGetValue(animInfo.FBXPath, out subAnimInfos))
            {
                subAnimInfos = new List <AnimSplit>();
                infos.Add(animInfo.FBXPath, subAnimInfos);
            }

            if (defaultAnimName == null || defaultCol.GetBoolean(row))
            {
                defaultAnimName = animInfo.Name;
            }

            subAnimInfos.Add(animInfo);
            anims.Add(animInfo.Name);
        }


        bool result = true;

        foreach (var p in infos)
        {
            if (!_doSplitSelectedActorAnim(animationName, p.Key, p.Value))
            {
                result = false;
            }
        }

        if (result && defaultAnimName != null)
        {
            EditorUtility.DisplayDialog("拆分主角动画", "已完成", "继续工作");
        }
        else
        {
            EditorUtility.DisplayDialog("错误", "切分失败", "继续工作");
        }

        return;
    }