public void SetIsReadable(bool isReadable) { if (_textureImporter.isReadable == isReadable) { return; } _textureImporter.isReadable = isReadable; _textureImporter.ImportNow(); }
public static void SetIsReadable(this Texture2D texture, bool readable) { TextureImporter textureImporter = texture.GetTextureImporter(); if (textureImporter != null) { textureImporter.isReadable = readable; textureImporter.ImportNow(); } }
public static void PackTextures( Texture2D[] inputTextures, string atlasPath, int atlasSize, int atlasPadding, out Texture2D atlasTexture, out TextureImporter atlasTextureImporter) { List <PackedTextureInfo> inputTexturesList = inputTextures.Select(texture2D => new PackedTextureInfo(texture2D)).ToList(); // Force each input texture to be readable foreach (PackedTextureInfo textureInfo in inputTexturesList) { textureInfo.SetIsReadable(true); } // Try to pack textures try { // Allocate new texture for atlas and pack atlasTexture = new Texture2D(atlasSize, atlasSize, TextureFormat.ARGB32, false, true); Rect[] packedRects = atlasTexture.PackTextures(inputTextures, atlasPadding, atlasSize, false); // Preserve original dimensions for later use int atlasTextureWidth = atlasTexture.width; int atlasTextureHeight = atlasTexture.height; if (atlasTexture.format != TextureFormat.ARGB32) { // Retrieve data from atlas and destroy it. // We have to do this because we want ARGB32 texture format // so it'd be possible to save it as PNG, not DXT // or whatever Texture2D.PackTextures may pack them into Color32[] atlasPixels = atlasTexture.GetPixels32(); DestroyImmediate(atlasTexture); // Allocate new atlas texture and copy data as ARGB32 atlasTexture = new Texture2D(atlasTextureWidth, atlasTextureHeight, TextureFormat.ARGB32, false, true); atlasTexture.SetPixels32(atlasPixels); } // Encode atlas to PNG and write byte[] atlasPngData = atlasTexture.EncodeToPNG(); File.WriteAllBytes(atlasPath, atlasPngData); // Free the in-memory texture and re-import it as an asset DestroyImmediate(atlasTexture); AssetDatabase.ImportAsset(atlasPath, ImportAssetOptions.ForceSynchronousImport); atlasTexture = AssetDatabase.LoadAssetAtPath(atlasPath, typeof(Texture2D)) as Texture2D; if (atlasTexture == null) { throw new FileLoadException(string.Format("Could not load atlas texture '{0}'", atlasPath)); } // Get TextureImporter and setup it for sprites atlasTextureImporter = atlasTexture.GetTextureImporter(); atlasTextureImporter.spriteImportMode = SpriteImportMode.Multiple; atlasTextureImporter.textureType = TextureImporterType.Default; atlasTextureImporter.maxTextureSize = atlasSize; atlasTextureImporter.mipmapEnabled = true; atlasTextureImporter.alphaIsTransparency = true; TextureImporterSettings atlasTextureImporterSettings = new TextureImporterSettings(); atlasTextureImporter.ReadTextureSettings(atlasTextureImporterSettings); atlasTextureImporterSettings.spriteMeshType = SpriteMeshType.FullRect; atlasTextureImporter.SetTextureSettings(atlasTextureImporterSettings); // Fill sprite metadata for each texture SpriteMetaData[] atlasSpriteMetaData = new SpriteMetaData[packedRects.Length]; for (int i = 0; i < packedRects.Length; i++) { Rect rect = packedRects[i]; rect.x *= atlasTextureWidth; rect.y *= atlasTextureHeight; rect.width *= atlasTextureWidth; rect.height *= atlasTextureHeight; SpriteMetaData spriteMetaData = new SpriteMetaData(); spriteMetaData.name = inputTexturesList[i].Name; spriteMetaData.alignment = 0; spriteMetaData.rect = rect; // Center pivot spriteMetaData.pivot = new Vector2( (spriteMetaData.rect.xMax - spriteMetaData.rect.xMin) / 2f, (spriteMetaData.rect.yMax - spriteMetaData.rect.yMin) / 2f ); atlasSpriteMetaData[i] = spriteMetaData; } // Apply spritesheet atlasTextureImporter.spritesheet = atlasSpriteMetaData; atlasTextureImporter.ImportNow(); } finally { // Restore IsReadable on input textures foreach (PackedTextureInfo textureInfo in inputTexturesList) { textureInfo.SetIsReadable(textureInfo.IsReadable); } } }