SetAnimationClips() private method

private SetAnimationClips ( Animation animation, AnimationClip clips ) : void
animation Animation
clips AnimationClip
return void
 private AnimationClip AddClipToAnimationComponent(AnimationClip newClip)
 {
     if (this.animatedObject.GetComponent <Animation>() == null)
     {
         Animation animation = this.animatedObject.AddComponent(typeof(Animation)) as Animation;
         animation.clip = newClip;
     }
     AnimationClip[] animationClips = AnimationUtility.GetAnimationClips(this.animatedObject);
     Array.Resize <AnimationClip>(ref animationClips, animationClips.Length + 1);
     animationClips[animationClips.Length - 1] = newClip;
     AnimationUtility.SetAnimationClips(this.animatedObject.GetComponent <Animation>(), animationClips);
     return(newClip);
 }
示例#2
0
        private void fbxCreatePrefab(FBXInfo fbxInfo)
        {
            DirectoryInfo info     = new DirectoryInfo(fbxInfo.rawFolder);
            List <string> fbxsList = new List <string>();

            if (Directory.Exists(fbxInfo.fbxFolder) == false)
            {
                Directory.CreateDirectory(fbxInfo.fbxFolder);
            }
            if (Directory.Exists(fbxInfo.prefabFolder) == false)
            {
                Directory.CreateDirectory(fbxInfo.prefabFolder);
            }
            bool hasModel = false;

            foreach (FileInfo fileInfo in info.GetFiles())
            {
                if (fileInfo.Extension.ToUpper() == ".FBX")
                {
                    string fbxName = fileInfo.Name;
                    string fbxPath;
                    if (fbxName == fbxInfo.fileName + fileInfo.Extension)
                    {
                        hasModel = true;
                        fbxPath  = fbxInfo.fbxFolder + fbxName;
                        fileInfo.CopyTo(fbxPath, true);
                        continue;
                    }

                    if (fbxInfo.type == FBXType.MODEL)
                    {
                        continue;
                    }

                    if (fileInfo.Name.IndexOf(fbxInfo.fileName + "@") == -1)
                    {
                        fbxName = fbxInfo.fileName + "@" + fbxName;
                    }
                    fbxPath = fbxInfo.fbxFolder + fbxName;
                    fileInfo.CopyTo(fbxPath, true);
                    fbxsList.Add(fbxName);
                }
                else
                {
                    fileInfo.CopyTo(fbxInfo.fbxFolder + fileInfo.Name, true);
                }
            }

            string prefabFbxName = fbxInfo.fileName + ".fbx";

            if (hasModel == false)
            {
                if (fbxsList.Count == 0)
                {
                    ShowNotification(new GUIContent("不存在文件"));
                    return;
                }

                string prefabCopyFBX = fbxInfo.fbxFolder + fbxsList[0];
                string prefabFBX     = fbxInfo.fbxFolder + prefabFbxName;
                File.Copy(prefabCopyFBX, prefabFBX, true);
                fbxsList.Add(prefabFbxName);
            }
            else
            {
                fbxsList.Add(prefabFbxName);
            }

            loopAnimationClipNames = EditorConfigUtils.LoopAnimationClipNames;

            AssetDatabase.Refresh();
            List <AnimationClip> animationClipList = new List <AnimationClip>();

            foreach (string fbxName in fbxsList)
            {
                string        fbxPath       = fbxInfo.fbxFolder + fbxName;
                ModelImporter assetImporter = AssetImporter.GetAtPath(fbxPath) as ModelImporter;
                if (assetImporter == null)
                {
                    continue;
                }

                bool isHuman = fbxInfo.animationType == ModelImporterAnimationType.Human;
                assetImporter.animationType = fbxInfo.animationType;
                if (prefabFbxName == fbxName)
                {
                    assetImporter.importAnimation = false;
                }
                else
                {
                    assetImporter.importAnimation = true;

                    ModelImporterClipAnimation[] clipAnimations = assetImporter.defaultClipAnimations;
                    foreach (ModelImporterClipAnimation modelImporterClipAnimation in clipAnimations)
                    {
                        if (loopAnimationClipNames.Contains(modelImporterClipAnimation.name))
                        {
                            modelImporterClipAnimation.loop     = true;
                            modelImporterClipAnimation.wrapMode = WrapMode.Loop;
                        }
                    }
                }
                assetImporter.SaveAndReimport();
                if (assetImporter.importAnimation)
                {
                    AnimationClip clip = AssetDatabase.LoadAssetAtPath <AnimationClip>(fbxPath);
                    animationClipList.Add(clip);
                }
            }

            GameObject fbxRawModel = AssetDatabase.LoadAssetAtPath <GameObject>(fbxInfo.fbxFolder + prefabFbxName);

            if (fbxRawModel == null)
            {
                ShowNotification(new GUIContent("模型文件不存在"));
                return;
            }

            string prefabPath = fbxInfo.prefabPath;

            GameObject fbxPrefab = AssetDatabase.LoadAssetAtPath <GameObject>(prefabPath);

            if (fbxPrefab == null)
            {
                fbxPrefab = PrefabUtility.SaveAsPrefabAsset(fbxRawModel, prefabPath);
            }
            else
            {
                ///已经存在了的
                PrefabCopy2Utility prefabCopyUtility = new PrefabCopy2Utility();
                //PrefabCopyUtility prefabCopyUtility = new PrefabCopyUtility();
                fbxPrefab = prefabCopyUtility.replace(fbxPrefab, fbxRawModel);
            }

            if (fbxInfo.animationType != ModelImporterAnimationType.Legacy)
            {
                Animation animation = fbxPrefab.GetComponent <Animation>();
                if (animation != null)
                {
                    GameObject.DestroyImmediate(animation, true);
                }
                Animator animator = fbxPrefab.GetComponent <Animator>();
                if (animationClipList.Count > 0)
                {
                    if (animator == null)
                    {
                        animator = fbxPrefab.AddComponent <Animator>();
                    }
                    string             controllerFullPath = string.Format("{0}default.controller", fbxInfo.fbxFolder);
                    AnimatorController animatorController = AssetDatabase.LoadAssetAtPath <AnimatorController>(controllerFullPath);
                    if (animatorController == null)
                    {
                        animatorController = AnimatorController.CreateAnimatorControllerAtPath(controllerFullPath);
                    }
                    if (animatorController.layers.Length == 0)
                    {
                        animatorController.AddLayer("Base Layer");
                    }
                    AnimatorControllerLayer animatorControllerLayer = animatorController.layers[0];
                    AnimatorState           defaultState            = animatorControllerLayer.stateMachine.defaultState;

                    foreach (AnimationClip clip in animationClipList)
                    {
                        //需要重新加载不然会崩溃
                        AnimatorState state = AnimatorControllerCreater.addNoExistState(animatorControllerLayer, clip);
                        if (state != null && defaultState == null)
                        {
                            if (state.name.IndexOf("idle") == 0)
                            {
                                defaultState = state;
                                animatorControllerLayer.stateMachine.defaultState = defaultState;
                            }
                        }
                    }

                    if (animator.runtimeAnimatorController == null)
                    {
                        animator.runtimeAnimatorController = animatorController;
                    }

                    AnimatorClipRef animatiorClipRef = fbxPrefab.GetComponent <AnimatorClipRef>();
                    if (animatiorClipRef == null)
                    {
                        animatiorClipRef = fbxPrefab.AddComponent <AnimatorClipRef>();
                    }
                    animatiorClipRef.animationClips = animationClipList.ToArray();
                }
                else if (animator != null)
                {
                    AnimatorClipRef animatiorClipRef = fbxPrefab.GetComponent <AnimatorClipRef>();
                    if (animatiorClipRef == null)
                    {
                        GameObject.DestroyImmediate(animator, true);
                    }
                }
            }
            else
            {
                Animation animation = fbxPrefab.GetComponent <Animation>();
                if (animationClipList.Count > 0)
                {
                    if (animation == null)
                    {
                        animation = fbxPrefab.AddComponent <Animation>();
                    }
                    AnimationUtility.SetAnimationClips(animation, animationClipList.ToArray());
                }
                else
                {
                    if (animation != null)
                    {
                        GameObject.DestroyImmediate(animation, true);
                    }
                }
            }

            UnitCFG cfg = fbxPrefab.GetComponent <UnitCFG>();

            if (cfg == null)
            {
                cfg = fbxPrefab.AddComponent <UnitCFG>();
            }
            EditorUtility.SetDirty(fbxPrefab);
            viewPrefab(fbxPrefab);
            AssetDatabase.Refresh();
        }