ModelProcessor.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. using UnityEditor;
  2. using UnityEngine;
  3. using System;
  4. using System.IO;
  5. using System.Collections.Generic;
  6. /// <summary>
  7. /// 自动裁剪出模型,需要把模型放入到/Artise/ClipAnimation目录下
  8. /// </summary>
  9. namespace James.Editor
  10. {
  11. public class ModelProcessor : AssetPostprocessor
  12. {
  13. public void OnPostprocessModel(GameObject go)
  14. {
  15. //Set the file path variable for our models
  16. string lowerCaseAssetPath = assetPath.ToLower();
  17. //Apply this script ONLY if we are in our folder
  18. if (lowerCaseAssetPath.IndexOf("artise/clipanimation") == -1) //type your file location with all your models here in lowercase
  19. return;
  20. string HowToClipCfgPath = lowerCaseAssetPath.Replace(".fbx", ".txt");
  21. if (!File.Exists(Directory.GetCurrentDirectory() + "/" + HowToClipCfgPath)) return;
  22. ModelInterpret mi = ModelInterpretor.read(HowToClipCfgPath);
  23. if (mi == null) return;
  24. ///Model importer lets you modify model import settings from editor scripts.
  25. ModelImporter modelImporter = assetImporter as ModelImporter;
  26. modelImporter.animationType = ModelImporterAnimationType.Legacy;
  27. List<ModelImporterClipAnimation> clips = new List<ModelImporterClipAnimation>();
  28. foreach (CustomizeAnim curConfig in mi.anims)
  29. {
  30. clips.Add(ParseAnimationClip(curConfig));
  31. }
  32. if (clips.Count > 0)
  33. modelImporter.clipAnimations = clips.ToArray();
  34. modelImporter.assetBundleName = mi.abName;
  35. }
  36. ModelImporterClipAnimation ParseAnimationClip(CustomizeAnim anim)
  37. {
  38. string clipName = anim.name;
  39. int startIndex = anim.start;
  40. int endIndex = anim.end;
  41. ModelImporterClipAnimation clip = new ModelImporterClipAnimation();
  42. clip.firstFrame = Convert.ToSingle(startIndex);
  43. clip.lastFrame = Convert.ToSingle(endIndex);
  44. clip.loopTime = true;
  45. clip.name = clipName;
  46. return clip;
  47. }
  48. }
  49. }