Remove alpha channel from all images in Unity project

Source: Internet
Author: User

To test the pressure on the performance of the texture Alpha in the Untiy project, you need to temporarily remove the alpha channel from all the images in the Unity project and do a test comparison. Here is a basic technique, when the image does not have an alpha channel, it does not need to be processed, how to determine whether the image has an alpha channel, unity does not exist a direct interface. But you can do this: 1. Ti.textureformat = Textureimporterformat.automatictruecolor;
Assetdatabase.importasset (_relativeassetpath); 2. static bool Isnoalphatexture (Texture2d texture)
{
return Texture.format = = Textureformat.rgb24;
}
Using Unityengine;  Using System.Collections;  Using System.Collections.Generic;  Using Unityeditor;  Using System.IO;  Using System.reflection;public class removealphachanel{[MenuItem ("Texturetest/remove Texture Alpha Chanel")] static            void Modifytextures () {Debug.Log ("Start removing Alpha Chanel.");        string[] paths = Directory.GetFiles (Application.datapath, "* *", searchoption.alldirectories); foreach (string path in Paths) {if (!string. IsNullOrEmpty (path) && istexturefile (path))//full name {Removetexturealphachanel (P            ATH);    }} assetdatabase.refresh (); Refresh to ensure new generated RBA and Alpha textures shown in Unity as well as the meta file Debug.Log ("Finish    removing Alpha Chanel. "); [MenuItem ("texturetest/limittexturesizeto128")] static void Limittextures () {Debug.Log ("Start Limit Textures        ."); string[] paths = directory.geTfiles (Application.datapath, "* *", searchoption.alldirectories); foreach (string path in Paths) {if (!string.                    IsNullOrEmpty (path) && istexturefile (path))//full name {try {                    String assetrelativepath = Getrelativeassetpath (path);                    Reimportasset (Assetrelativepath);                Debug.Log ("Limit Texture:" + Assetrelativepath); } catch {Debug.logerror ("Reimport Texture failed:" + getrelativeassetpa                Th (path));    }}} Assetdatabase.refresh (); Refresh to ensure new generated RBA and Alpha textures shown in Unity as well as the meta file Debug.Log ("Finish    Limit textures. "); #region process texture static void Removetexturealphachanel (String _texpath) {string assetrelativepath = Ge        Trelativeassetpath (_texpath); SetTextUrereadableex (Assetrelativepath); Set readable flag and set TextureFormat TrueColor texture2d Sourcetex = Resources.loadassetatpath (Assetrelativepa  Th, typeof (Texture2d)) as texture2d; Not just the textures under Resources file if (!sourcetex) {debug.logerror ("Load Texture Fai            LED: "+ Assetrelativepath);        Return } if (Isnoalphatexture (Sourcetex)) {Debug.Log ("pass.            No Alpha texture: "+ assetrelativepath);        Return        } #region Get origion Mipmap Setting textureimporter ti = null;        try {ti = (textureimporter) textureimporter.getatpath (Assetrelativepath);            } catch {Debug.logerror ("Load Texture failed:" + assetrelativepath);        Return        } if (ti = = null) {return;    } bool Bgeneratemipmap = ti.mipmapenabled;        Same with the texture import setting      #endregion texture2d Rgbtex = new Texture2d (Sourcetex.width, Sourcetex.height, Textureformat.rgb24, bgenerate        MIPMAP); Rgbtex.setpixels (Sourcetex.              Getpixels ());              Rgbtex.apply ();        byte[] bytes = Rgbtex.encodetopng ();        File.writeallbytes (Assetrelativepath, bytes);            Reimportasset (Assetrelativepath, Sourcetex.width, sourcetex.height);    Debug.Log ("Succeed removing Alpha:" + Assetrelativepath);    } static bool Isnoalphatexture (Texture2d texture) {return Texture.format = = Textureformat.rgb24;    } static void Settexturereadableex (string _relativeassetpath)//set readable flag and set TextureFormat TrueColor        {Textureimporter ti = null;        try {ti = (textureimporter) textureimporter.getatpath (_relativeassetpath);            } catch {Debug.logerror ("Load Texture failed:" + _relativeassetpath);        Return    } if (ti = = null) {        Return        } ti.isreadable = true;      Ti.textureformat = Textureimporterformat.automatictruecolor; This is essential for departing textures for ETC1.        No compression format for following operation.    Assetdatabase.importasset (_relativeassetpath);        } static void Reimportasset (string path) {Textureimporter importer = null;        try {importer = (textureimporter) textureimporter.getatpath (path);            } catch {Debug.logerror ("Load Texture failed:" + path);        Return        } if (importer = = null) {return;        } importer.maxtexturesize = 128;        Importer.anisolevel = 0;  Importer.isreadable = false;        Increase memory cost If readable is true importer.textureformat = textureimporterformat.automaticcompressed;    Assetdatabase.importasset (path);     } static void Reimportasset (string path, int width, int height) {   try {assetdatabase.importasset (path);            } catch {Debug.logerror ("Import Texture failed:" + path);        Return        } textureimporter importer = null;        try {importer = (textureimporter) textureimporter.getatpath (path);            } catch {Debug.logerror ("Load Texture failed:" + path);        Return        } if (importer = = null) {return;        } importer.maxtexturesize = Mathf.max (width, height);        Importer.anisolevel = 0;  Importer.isreadable = false;        Increase memory cost If readable is true importer.textureformat = textureimporterformat.automaticcompressed;        Importer.texturetype = Textureimportertype.image; if (path.        Contains ("/ui/")) {importer.texturetype = Textureimportertype.gui;    } assetdatabase.importasset (path); } #endregion #region string or path heLper static bool Istexturefile (string _path) {string path = _path.          ToLower (); return path. EndsWith (". psd") | | Path. EndsWith (". TGA") | | Path. EndsWith (". png") | | Path. EndsWith (". jpg") | | Path. EndsWith (". bmp") | | Path. EndsWith (". tif") | | Path.      EndsWith (". gif");          } static string Getrelativeassetpath (String _fullpath) {_fullpath = Getrightformatpath (_fullpath);          int idx = _fullpath.indexof ("Assets");          String assetrelativepath = _fullpath.substring (idx);      return assetrelativepath; } static string Getrightformatpath (String _path) {return _path.      Replace ("\ \", "/"); } static string Getfilepostfix (String _filepath)//including '. ' eg '. tga ', '. png ', no '. DDS ' {str          ing postfix = ""; int idx = _filepath.          LastIndexOf ('. '); if (idx > 0 && idx < _filepath. Length) Postfix = _filepath. Substring (IDX, _filepath.         LENGTH-IDX); return postfix;   } #endregion}


Remove alpha channel from all images in Unity project

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.