// ------------------------------------------------------------------
/// \param _atlasInfo the atlas info
/// \param _noImport if true, ex2D will not import the texture to fit for atlas
/// build the atlas info to atlas
// ------------------------------------------------------------------
public static void Build(exAtlasInfo _atlasInfo, bool _noImport = false)
{
exAtlas atlas = _atlasInfo.atlas;
Texture2D texture = _atlasInfo.texture;
Material material = _atlasInfo.material;
// check if the atlas info is valid for build
if (atlas == null)
{
Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the atlas is missing!");
return;
}
if (texture == null)
{
Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the texture is missing!");
return;
}
if (material == null)
{
Debug.LogError("Failed to build atlas info " + _atlasInfo.name + ", the material is missing!");
return;
}
//
if (_atlasInfo.needLayout)
{
_atlasInfo.LayoutElements();
_atlasInfo.needLayout = false;
}
// create temp texture
Color32 buildColor = new Color(0.0f, 0.0f, 0.0f, 0.0f);
if (_atlasInfo.useBuildColor)
{
buildColor = new Color(_atlasInfo.buildColor.r,
_atlasInfo.buildColor.g,
_atlasInfo.buildColor.b,
0.0f);
}
string path = AssetDatabase.GetAssetPath(texture);
TextureImporter importer = TextureImporter.GetAtPath(path) as TextureImporter;
// TextureImporterSettings textureImporterSettings = new TextureImporterSettings();
// importer.ReadTextureSettings(textureImporterSettings);
// textureImporterSettings.readable = true;
// importer.SetTextureSettings(textureImporterSettings);
importer.wrapMode = TextureWrapMode.Clamp;
importer.isReadable = true;
AssetDatabase.ImportAsset(path);
Color32[] colors = new Color32[_atlasInfo.width * _atlasInfo.height];
for (int i = 0; i < _atlasInfo.width * _atlasInfo.height; ++i)
{
colors[i] = buildColor;
}
texture.SetPixels32(colors);
try {
EditorUtility.DisplayProgressBar("Building Atlas " + _atlasInfo.name, "Building Atlas...", 0.1f);
// build atlas texture
_atlasInfo.elements.Sort(exAtlasInfo.CompareByName);
FillAtlasTexture(texture, _atlasInfo, _noImport);
EditorUtility.DisplayProgressBar("Building Atlas " + _atlasInfo.name,
"Import Atlas",
0.9f);
// write to disk
byte[] pngData = texture.EncodeToPNG();
if (pngData != null)
{
File.WriteAllBytes(path, pngData);
}
// now we finish atlas texture filling, we should turn off Read/Write settings, that will save memory a lot!
TextureImporter importSettings = TextureImporter.GetAtPath(path) as TextureImporter;
importSettings.wrapMode = TextureWrapMode.Clamp;
importSettings.isReadable = _atlasInfo.readable;
AssetDatabase.ImportAsset(path);
//
atlas.elements = new exAtlas.Element[_atlasInfo.elements.Count];
for (int i = 0; i < _atlasInfo.elements.Count; ++i)
{
exAtlasInfo.Element el = _atlasInfo.elements[i];
exAtlas.Element el2 = new exAtlas.Element();
int coord_x = el.coord[0];
int coord_y = el.atlasInfo.height - el.coord[1] - (int)el.Height();
float xStart = (float)coord_x / (float)el.atlasInfo.width;
float yStart = (float)coord_y / (float)el.atlasInfo.height;
float xEnd = (float)(coord_x + el.Width()) / (float)el.atlasInfo.width;
float yEnd = (float)(coord_y + el.Height()) / (float)el.atlasInfo.height;
el2.name = el.texture.name;
el2.coords = new Rect(xStart, yStart, xEnd - xStart, yEnd - yStart);
el2.rotated = el.rotated;
el2.originalWidth = el.texture.width;
el2.originalHeight = el.texture.height;
el2.trimRect = el.trimRect;
atlas.elements[i] = el2;
// update the index in exAtlasDB
if (el.isFontElement == false)
{
exAtlasDB.UpdateElementInfo(el, i);
}
}
atlas.texture = texture;
atlas.material = material;
EditorUtility.SetDirty(atlas);
EditorUtility.ClearProgressBar();
}
catch (System.Exception) {
EditorUtility.ClearProgressBar();
throw;
}
// save the needRebuild setting
_atlasInfo.needRebuild = false;
EditorUtility.SetDirty(_atlasInfo);
}