Browse Source

fix:更新贴图属性设置

chenbin 1 month ago
parent
commit
216adc27f3

+ 131 - 0
Assets/Scripts/Business/Editor/Tools/FLAssetPostprocessor.Model.cs

@@ -0,0 +1,131 @@
+using UnityEditor;
+using UnityEngine;
+
+namespace FL.Editor
+{
+    /// <summary>
+    /// 模型资源属性预设
+    /// </summary>
+    public partial class FLAssetPostprocessor : AssetPostprocessor
+    {
+        private void OnPreprocessModel()
+        {
+            if (!assetPath.StartsWith(ResRelative))
+                return;
+	        
+            var modelImporter = (ModelImporter)assetImporter;
+            modelImporter.isReadable = assetPath.ToLower().Contains(Readable);
+            
+            //顶点优化选项,开启后顶点将被重新排序,GPU性能可以得到提升
+            modelImporter.optimizeMeshVertices = true;
+            
+            //合并相同位置的顶点
+            modelImporter.weldVertices = true;
+            
+            //优化骨骼节点
+            modelImporter.optimizeGameObjects = true;
+        
+            // 混合形状,人物模型看情况开启,开启的话使用SkinnedMeshRenderer代替MeshRenderer
+            modelImporter.importBlendShapes = false;
+            
+            // 可见性
+            //modelImporter.importVisibility = false;
+            
+            // 导入相机
+            modelImporter.importCameras = false;
+            
+            // 光照
+            modelImporter.importLights = false;
+            
+            //材质球
+            //临时注释
+            //modelImporter.materialImportMode = ModelImporterMaterialImportMode.None;
+            
+            modelImporter.keepQuads = false;
+            modelImporter.meshCompression = ModelImporterMeshCompression.Medium;
+            modelImporter.meshOptimizationFlags = MeshOptimizationFlags.Everything;
+            
+            modelImporter.importAnimation = assetPath.Contains("@");
+            modelImporter.animationCompression = ModelImporterAnimationCompression.Optimal;
+            modelImporter.animationType = ModelImporterAnimationType.Generic;
+            modelImporter.skinWeights = ModelImporterSkinWeights.Custom;
+            modelImporter.maxBonesPerVertex = 2;
+            
+            //网格缓冲区大小,默认为Auto,根据Mesh Size自动选择16或者32位Buff
+            //modelImporter.indexFormat = ModelImporterIndexFormat.Auto;
+            //法线,先全部关闭,后期看美术是否需要使用Mesh 的法线再开启
+            //modelImporter.importNormals = ModelImporterNormals.None;
+            //切线
+            modelImporter.importTangents = ModelImporterTangents.None;
+            //uv交换,使用lightmap时需要开启
+            modelImporter.swapUVChannels = false;
+            modelImporter.generateSecondaryUV = false;
+        }
+
+        private void OnPostprocessModel(GameObject model)
+        {
+            var modelImporter = (ModelImporter)assetImporter;
+            var isAnimation = assetPath.Contains("@");
+
+            //动画后处理部分
+			if (isAnimation)
+			{
+				//var animationClipList = new List<AnimationClip>(AnimationUtility.GetAnimationClips(model));
+				//if (animationClipList.Count == 0) 
+				//{
+				//	var objectList = UnityEngine.Object.FindObjectsOfType (typeof(AnimationClip)) as AnimationClip[];
+				//	animationClipList.AddRange(objectList);
+				//}
+				
+				//foreach (var clip in animationClipList)
+				//{
+				//	try
+				//	{
+    //                    //AnimationUtils.CompressAnimationClip(clip);
+				//		//var curves = AnimationUtility.GetAllCurves(clip);
+
+				//		////浮点数精度压缩为f4
+				//		//foreach (var curveDate in curves)
+				//		//{
+				//		//	if (curveDate.curve?.keys == null)
+				//		//		continue;
+
+				//		//	var keyFrames = curveDate.curve.keys;
+				//		//	for (var i = 0; i < keyFrames.Length; i++)
+				//		//	{
+				//		//		var key = keyFrames[i];
+				//		//		key.value = float.Parse(key.value.ToString("f4"));
+				//		//		key.inTangent = float.Parse(key.inTangent.ToString("f4"));
+				//		//		key.outTangent = float.Parse(key.outTangent.ToString("f4"));
+				//		//		keyFrames[i] = key;
+				//		//	}
+				//		//	curveDate.curve.keys = keyFrames;
+				//		//	clip.SetCurve(curveDate.path, curveDate.type, curveDate.propertyName, curveDate.curve);
+				//		//}
+				//	}
+				//	catch (System.Exception e)
+				//	{
+				//		Debug.LogError($"CompressAnimationClip Failed !!! animationPath : {assetPath} error: {e}");
+				//	}
+				//}
+			}
+			else
+			{
+				var renderers = model.transform.GetComponentsInChildren<Renderer>();
+				foreach (var renderer in renderers)
+				{
+                    if (renderer == null || renderer.sharedMaterial == null)
+						continue;
+                    
+					if (renderer.sharedMaterial.name != model.name)
+					{
+						Debug.LogWarning("材质名和模型名不匹配!" + model.name);
+					}
+				}
+			}
+        }
+        
+    }
+
+}
+

