/// <summary> /// Builds and adds a new <see cref="dfAtlas.ItemInfo"/> to <paramref name="atlas"/> with the texture of <paramref name="tex"/> and the name of <paramref name="name"/>. /// </summary> /// <param name="atlas">The <see cref="dfAtlas"/> to add the new <see cref="dfAtlas.ItemInfo"/> to.</param> /// <param name="tex">The texture of the new <see cref="dfAtlas.ItemInfo"/>.</param> /// <param name="name">The name of the new <see cref="dfAtlas.ItemInfo"/>. If <see langword="null"/>, it will default to <paramref name="tex"/>'s name.</param> /// <returns>The built <see cref="dfAtlas.ItemInfo"/>.</returns> public static dfAtlas.ItemInfo AddNewItemToAtlas(this dfAtlas atlas, Texture2D tex, string name = null) { if (string.IsNullOrEmpty(name)) { name = tex.name; } if (atlas[name] != null) { return(atlas[name]); } dfAtlas.ItemInfo item = new dfAtlas.ItemInfo { border = new RectOffset(), deleted = false, name = name, region = atlas.FindFirstValidEmptySpace(new IntVector2(tex.width, tex.height)), rotated = false, sizeInPixels = new Vector2(tex.width, tex.height), texture = tex, textureGUID = name }; int startPointX = Mathf.RoundToInt(item.region.x * atlas.Texture.width); int startPointY = Mathf.RoundToInt(item.region.y * atlas.Texture.height); for (int x = startPointX; x < Mathf.RoundToInt(item.region.xMax * atlas.Texture.width); x++) { for (int y = startPointY; y < Mathf.RoundToInt(item.region.yMax * atlas.Texture.height); y++) { atlas.Texture.SetPixel(x, y, tex.GetPixel(x - startPointX, y - startPointY)); } } atlas.Texture.Apply(); atlas.AddItem(item); return(item); }
public bool AddTexture(dfAtlas atlas, params Texture2D[] newTextures) { try { selectedTextures.Clear(); var addedItems = new List <dfAtlas.ItemInfo>(); for (int i = 0; i < newTextures.Length; i++) { // Grab reference to existing item, if it exists, to preserve border information var existingItem = atlas[newTextures[i].name]; // Remove the existing item if it already exists atlas.Remove(newTextures[i].name); // Keep the texture size available var size = new Vector2(newTextures[i].width, newTextures[i].height); // Determine the guid for the texture var spritePath = AssetDatabase.GetAssetPath(newTextures[i]); var guid = AssetDatabase.AssetPathToGUID(spritePath); // Add the new texture to the Items collection var newItem = new dfAtlas.ItemInfo() { textureGUID = guid, name = newTextures[i].name, border = (existingItem != null) ? existingItem.border : new RectOffset(), sizeInPixels = size }; addedItems.Add(newItem); atlas.AddItem(newItem); } if (!rebuildAtlas(atlas)) { atlas.Items.RemoveAll(i => addedItems.Contains(i)); return(false); } return(true); } catch (Exception err) { Debug.LogError(err.ToString(), atlas); EditorUtility.DisplayDialog("Error Adding Sprite", err.Message, "OK"); } return(false); }
public static T SetupDfSpriteFromTexture <T>(this GameObject obj, Texture2D texture, Shader shader) where T : dfSprite { T sprite = obj.GetOrAddComponent <T>(); dfAtlas atlas = obj.GetOrAddComponent <dfAtlas>(); atlas.Material = new Material(shader); atlas.Material.mainTexture = texture; atlas.Items.Clear(); dfAtlas.ItemInfo info = new dfAtlas.ItemInfo { border = new RectOffset(), deleted = false, name = "main_sprite", region = new Rect(Vector2.zero, new Vector2(1, 1)), rotated = false, sizeInPixels = new Vector2(texture.width, texture.height), texture = null, textureGUID = "main_sprite" }; atlas.AddItem(info); sprite.Atlas = atlas; sprite.SpriteName = "main_sprite"; return(sprite); }
public bool AddTexture( dfAtlas atlas, params Texture2D[] newTextures ) { try { selectedTextures.Clear(); var addedItems = new List<dfAtlas.ItemInfo>(); for( int i = 0; i < newTextures.Length; i++ ) { // Grab reference to existing item, if it exists, to preserve border information var existingItem = atlas[ newTextures[ i ].name ]; // Remove the existing item if it already exists atlas.Remove( newTextures[ i ].name ); // Keep the texture size available var size = new Vector2( newTextures[ i ].width, newTextures[ i ].height ); // Determine the guid for the texture var spritePath = AssetDatabase.GetAssetPath( newTextures[ i ] ); var guid = AssetDatabase.AssetPathToGUID( spritePath ); // Add the new texture to the Items collection var newItem = new dfAtlas.ItemInfo() { textureGUID = guid, name = newTextures[ i ].name, border = ( existingItem != null ) ? existingItem.border : new RectOffset(), sizeInPixels = size }; addedItems.Add( newItem ); atlas.AddItem( newItem ); } if( !rebuildAtlas( atlas ) ) { atlas.Items.RemoveAll( i => addedItems.Contains( i ) ); return false; } return true; } catch( Exception err ) { Debug.LogError( err.ToString(), atlas ); EditorUtility.DisplayDialog( "Error Adding Sprite", err.Message, "OK" ); } return false; }