public void SetPreview(GameObject prefab) { this.m_Clip = null; currentSelectedMaterial = null; currentSelectedIndexMaterial = -1; previewPrefab = prefab; unitCfg = null; animatorClipRef = null; AnimationClip defaultClip = null; if (previewPrefab != null) { animationClips.Clear(); animatorClipRef = previewPrefab.GetComponentInChildren <AnimatorClipRef>(); unitCfg = previewPrefab.GetComponentInChildren <UnitCFG>(); if (animatorClipRef != null) { foreach (AnimationClip clip in animatorClipRef.animationClips) { if (clip.name.ToLower().IndexOf("idle") != 0) { defaultClip = clip; } animationClips.Add(clip); } } else { Animator animator = previewPrefab.GetComponentInChildren <Animator>(); if (animator != null) { RuntimeAnimatorController runtimeAnimatorController = animator.runtimeAnimatorController; if (runtimeAnimatorController != null) { foreach (AnimationClip clip in runtimeAnimatorController.animationClips) { if (clip.name.ToLower().IndexOf("idle") != 0) { defaultClip = clip; } animationClips.Add(clip); } } } } particleSystems = previewPrefab.GetComponentsInChildren <ParticleSystem>(); renderers = previewPrefab.GetComponentsInChildren <Renderer>(); } if (animationClips.Count > 0) { this.m_Clip = animationClips[0]; if (defaultClip) { this.m_Clip = defaultClip; } this.playingClipName = this.m_Clip.name; } this.m_AvatarPreview.InitInstance(null, m_Clip); if (m_Clip) { this.m_AvatarPreview.fps = Mathf.RoundToInt(m_Clip.frameRate); this.m_AvatarPreview.ShowIKOnFeetButton = m_Clip.isHumanMotion; } this.m_AvatarPreview.SetPreview(previewPrefab); this.m_AvatarPreview.ResetPreviewFocus(); if (this.m_AvatarPreview.timeControl.currentTime == float.NegativeInfinity) { this.m_AvatarPreview.timeControl.Update(); } if (this.m_AvatarPreview.PreviewObject) { //previewCameraDrawLineBounds.SetView(this.m_AvatarPreview.PreviewObject, previewPrefab); } }
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(); }