#if UNITY_EDITORusing System.Collections;using System.Collections.Generic;using System.IO;using System.Text;using UnityEditor;using UnityEngine;public class AnimImport : AssetPostprocessor{ StringBuilder sb = new StringBuilder(); //fbx动画导入前的处理,对动画集进行分段设置,划分为各个子集 void OnPreprocessAnimation() { var fpath = Path.GetDirectoryName(assetImporter.assetPath) + "/times.txt"; if (!File.Exists(fpath)) { //EditorUtility.DisplayDialog("缺少时间配置文件", assetImporter.assetPath + "的动画分段文件time.txt不存在,动画子集无法划分", "ok"); return; } var sr = new StreamReader(File.OpenRead(fpath), Encoding.ASCII); var longStr = sr.ReadToEnd(); var dats = longStr.Split('\"'); var anims = new List(); for (var i = 0; i < dats.Length; ++i) { if (dats[i] == "" || dats[i] == "\r\n") continue; var anim = new ModelImporterClipAnimation(); var subdat = dats[i].Split(' '); anim.name = subdat[0]; anim.firstFrame = float.Parse(subdat[1].TrimEnd('f')); anim.lastFrame = float.Parse(subdat[2].TrimEnd('f')); anims.Add(anim); } var modelImporter = assetImporter as ModelImporter; modelImporter.clipAnimations = anims.ToArray(); } void OnPreprocessModel() { var modelImporter = assetImporter as ModelImporter; modelImporter.globalScale = 0.6f; modelImporter.importMaterials = true; modelImporter.materialName = ModelImporterMaterialName.BasedOnTextureName; } void OnPostprocessModel(GameObject go) { var modelImporter = assetImporter as ModelImporter; //1,找到Materials文件夹下所有的材质文件,替换它的shader var dir = Path.GetDirectoryName(modelImporter.assetPath) + "/Materials"; var files = Directory.GetFiles(dir, "*.mat"); foreach (var fs in files) { var mat = AssetDatabase.LoadAssetAtPath(fs.Replace('\\', '/'), typeof(Material)) as Material; mat.shader = Shader.Find("Qx/Unity/Mobile/Mobile-Diffuse"); } AssetDatabase.SaveAssets(); //2,创建prefab到assets/res/prefabs/char/下 //经试验,查找文档,发现在此方法(OnPostprocessModel)中创建prefab是不可能的,其它方法更不可能 //方式一,这种方式加载出来的fbx是空的 //var fbx = AssetDatabase.LoadAssetAtPath(assetImporter.assetPath, typeof(GameObject)) as GameObject; //方式二,这种方式虽然能成功创建预置体,但创建出的预置体上mesh引用为空(原因见U3D文档) //var fbx = go; //PrefabUtility.CreatePrefab(Path.GetDirectoryName(modelImporter.assetPath) + "/testfbx.prefab", go); }}#endif