+ 3 - 0
Assets/Scripts/Business/Editor/Tools/FLAssetPostprocessor.Model.cs.meta

@@ -0,0 +1,3 @@
+fileFormatVersion: 2
+guid: 019eb608a51440458510b7b7d1e658b7
+timeCreated: 1619600083

+ 182 - 0
Assets/Scripts/Business/Editor/Tools/FLAssetPostprocessor.Texture.cs

@@ -0,0 +1,182 @@
+using UnityEditor;
+using UnityEngine;
+using XGame.Editor.Asset.Tools;
+
+namespace FL.Editor
+{
+    /// <summary>
+    /// 纹理资源属性预设
+    /// </summary>
+    public partial class FLAssetPostprocessor : AssetPostprocessor
+    {
+        /// <summary>
+        /// Assets相对路径前缀
+        /// </summary>
+        private const string ResRelative = "Assets/Res/";
+        private const string i18nRelative = "Assets/i18n/"; 
+        
+        private const string Readable = "readable";
+        private const string Mipmap = "mipmap";
+        
+        private const string UIAtlas = "/Atlas/";
+        
+        private const string Sprites = "/Sprites/";
+        
+        private const string Textures = "/Textures/";
+
+        private const string FGUITextures = "/Addressable/FGUI/";
+        
+        private const string SpineTextures = "/Spines/";
+        
+        private const int AtlasTextureMaxSize = 512;
+        private const int TextureMaxSize = 2048;
+        private const TextureImporterFormat HaveAlphaSettingFormat = TextureImporterFormat.ASTC_6x6;
+        private const TextureImporterFormat AndroidNoAlphaSettingFormat = TextureImporterFormat.ETC2_RGB4;
+        private const TextureImporterFormat IosNoAlphaSettingFormat = TextureImporterFormat.ASTC_6x6;
+        
+        private void OnPreprocessTexture()
+        {
+            if (!assetPath.StartsWith(ResRelative))
+                return;
+            // CheckTextureAlpha(assetPath);
+
+            var textureImporter = (TextureImporter)assetImporter;
+            textureImporter.isReadable = assetPath.ToLower().Contains(Readable);
+            textureImporter.anisoLevel = 1;
+
+            var haveAlpha = textureImporter.DoesSourceTextureHaveAlpha();
+        
+            #region Atlas
+        
+            if (assetPath.Contains(UIAtlas))
+            {
+                textureImporter.textureType = TextureImporterType.Sprite;
+                textureImporter.spriteImportMode = SpriteImportMode.Single;
+                textureImporter.mipmapEnabled = false;
+                var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
+                defaultSetting.format = TextureImporterFormat.Automatic;
+                defaultSetting.textureCompression = TextureImporterCompression.Uncompressed;
+                defaultSetting.maxTextureSize = AtlasTextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(defaultSetting);
+        
+                return;
+            }
+        
+            #endregion
+        
+            #region Sprite
+        
+            if (assetPath.Contains(Sprites))
+            {
+                textureImporter.textureType = TextureImporterType.Sprite;
+                textureImporter.spriteImportMode = SpriteImportMode.Single;
+                textureImporter.mipmapEnabled = false;
+                textureImporter.npotScale = TextureImporterNPOTScale.None;
+                var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
+                defaultSetting.format = TextureImporterFormat.Automatic;
+                defaultSetting.textureCompression = TextureImporterCompression.Compressed;
+                defaultSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(defaultSetting);
+                //ios
+                var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
+                iosSetting.overridden = true;
+                iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
+                iosSetting.textureCompression = TextureImporterCompression.Compressed;
+                iosSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(iosSetting);
+                //Android
+                var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
+                androidSetting.overridden = true;
+                androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
+                androidSetting.textureCompression = TextureImporterCompression.Compressed;
+                androidSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(androidSetting);
+        
+                return;
+            }
+            #endregion
+
+            #region Texture
+            if (assetPath.Contains(Textures) || assetPath.Contains(FGUITextures))
+            {
+                textureImporter.textureType = TextureImporterType.Default;
+                // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled
+                textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap);
+                var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
+                defaultSetting.format = TextureImporterFormat.Automatic;
+                defaultSetting.textureCompression = TextureImporterCompression.Compressed;
+                defaultSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(defaultSetting);
+                //ios
+                var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
+                iosSetting.overridden = true;
+                iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
+                iosSetting.textureCompression = TextureImporterCompression.Compressed;
+                iosSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(iosSetting);
+                //Android
+                var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
+                androidSetting.overridden = true;
+                androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
+                androidSetting.textureCompression = TextureImporterCompression.Compressed;
+                androidSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(androidSetting);
+        
+                return;
+            }
+
+            #endregion
+
+            #region Spine
+            return;
+            if (assetPath.Contains(SpineTextures))
+            {
+                textureImporter.textureType = TextureImporterType.Default;
+                // Unity导入贴图默认使用的sprite,切换为Default时会开启mipmapEnabled,因此需要在设置textureType后再设置mipmapEnabled
+                textureImporter.mipmapEnabled = assetPath.ToLower().Contains(Mipmap);
+                textureImporter.npotScale = TextureImporterNPOTScale.None;
+
+                var defaultSetting = textureImporter.GetDefaultPlatformTextureSettings();
+                defaultSetting.format = TextureImporterFormat.Automatic;
+                defaultSetting.textureCompression = TextureImporterCompression.CompressedHQ;
+                defaultSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(defaultSetting);
+                //ios
+                var iosSetting = textureImporter.GetPlatformTextureSettings("iOS");
+                iosSetting.overridden = true;
+                iosSetting.format = haveAlpha ? HaveAlphaSettingFormat : IosNoAlphaSettingFormat;
+                iosSetting.textureCompression = TextureImporterCompression.CompressedHQ;
+                iosSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(iosSetting);
+                //Android
+                var androidSetting = textureImporter.GetPlatformTextureSettings("Android");
+                androidSetting.overridden = true;
+                androidSetting.format = haveAlpha ? HaveAlphaSettingFormat : AndroidNoAlphaSettingFormat;
+                androidSetting.compressionQuality = 100; //这个参数是百分比的意思,100的时候才能保持贴图精度
+                androidSetting.textureCompression = TextureImporterCompression.CompressedHQ;
+                androidSetting.maxTextureSize = TextureMaxSize;
+                textureImporter.SetPlatformTextureSettings(androidSetting);
+            }
+            #endregion
+        }
+
+        private void OnPostprocessTexture(Texture2D texture)
+        {
+            //检测texture
+            var task = new TextureVerifyTask();
+            task.VerifyTextureByPath(assetPath);
+            //Packages 文件夹下不判断
+            //if (assetPath.Contains("Packages"))
+            //    return;
+            //if (assetPath.ToLower().Contains("atlas"))
+            //    return;
+            //if (EditorUtility.DisplayDialog(title, message, confirm))
+            //{
+            //    AssetDatabase.DeleteAsset(assetPath);
+            //    AssetDatabase.Refresh();
+            //}
+        }
+    }
+
+}
+

+ 11 - 0
Assets/Scripts/Business/Editor/Tools/FLAssetPostprocessor.Texture.cs.meta

@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 3c93b9d71a5a5d34285a6539e62a8ee5
+MonoImporter:
+  externalObjects: {}
+  serializedVersion: 2
+  defaultReferences: []
+  executionOrder: 0
+  icon: {instanceID: 0}
+  userData: 
+  assetBundleName: 
+  assetBundleVariant: