/// <summary> /// Combine all sprites into a single texture and save it to disk. /// </summary> static public bool UpdateTexture(UIAtlas atlas, List <SpriteEntry> sprites) { // Get the texture for the atlas Texture2D tex = atlas.texture as Texture2D; Texture2D alphaTex = null; string oldPath = (tex != null) ? AssetDatabase.GetAssetPath(tex.GetInstanceID()) : ""; string newPath = NGUIEditorTools.GetSaveableTexturePath(atlas); string alphaPath = newPath.Replace(".png", "`alpha.png"); // Clear the read-only flag in texture file attributes if (System.IO.File.Exists(newPath)) { System.IO.FileAttributes newPathAttrs = System.IO.File.GetAttributes(newPath); newPathAttrs &= ~System.IO.FileAttributes.ReadOnly; System.IO.File.SetAttributes(newPath, newPathAttrs); } bool newTexture = (tex == null || oldPath != newPath); if (newTexture) { // Create a new texture for the atlas tex = new Texture2D(1, 1, TextureFormat.ARGB32, false); } else { // Make the atlas readable so we can save it if (tex.format.ToString().EndsWith("32")) { tex = NGUIEditorTools.ImportTexture(oldPath, true, false, false); } else { tex = new Texture2D(tex.width, tex.height, TextureFormat.ARGB32, false); } } bool alphaSplit = NGUISettings.atlasAlphaSplit && !atlas.premultipliedAlpha; if (!alphaSplit) { alphaTex = NGUIEditorTools.ImportTexture(alphaPath, true, true, false); if (alphaTex != null) { tex = AlphaMerge(tex, alphaTex); byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(newPath, bytes); bytes = null; AssetDatabase.SaveAssets(); AssetDatabase.DeleteAsset(alphaPath); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); tex = NGUIEditorTools.ImportTexture(newPath, true, true, true); } } // Pack the sprites into this texture if (PackTextures(tex, sprites)) { atlas.InitSize(tex); if (alphaSplit) { alphaTex = AlphaSplit(ref tex); byte[] alphaBytes = alphaTex.EncodeToPNG(); System.IO.File.WriteAllBytes(alphaPath, alphaBytes); } byte[] bytes = tex.EncodeToPNG(); System.IO.File.WriteAllBytes(newPath, bytes); // Load the texture we just saved as a Texture2D AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); tex = NGUIEditorTools.ImportTexture(newPath, false, true, !atlas.premultipliedAlpha && !alphaSplit); if (alphaSplit) { alphaTex = NGUIEditorTools.ImportTexture(alphaPath, false, true, false); } // Update the atlas texture if (newTexture) { if (tex == null) { Debug.LogError("Failed to load the created atlas saved as " + newPath); } else { atlas.spriteMaterial.mainTexture = tex; if (alphaTex != null) { atlas.spriteMaterial.SetTexture("_AlphaTex", alphaTex); } } ReleaseSprites(sprites); AssetDatabase.SaveAssets(); AssetDatabase.Refresh(ImportAssetOptions.ForceSynchronousImport); } return(true); } else { if (!newTexture) { NGUIEditorTools.ImportTexture(oldPath, false, true, !atlas.premultipliedAlpha); } //Debug.LogError("Operation canceled: The selected sprites can't fit into the atlas.\n" + // "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead."); EditorUtility.DisplayDialog("Operation Canceled", "The selected sprites can't fit into the atlas.\n" + "Keep large sprites outside the atlas (use UITexture), and/or use multiple atlases instead", "OK"); return(false); } }