SimpleSpritePackerEditor.SPTools.AssetSetFormat C# (CSharp) Method

AssetSetFormat() public static method

Sets the asset texture format.
public static AssetSetFormat ( string path, TextureImporterFormat format ) : bool
path string Path.
format TextureImporterFormat Format.
return bool
        public static bool AssetSetFormat(string path, TextureImporterFormat format)
        {
            if (string.IsNullOrEmpty(path))
                return false;

            TextureImporter ti = AssetImporter.GetAtPath(path) as TextureImporter;

            if (ti == null)
                return false;

            TextureImporterSettings settings = new TextureImporterSettings();
            ti.ReadTextureSettings(settings);

            settings.textureFormat = format;
            ti.SetTextureSettings(settings);
            SPTools.DoAssetReimport(path, ImportAssetOptions.ForceUpdate | ImportAssetOptions.ForceSynchronousImport);

            return true;
        }

Usage Example

        /// <summary>
        /// Corrects the textures format.
        /// </summary>
        /// <param name="spriteInfoList">Sprite info list.</param>
        protected void CorrectTexturesFormat(List <SPSpriteInfo> spriteInfoList)
        {
            if (spriteInfoList == null || spriteInfoList.Count == 0)
            {
                return;
            }

            foreach (SPSpriteInfo spriteInfo in spriteInfoList)
            {
                Texture2D texture = null;

                // No source but present target sprite
                if (spriteInfo.source == null && spriteInfo.targetSprite != null)
                {
                    texture = spriteInfo.targetSprite.texture;
                }
                // Texture source
                else if (spriteInfo.source is Texture2D)
                {
                    texture = (spriteInfo.source as Texture2D);
                }
                // Sprite source
                else if (spriteInfo.source is Sprite)
                {
                    texture = (spriteInfo.source as Sprite).texture;
                }

                if (texture != null)
                {
                    // Make sure it's the correct format
                    if (texture.format != TextureFormat.ARGB32 &&
                        texture.format != TextureFormat.RGBA32 &&
                        texture.format != TextureFormat.BGRA32 &&
                        texture.format != TextureFormat.RGB24 &&
                        texture.format != TextureFormat.Alpha8 &&
                        texture.format != TextureFormat.DXT1 &&
                        texture.format != TextureFormat.DXT5)
                    {
                        // Get the texture asset path
                        string assetPath = SPTools.GetAssetPath(texture);

                        // Set new texture format
                        if (!SPTools.AssetSetFormat(assetPath, TextureImporterFormat.ARGB32))
                        {
                            Debug.LogWarning("Sprite Packer failed to set texture format ARGB32 on asset: " +
                                             assetPath);
                        }
                    }
                }
            }
        }