UGUI导入图集自动设置格式

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Rhett_Yuan/article/details/89227980

unity-ugui由于其自身引擎设计,需要对导入的图片设置格式设置,以便更好的引擎支持,一下为自动设置的代码:

private static void OnImportTexture (string assetPath) 
{
    if(assetPath.Contains(RAW_ATLAS_PATH))
    {
        TextureImporter textureImporter =  AssetImporter.GetAtPath(assetPath) as TextureImporter;
	if(textureImporter != null)
        {
	    string AtlasName =  new System.IO.DirectoryInfo(System.IO.Path.GetDirectoryName(assetPath)).Name;
	    textureImporter.textureType = TextureImporterType.Sprite;
	    textureImporter.spriteImportMode = SpriteImportMode.Single;
            textureImporter.spritePixelsPerUnit = 100;
	    textureImporter.spritePackingTag = AtlasName;

            TextureImporterSettings textureImportSetting = new TextureImporterSettings();
            textureImporter.ReadTextureSettings(textureImportSetting);
            textureImportSetting.spriteMeshType = SpriteMeshType.FullRect;
            textureImportSetting.spriteExtrude = 1;
            textureImportSetting.spriteGenerateFallbackPhysicsShape = false;
            textureImporter.SetTextureSettings(textureImportSetting);

            textureImporter.mipmapEnabled = false;
	    textureImporter.isReadable = false;
            textureImporter.wrapMode = TextureWrapMode.Clamp;
            textureImporter.filterMode = FilterMode.Bilinear;
            textureImporter.alphaIsTransparency = true;
            textureImporter.alphaSource = TextureImporterAlphaSource.FromInput;
            textureImporter.sRGBTexture = true;

            TextureImporterPlatformSettings platformSetting = textureImporter.GetPlatformTextureSettings("Standalone");
            platformSetting.maxTextureSize = 2048;
            platformSetting.textureCompression = TextureImporterCompression.Compressed;
            platformSetting.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            platformSetting.overridden = true;
            platformSetting.textureCompression = TextureImporterCompression.Compressed;
            platformSetting.format = TextureImporterFormat.DXT5;
            textureImporter.SetPlatformTextureSettings(platformSetting);

            platformSetting = textureImporter.GetPlatformTextureSettings("iPhone");
            platformSetting.maxTextureSize = 2048;
            platformSetting.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            platformSetting.overridden = true;
            platformSetting.compressionQuality = 100;
            platformSetting.textureCompression = TextureImporterCompression.Compressed;
            platformSetting.format = TextureImporterFormat.PVRTC_RGBA4;
            textureImporter.SetPlatformTextureSettings(platformSetting);

            platformSetting = textureImporter.GetPlatformTextureSettings("Android");
            platformSetting.maxTextureSize = 2048;
            platformSetting.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            platformSetting.overridden = true;
            platformSetting.textureCompression = TextureImporterCompression.Compressed;
            platformSetting.format = TextureImporterFormat.ETC2_RGBA8;
            textureImporter.SetPlatformTextureSettings(platformSetting);

            platformSetting = textureImporter.GetPlatformTextureSettings("Web");
            platformSetting.maxTextureSize = 2048;
            platformSetting.resizeAlgorithm = TextureResizeAlgorithm.Mitchell;
            platformSetting.overridden = true;
            platformSetting.format = TextureImporterFormat.RGBA32;
            textureImporter.SetPlatformTextureSettings(platformSetting);
        }
    }
}

猜你喜欢

转载自blog.csdn.net/Rhett_Yuan/article/details/89227980
今日推荐