unity 导入资源(图片,音效,模型等)格式设置

unity 导入资源(图片,音效,模型等)格式设置,第1张

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using System.IO;

public class TexturePostprocessor : AssetPostprocessor
{
    static void OnPostprocessAllAssets(string[] importedAssets, string[] deletedAssets, string[] movedAssets, string[] movedFromAssetPaths)
    {
        string chinesFiles = null;
        foreach (string str in importedAssets)
        {
            //Debug.Log("Reimported Asset: " + str);
            if(str.Contains("Assets/path") && EditorUtil.CheckHasChineseStr(str))
            {
                if(chinesFiles == null)
                {
                    chinesFiles = str;
                }
                else
                {
                    chinesFiles += "," + str;
                }
                //AssetDatabase.DeleteAsset(str);
            }
        }

        if (chinesFiles != null)
        {
            string tipStr = string.Format("文件{0}中含有中文名称,请立即修改", chinesFiles);
            EditorUtility.DisplayDialog("Error", tipStr, "ok");
            Debug.Log(tipStr);
        }
        //foreach (string str in deletedAssets)
        //{
        //    Debug.Log("Deleted Asset: " + str);
        //}

        //for (int i = 0; i < movedAssets.Length; i++)
        //{
        //    Debug.Log("Moved Asset: " + movedAssets[i] + " from: " + movedFromAssetPaths[i]);
        }
    }

    void OnPreprocessTexture()
    {
        TextureImporter textureImporter = (TextureImporter)assetImporter;
        TextureImporterFormat format = TextureImporterFormat.ASTC_RGBA_8x8;

        if(assetPath.Contains("Assets/Editor"))
        {
            return;
        }

        //只压缩png格式的图片
        if (assetPath.EndsWith(".png") || assetPath.EndsWith(".tga"))
        {
#if USE_ETC2
            format = TextureImporterFormat.ETC2_RGBA8;
#else
            format = TextureImporterFormat.ASTC_RGBA_8x8;
#endif
            //一些字体要设置成Default,如果这是为Sprite,会导致打包时,材质里面会多包含个texture2D的贴图
            if(assetPath.Contains("Assets/Buyu/RawResources/Arts/UI/font/") || assetPath.Contains("Assets/Buyu/RawResources/exportRes/hotres1/arts/fontart/"))
            {
                textureImporter.textureType = TextureImporterType.Default;
            }
        }
        else if (assetPath.EndsWith(".jpg"))
        {
#if USE_ETC2
            format = TextureImporterFormat.ETC_RGB4;
#else
            format = TextureImporterFormat.ASTC_RGBA_8x8;
#endif
            //一些字体要设置成Default,如果这是为Sprite,会导致打包时,材质里面会多包含个texture2D的贴图
            if (assetPath.Contains("Assets/Buyu/RawResources/Arts/UI/font/") || assetPath.Contains("Assets/Buyu/RawResources/exportRes/hotres1/arts/fontart/"))
            {
                textureImporter.textureType = TextureImporterType.Default;
            }
        }


        TextureImporterPlatformSettings setIos = new TextureImporterPlatformSettings()
        {
            name = "iPhone",
            overridden = true,
            format = format,
        };

        TextureImporterPlatformSettings setAndroid = new TextureImporterPlatformSettings()
        {
            name = "Android",
            overridden = true,
            format = format,
        };

        
        int textureSize = 0;
        TextureImporterFormat textureFormat;
        if (textureImporter.GetPlatformTextureSettings("Android", out textureSize, out textureFormat))
        {
            if (format == TextureImporterFormat.ETC2_RGBA8 || format == TextureImporterFormat.ETC2_RGBA8 || !(textureFormat == TextureImporterFormat.ASTC_RGBA_6x6 || textureFormat == TextureImporterFormat.ASTC_RGBA_4x4 || textureFormat == TextureImporterFormat.ASTC_RGB_4x4))
            {
                textureImporter.SetPlatformTextureSettings(setAndroid);
            }
        }
        else
        {
            textureImporter.SetPlatformTextureSettings(setAndroid);
        }

        if (textureImporter.GetPlatformTextureSettings("iPhone", out textureSize, out textureFormat))
        {
            if (format == TextureImporterFormat.ETC2_RGBA8 || format == TextureImporterFormat.ETC2_RGBA8 || !(textureFormat == TextureImporterFormat.ASTC_RGBA_6x6 || textureFormat == TextureImporterFormat.ASTC_RGBA_4x4 || textureFormat == TextureImporterFormat.ASTC_RGB_4x4))
            {
                textureImporter.SetPlatformTextureSettings(setIos);
            }
        }
        else
        {
            textureImporter.SetPlatformTextureSettings(setIos);
        }

        
        textureImporter.mipmapEnabled = false;
    }

    public void OnPreprocessAudio()
	{
		AudioImporter audio = this.assetImporter as AudioImporter;

        string name = Path.GetFileNameWithoutExtension(audio.assetPath);
        bool isBgAudio = name.StartsWith("bgm") || name.EndsWith("bg");
        AudioClipLoadType audioLoadType = isBgAudio ? AudioClipLoadType.Streaming : AudioClipLoadType.CompressedInMemory;
        audio.loadInBackground = false;
        audio.preloadAudioData = false;

        audio.defaultSampleSettings =  new AudioImporterSampleSettings()
        {
            loadType = AudioClipLoadType.CompressedInMemory,
            sampleRateSetting = AudioSampleRateSetting.OverrideSampleRate,
            sampleRateOverride = 22050,
            //sampleRateOverride = 11025,
            compressionFormat = AudioCompressionFormat.Vorbis,
            quality = 0.5f,
        };

        //非背景强制单声道
        //if(!audio.assetPath.Contains("bgm"))
        //{
        //    audio.forceToMono = true;
        //}
    }

    public void OnPreprocessShader()
    {
        ShaderImporter si = this.assetImporter as ShaderImporter;
        si.SetDefaultTextures(null, null);
    }
    private void OnPostprocessModel(GameObject model)
    {
        if (null == model) return;
        ModelImporter modleImport = this.assetImporter as ModelImporter;

        string path = modleImport.assetPath;
        if (!path.Contains("模型的路径"))
        {
            return;
        }

        //导致ab 加载时候出错。。。
        //Renderer[] renders = model.GetComponentsInChildren();
        //if (null == renders) return;
        //foreach (Renderer render in renders)
        //{
        //    render.sharedMaterials = new Material[render.sharedMaterials.Length];
        //}

        if (modleImport != null)
        {
            //if (modelUseToParticle.ContainsKey(model.name))
            //{
            //    modleImport.isReadable = true;
            //}
            //else
            //{
            //    modleImport.isReadable = false;
            //}
        }
    }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/739682.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-04-28
下一篇 2022-04-28

发表评论

登录后才能评论

评论列表(0条)

保存