The RGB and alpha channel separation for images in the "improved" Unity project

Source: Internet
Author: User

http://blog.csdn.net/u010153703/article/details/39477887

"There are two obvious questions in this article:
1. Processing an alpha map is a pixel-by-pixel processing with the Texture.setpixel () function. Recommended batch processing, using the Texture.setpixels () function. Recommended batch processing, using the Texture.setpixels () function. ”

The improvement:

Using Unityengine;  Using System.Collections;  Using System.Collections.Generic;  Using Unityeditor;  Using System.IO;    Using System.Reflection;    public class materialtextureforetc1{private static string defaultwhitetexpath_relative = "Assets/default_alpha.png";      private static texture2d Defaultwhitetex = null;        [MenuItem ("Effortforetc1/depart RGB and Alpha Channel")] static void Seperatealltexturesrgbandalphachannel () {        Debug.Log ("Start departing."); if (!        Getdefaultwhitetexture ()) {return;        } string[] paths = Directory.GetFiles (Application.datapath, "* *", searchoption.alldirectories); foreach (string path in Paths) {if (!string. IsNullOrEmpty (path) && istexturefile (path) &&!            istextureconverted (path))//full name {seperatergbaandlphachannel (path);    }} assetdatabase.refresh (); Refresh to ensure new generated RBA andAlpha textures shown in Unity as well as the meta file Debug.Log ("Finish departing."); } #region Process texture static void Seperatergbaandlphachannel (String _texpath) {string Assetrelativepa        th = Getrelativeassetpath (_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        } 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 texture2d Rgbtex = new Texture2d (Sourcetex.width, Sourcetex.height, Te        Xtureformat.rgb24, Bgeneratemipmap); Rgbtex.setpixels (Sourcetex.             Getpixels ());  Texture2d Mipmaptex = new Texture2d (Sourcetex.width, Sourcetex.height, Textureformat.rgba32, true); Alpha Channel needed here mipmaptex.setpixels (Sourcetex.        Getpixels ());        Mipmaptex.apply ();   color[] Colors2rdlevel = mipmaptex.getpixels (1);        Second level of Mipmap color[] Colorsalpha = new Color[colors2rdlevel.length]; if (colors2rdlevel.length! = (mipmaptex.width+1)/2 * (mipmaptex.height+1)/2) {Debug.logerror ("Size            Error. ");        Return        } bool Balphaexist = FALSE;            for (int i = 0; i < colors2rdlevel.length; ++i) {COLORSALPHA[I].R = colors2rdlevel[i].a;            COLORSALPHA[I].G = COLORS2RDLEVEL[I].A; COLORSALPHA[I].B = Colors2RDLEVEL[I].A; if (!            Mathf.approximately (COLORS2RDLEVEL[I].A, 1.0f)) {balphaexist = true;        }} texture2d Alphatex = null; if (balphaexist) {Alphatex = new texture2d ((sourcetex.width+1)/2, (sourcetex.height+1)/2, TEXTUREFO Rmat.        RGB24, Bgeneratemipmap); } else {Alphatex = new texture2d (Defaultwhitetex.width, Defaultwhitetex.height, TEXTUREFORMAT.RG        B24, false);        } alphatex.setpixels (Colorsalpha);        Rgbtex.apply ();        Alphatex.apply ();        byte[] bytes = Rgbtex.encodetopng ();        File.writeallbytes (Assetrelativepath, bytes);        byte[] alphabytes = Alphatex.encodetopng ();        String alphatexrelativepath = Getalphatexpath (_texpath);        File.writeallbytes (Alphatexrelativepath, alphabytes);        Reimportasset (Assetrelativepath, Rgbtex.width, rgbtex.height); Reimportasset (Alphatexrelativepath, Alphatex.width, alphatex.height);        Debug.Log ("Succeed departing:" + Assetrelativepath); } static void Reimportasset (string path, int width, int height) {try {Assetdatabase.impor        Tasset (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);    } 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 bool Getdefaultwhitetexture () {Defaultwhitetex = Resources.loadassetatpath (Defaultwhitetexpath_rela  tive, typeof (Texture2d)) as texture2d; Not just the textures unDer Resources file if (!defaultwhitetex) {debug.logerror ("Load Texture Failed:" + defaultwhi            tetexpath_relative);        return false;    } return true; } #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 bool Istextureconverted (string _path) {return _path. Contains ("_rgb.") | | _path.    Contains ("_alpha.");      } static string Getrgbtexpath (String _texpath) {return Gettexpath (_texpath, "_rgb.");      } static string Getalphatexpath (String _texpath) {return Gettexpath (_texpath, "_alpha."); } static string Gettexpath (String _texpath, String _texrole) {string dir = system.io.pAth.        GetDirectoryName (_texpath);        string filename = System.IO.Path.GetFileNameWithoutExtension (_texpath);        string result = Dir + "/" + filename + _texrole + "png";      return result;          } 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 ("\ \", "/");   } #endregion}




The RGB and alpha channel separation for images in the "improved" 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.