FLAssetPostprocessor.Model.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using UnityEditor;
  2. using UnityEngine;
  3. namespace FL.Editor
  4. {
  5. /// <summary>
  6. /// 模型资源属性预设
  7. /// </summary>
  8. public partial class FLAssetPostprocessor : AssetPostprocessor
  9. {
  10. private void OnPreprocessModel()
  11. {
  12. if (!assetPath.StartsWith(ResRelative))
  13. return;
  14. var modelImporter = (ModelImporter)assetImporter;
  15. modelImporter.isReadable = assetPath.ToLower().Contains(Readable);
  16. //顶点优化选项,开启后顶点将被重新排序,GPU性能可以得到提升
  17. modelImporter.optimizeMeshVertices = true;
  18. //合并相同位置的顶点
  19. modelImporter.weldVertices = true;
  20. //优化骨骼节点
  21. modelImporter.optimizeGameObjects = true;
  22. // 混合形状,人物模型看情况开启,开启的话使用SkinnedMeshRenderer代替MeshRenderer
  23. modelImporter.importBlendShapes = false;
  24. // 可见性
  25. //modelImporter.importVisibility = false;
  26. // 导入相机
  27. modelImporter.importCameras = false;
  28. // 光照
  29. modelImporter.importLights = false;
  30. //材质球
  31. //临时注释
  32. //modelImporter.materialImportMode = ModelImporterMaterialImportMode.None;
  33. modelImporter.keepQuads = false;
  34. modelImporter.meshCompression = ModelImporterMeshCompression.Medium;
  35. modelImporter.meshOptimizationFlags = MeshOptimizationFlags.Everything;
  36. modelImporter.importAnimation = assetPath.Contains("@");
  37. modelImporter.animationCompression = ModelImporterAnimationCompression.Optimal;
  38. modelImporter.animationType = ModelImporterAnimationType.Generic;
  39. modelImporter.skinWeights = ModelImporterSkinWeights.Custom;
  40. modelImporter.maxBonesPerVertex = 2;
  41. //网格缓冲区大小,默认为Auto,根据Mesh Size自动选择16或者32位Buff
  42. //modelImporter.indexFormat = ModelImporterIndexFormat.Auto;
  43. //法线,先全部关闭,后期看美术是否需要使用Mesh 的法线再开启
  44. //modelImporter.importNormals = ModelImporterNormals.None;
  45. //切线
  46. modelImporter.importTangents = ModelImporterTangents.None;
  47. //uv交换,使用lightmap时需要开启
  48. modelImporter.swapUVChannels = false;
  49. modelImporter.generateSecondaryUV = false;
  50. }
  51. private void OnPostprocessModel(GameObject model)
  52. {
  53. var modelImporter = (ModelImporter)assetImporter;
  54. var isAnimation = assetPath.Contains("@");
  55. //动画后处理部分
  56. if (isAnimation)
  57. {
  58. //var animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(model));
  59. //if (animationClipList.Count == 0)
  60. //{
  61. // var objectList = UnityEngine.Object.FindObjectsOfType (typeof(AnimationClip)) as AnimationClip[];
  62. // animationClipList.AddRange(objectList);
  63. //}
  64. //foreach (var clip in animationClipList)
  65. //{
  66. // try
  67. // {
  68. // //AnimationUtils.CompressAnimationClip(clip);
  69. // //var curves = AnimationUtility.GetAllCurves(clip);
  70. // ////浮点数精度压缩为f4
  71. // //foreach (var curveDate in curves)
  72. // //{
  73. // // if (curveDate.curve?.keys == null)
  74. // // continue;
  75. // // var keyFrames = curveDate.curve.keys;
  76. // // for (var i = 0; i < keyFrames.Length; i++)
  77. // // {
  78. // // var key = keyFrames[i];
  79. // // key.value = float.Parse(key.value.ToString("f4"));
  80. // // key.inTangent = float.Parse(key.inTangent.ToString("f4"));
  81. // // key.outTangent = float.Parse(key.outTangent.ToString("f4"));
  82. // // keyFrames[i] = key;
  83. // // }
  84. // // curveDate.curve.keys = keyFrames;
  85. // // clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
  86. // //}
  87. // }
  88. // catch (System.Exception e)
  89. // {
  90. // Debug.LogError($"CompressAnimationClip Failed !!! animationPath : {assetPath} error: {e}");
  91. // }
  92. //}
  93. }
  94. else
  95. {
  96. var renderers = model.transform.GetComponentsInChildren<Renderer>();
  97. foreach (var renderer in renderers)
  98. {
  99. if (renderer == null || renderer.sharedMaterial == null)
  100. continue;
  101. if (renderer.sharedMaterial.name != model.name)
  102. {
  103. Debug.LogWarning("材质名和模型名不匹配!" + model.name);
  104. }
  105. }
  106. }
  107. }
  108. }
  109. }