Reference Links:
http://blog.csdn.net/u010153703/article/details/45502895
http://blog.csdn.net/candycat1992/article/details/42127811
Http://www.cnblogs.com/joeshifu/p/5489906.html
This is the protagonist of this article:
0.
Map compression:
The Android platform is compressed using the ETC1 format, but it does not support image compression with alpha channels.
Therefore, RGB and Alpha are generally separated, RGB values are obtained from the RGB graph, a value is obtained from the Alpha graph.
With the release of OpenGL ES 3.0, ETC2 also out, supporting the alpha channel, but in the current market, the supported models are still relatively small, so you can not consider.
The iOS platform uses PVRTC format compression, which supports picture compression with alpha channels.
Map settings:
N-Th square with a width height of 2
Read/write enabled does not tick
Generate MIP Maps not tick
1. First understand Texture2d.getpixels () This method, it will return the color values of all the pixels in the texture, one of the parameters is Miplevel, by adjusting the bottom right of the image below the slide button to see the different miplevel.
Miplevel is 0 o'clock (default), with the highest number of pixels and clearest:
Miplevel is 3 o'clock, a bit of a pixel wind feeling:
So what's the real difference when Miplevel is different, test it:
Using Unityengine;
Using System.Collections;
public class Testtexture:monobehaviour {
//need to set TextureType to advanced first, then set Read/write enabled to True
// If the picture width is not 2 of the N-square, it is recommended to set the NONPOWEROF2 to a non-none option, convenient to test public
texture2d texture2d;
Private color[] colors;
void Start ()
{
//api:public color[] getpixels (int miplevel = 0);
Take a picture of 512x512 as an example, the output is:
//262144,262144,65536,16384
//Conclusion: Miplevel each additional 1, the width and height are divided by 2
colors = Texture2d.getpixels ();
Debug.Log (colors. Length);
colors = texture2d.getpixels (0);
Debug.Log (colors. Length);
colors = texture2d.getpixels (1);
Debug.Log (colors. Length);
colors = Texture2d.getpixels (2);
Debug.Log (colors. Length);
}
}
Miplevel Range: Take a picture of a 512x512 (512, 2 9), then the value is 0 to 9.
Then know Miplevel, say again generate MIP maps, the following image is checked and uncheck the difference. When checked, will generate a variety of different miplevel values of the texture, when the texture is farther away from the camera, will automatically use the previously generated texture, the advantage is to reduce the amount of computation, the disadvantage is to increase the memory consumption; unchecked, the opposite.
2. Back to the chase, use the script batch processing, separate the channel:
Using Unityengine;
Using System.Collections;
Using Unityeditor;
Using System.Collections.Generic;
Using System.IO; <summary>///Select a folder or select multiple images, then right-click///</summary> public class Seperatechannel {private static Lis
T<string> Allfiles;
private static bool Mipmap = FALSE; [MenuItem ("Assets/seperate Channel")] static void seperate () {String path = GetFullPath (selection.objects
[0]);
Allfiles = new list<string> (); If you select a folder, get all the files under the folder//otherwise get the selected file if (!path. Contains ("."))
Getallfiles (path);
else Getallfiles ();
Allfiles.removeall ((s) = {return S.contains ("meta");});
string[] suffix = new string[] {". psd", ". TGA", ". png", ". jpg", ". bmp", ". tif", ". gif"};
string[] Convert = new string[] {"_rgb", "_alpha"};//the processed texture will have these words for (int i = 0; i < Allfiles.count; i++)
{bool Needhandle = false;
Filter out the textures to be processed after two for loops for (int j = 0; j < suffix. Length; J + +) {if (Allfiles[i].
EndsWith (Suffix[j])) {Needhandle = true;
Break }} for (int j = 0; J < Convert. Length; J + +) {if (Allfiles[i].
Contains (Convert[j])) {needhandle = false;
Break
}} if (!needhandle) continue;
Debug.Log (Allfiles[i]);
Allfiles[i] = Getassetpath (Allfiles[i]);
Textureimporter ti = Textureimporter.getatpath (allfiles[i]) as Textureimporter;
Ti.isreadable = true;
Texture2d Sourcetex = assetdatabase.loadassetatpath<texture2d> (Allfiles[i]);
color[] Sourcecolor = Sourcetex.getpixels ();
Texture2d Rgbtex = new Texture2d (Sourcetex.width, Sourcetex.height, Textureformat.rgb24, mipmap); Rgbtex.setpixels (Sourcecolor);
Writetexture (Rgbtex, Allfiles[i], true);
Texture2d Alphatex = new Texture2d (Sourcetex.width, Sourcetex.height, Textureformat.rgb24, mipmap);
Color[] C = new Color[sourcecolor.length];
for (int j = 0; J < C.length; J + +) {C[j] = new Color (sourcecolor[j].a, 0, 0);
} alphatex.setpixels (c);
Writetexture (Alphatex, allfiles[i], false);
} Assetdatabase.refresh ();
Debug.Log ("Separation Complete"); }//Get the full path (for recursive traversal) public static string GetFullPath (Object assetobj) {string path = assetdatabase.g
Etassetpath (Assetobj);
Path = Application.datapath + path; Path = path.
Replace ("Assetsassets", "Assets");
return path; }//Gets the path starting with asset public static string Getassetpath (String fullPath) {FullPath = Fullpath.substring (
Fullpath.indexof ("Assets")); Return FullPath; }//Get all files selected public static void Getallfiles () {for (int i = 0; i < Selection.objects.Length; i++
) {string s = GetFullPath (Selection.objects[i]);
Allfiles.add (s); }}//Recursively get all files under a root directory public static void Getallfiles (String dir) {string[] files = directory.g
Etfiles (dir);
foreach (var item in files) {Allfiles.add (item);
} string[] dirs = directory.getdirectories (dir);
foreach (var item in dirs) {getallfiles (item); }}//write public static void Writetexture (Texture2d tex, String assetpath, bool RGB) {string path
= Application.datapath + Assetpath; Path = path.
Replace ("Assetsassets", "Assets"); string suffix = path. Substring (path.
IndexOf (".")); if (RGB) path = path.
Replace (suffix, "_rgb" + suffix); else Path = path. Replace (suffix, "_alpha" + Suffix);
Debug.Log (path); Tex.
Apply (); byte[] bytes = Tex.
Encodetopng ();
File.writeallbytes (path, bytes);
Assetdatabase.refresh ();
Re-import Path = Getassetpath (path);
Textureimporter ti = textureimporter.getatpath (path) as Textureimporter;
Ti.isreadable = false;
Ti.textureformat = textureimporterformat.automaticcompressed;
Assetdatabase.importasset (path);
}
}