private void calculateTokenRenderSize(dfMarkupToken token) { try { dfFont font = (dfFont)base.Font; int kerning = 0; char chr = '\0'; char item = '\0'; if ((token.TokenType == dfMarkupTokenType.Whitespace ? true : token.TokenType == dfMarkupTokenType.Text)) { int num = 0; while (num < token.Length) { item = token[num]; if (item != '\t') { dfFont.GlyphDefinition glyph = font.GetGlyph(item); if (glyph != null) { if (num > 0) { kerning = kerning + font.GetKerning(chr, item); kerning = kerning + base.CharacterSpacing; } kerning = kerning + glyph.xadvance; } } else { kerning = kerning + base.TabSize; } num++; chr = item; } } else if (token.TokenType == dfMarkupTokenType.StartTag && token.Matches("sprite")) { if (token.AttributeCount < 1) { throw new Exception("Missing sprite name in markup"); } UnityEngine.Texture texture = font.Texture; int lineHeight = font.LineHeight; string value = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo itemInfo = font.atlas[value]; if (itemInfo != null) { float single = itemInfo.region.width * (float)texture.width / (itemInfo.region.height * (float)texture.height); kerning = Mathf.CeilToInt((float)lineHeight * single); } } token.Height = base.Font.LineHeight; token.Width = kerning; } finally { } }
private void calculateTokenRenderSize(dfMarkupToken token) { try { dfFont font = (dfFont)base.Font; int num = 0; char previousChar = '\0'; char id = '\0'; if ((token.TokenType == dfMarkupTokenType.Whitespace) || (token.TokenType == dfMarkupTokenType.Text)) { int num2 = 0; while (num2 < token.Length) { id = token[num2]; if (id == '\t') { num += base.TabSize; } else { dfFont.GlyphDefinition glyph = font.GetGlyph(id); if (glyph != null) { if (num2 > 0) { num += font.GetKerning(previousChar, id); num += base.CharacterSpacing; } num += glyph.xadvance; } } num2++; previousChar = id; } } else if ((token.TokenType == dfMarkupTokenType.StartTag) && token.Matches("sprite")) { if (token.AttributeCount < 1) { throw new Exception("Missing sprite name in markup"); } Texture texture = font.Texture; int lineHeight = font.LineHeight; string str = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo info = font.atlas[str]; if (info != null) { float num4 = (info.region.width * texture.width) / (info.region.height * texture.height); num = Mathf.CeilToInt(lineHeight * num4); } } token.Height = base.Font.LineHeight; token.Width = num; } finally { } }
public static Font GetFontFromdfFont(dfFont font) { Font f = new Font(); f.material = new Material(GUI.skin.font.material); f.material.mainTexture = font.Texture; f.material.mainTexture.wrapMode = TextureWrapMode.Repeat; CharacterInfo[] chars = new CharacterInfo[font.Glyphs.Count]; int maxHeight = 0; Debug.Log(font.Glyphs.Count); for (int i = 0; i < font.Glyphs.Count; i++) { CharacterInfo inf = new CharacterInfo(); dfFont.GlyphDefinition glyphDefinition = font.Glyphs[i]; dfAtlas.ItemInfo itemInfo = font.Atlas[font.Sprite]; inf.glyphWidth = glyphDefinition.width; inf.glyphHeight = glyphDefinition.height; inf.size = (int)size.x; if (inf.glyphHeight > maxHeight) { maxHeight = inf.glyphHeight; } inf.index = glyphDefinition.id; float num9 = itemInfo.region.x + (float)glyphDefinition.x * f.material.mainTexture.texelSize.x; float num10 = itemInfo.region.yMax - (float)glyphDefinition.y * f.material.mainTexture.texelSize.y; float num11 = num9 + (float)glyphDefinition.width * f.material.mainTexture.texelSize.x; float num12 = num10 - (float)glyphDefinition.height * f.material.mainTexture.texelSize.y; inf.uvBottomLeft = (new Vector2(num9, num12)); inf.uvBottomRight = (new Vector2(num11, num12)); inf.uvTopRight = (new Vector2(num11, num10)); inf.uvTopLeft = (new Vector2(num9, num10)); inf.advance = glyphDefinition.xadvance; inf.minY = 0; inf.maxY = inf.glyphHeight; inf.minX = 0; inf.maxX = inf.glyphWidth; chars[i] = inf; } f.characterInfo = chars; return(f); }
private void setDefaultSize(string spriteName) { if (this.Atlas != null) { dfAtlas.ItemInfo info = this.Atlas[spriteName]; if ((base.size == Vector2.zero) && (info != null)) { base.Size = info.sizeInPixels; } } }
private void setDefaultSize(string spriteName) { if (this.Atlas == null) { return; } dfAtlas.ItemInfo item = this.Atlas[spriteName]; if (this.size == Vector2.zero && item != null) { base.Size = item.sizeInPixels; } }
private void renderHover() { if (Application.isPlaying && ((((base.Atlas != null) && base.IsEnabled) && ((this.hoverIndex >= 0) && (this.hoverIndex <= (this.items.Length - 1)))) && !string.IsNullOrEmpty(this.ItemHover))) { dfAtlas.ItemInfo info = base.Atlas[this.ItemHover]; if (info != null) { Vector3 vector = base.pivot.TransformToUpperLeft(base.Size); Vector3 vector2 = new Vector3(vector.x + this.listPadding.left, (vector.y - this.listPadding.top) + this.scrollPosition, 0f); float stepSize = base.PixelsToUnits(); int num2 = this.hoverIndex * this.itemHeight; if (this.animateHover) { float num3 = Mathf.Abs((float)(this.hoverTweenLocation - num2)); float num4 = (this.size.y - this.listPadding.vertical) * 0.5f; if (num3 > num4) { this.hoverTweenLocation = num2 + (Mathf.Sign(this.hoverTweenLocation - num2) * num4); num3 = num4; } float maxDelta = (Time.deltaTime / stepSize) * 2f; this.hoverTweenLocation = Mathf.MoveTowards(this.hoverTweenLocation, (float)num2, maxDelta); } else { this.hoverTweenLocation = num2; } vector2.y -= this.hoverTweenLocation.Quantize(stepSize); Color32 color = base.ApplyOpacity(base.color); dfSprite.RenderOptions options = new dfSprite.RenderOptions { atlas = base.atlas, color = color, fillAmount = 1f, pixelsToUnits = base.PixelsToUnits(), size = new Vector3(this.size.x - this.listPadding.horizontal, (float)this.itemHeight), spriteInfo = info, offset = vector2 }; if ((info.border.horizontal > 0) || (info.border.vertical > 0)) { dfSlicedSprite.renderSprite(base.renderData, options); } else { dfSprite.renderSprite(base.renderData, options); } if (num2 != this.hoverTweenLocation) { this.Invalidate(); } } } }
internal void LoadImage(dfAtlas atlas, string source) { dfAtlas.ItemInfo info = atlas[source]; if (info == null) { throw new InvalidOperationException("Sprite does not exist in atlas: " + source); } this.Atlas = atlas; this.Source = source; base.Size = info.sizeInPixels; base.Baseline = (int)this.Size.y; }
internal void LoadImage(dfAtlas atlas, string source) { dfAtlas.ItemInfo item = atlas[source]; if (item == null) { throw new InvalidOperationException(string.Concat("Sprite does not exist in atlas: ", source)); } this.Atlas = atlas; this.Source = source; this.Size = item.sizeInPixels; this.Baseline = (int)this.Size.y; }
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); }
private void calculateTokenRenderSize(dfMarkupToken token) { try { int num = 0; char item = '\0'; bool flag = (token.TokenType == dfMarkupTokenType.Whitespace ? true : token.TokenType == dfMarkupTokenType.Text); dfDynamicFont font = (dfDynamicFont)base.Font; if (flag) { int num1 = Mathf.CeilToInt((float)font.FontSize * base.TextScale); CharacterInfo[] characterInfoArray = font.RequestCharacters(token.Value, num1, FontStyle.Normal); for (int i = 0; i < token.Length; i++) { item = token[i]; if (item != '\t') { CharacterInfo characterInfo = characterInfoArray[i]; num = num + (item == ' ' ? Mathf.CeilToInt(characterInfo.width) : Mathf.CeilToInt(characterInfo.vert.x + characterInfo.vert.width)); if (i > 0) { num = num + Mathf.CeilToInt((float)base.CharacterSpacing * base.TextScale); } } else { num = num + base.TabSize; } } token.Height = base.Font.LineHeight; token.Width = num; } else if (token.TokenType == dfMarkupTokenType.StartTag && token.Matches("sprite") && this.SpriteAtlas != null && token.AttributeCount == 1) { Texture2D texture = this.SpriteAtlas.Texture; float baseline = (float)font.Baseline * base.TextScale; string value = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo itemInfo = this.SpriteAtlas[value]; if (itemInfo != null) { float single = itemInfo.region.width * (float)texture.width / (itemInfo.region.height * (float)texture.height); num = Mathf.CeilToInt(baseline * single); } token.Height = Mathf.CeilToInt(baseline); token.Width = num; } } finally { } }
private void renderProgressFill() { if (this.Atlas == null) { return; } dfAtlas.ItemInfo item = this.Atlas[this.progressSprite]; if (item == null) { return; } Vector3 vector3 = new Vector3((float)this.padding.left, (float)(-this.padding.top)); Vector2 vector2 = new Vector2(this.size.x - (float)this.padding.horizontal, this.size.y - (float)this.padding.vertical); float single = 1f; float single1 = this.maxValue - this.minValue; float single2 = (this.rawValue - this.minValue) / single1; dfProgressFillMode _dfProgressFillMode = this.fillMode; if (_dfProgressFillMode == dfProgressFillMode.Stretch) { vector2.x *single2 >= (float)item.border.horizontal; } if (_dfProgressFillMode != dfProgressFillMode.Fill) { vector2.x = Mathf.Max((float)item.border.horizontal, vector2.x * single2); } else { single = single2; } Color32 color32 = base.ApplyOpacity((!base.IsEnabled ? base.DisabledColor : this.ProgressColor)); dfSprite.RenderOptions renderOption = new dfSprite.RenderOptions(); dfSprite.RenderOptions upperLeft = renderOption; upperLeft.atlas = this.atlas; upperLeft.color = color32; upperLeft.fillAmount = single; upperLeft.offset = this.pivot.TransformToUpperLeft(base.Size) + vector3; upperLeft.pixelsToUnits = base.PixelsToUnits(); upperLeft.size = vector2; upperLeft.spriteInfo = item; renderOption = upperLeft; if (item.border.horizontal != 0 || item.border.vertical != 0) { dfSlicedSprite.renderSprite(this.renderData, renderOption); } else { dfSprite.renderSprite(this.renderData, renderOption); } }
public override Vector2 CalculateMinimumSize() { dfAtlas.ItemInfo spriteInfo = this.SpriteInfo; if (spriteInfo == null) { return(Vector2.zero); } RectOffset border = spriteInfo.border; if (((border != null) && (border.horizontal > 0)) && (border.vertical > 0)) { return(Vector2.Max(base.CalculateMinimumSize(), new Vector2((float)border.horizontal, (float)border.vertical))); } return(base.CalculateMinimumSize()); }
public override Vector2 CalculateMinimumSize() { dfAtlas.ItemInfo backgroundSprite = this.getBackgroundSprite(); if (backgroundSprite == null) { return(base.CalculateMinimumSize()); } RectOffset rectOffset = backgroundSprite.border; if (rectOffset.horizontal <= 0 && rectOffset.vertical <= 0) { return(base.CalculateMinimumSize()); } return(Vector2.Max(base.CalculateMinimumSize(), new Vector2((float)rectOffset.horizontal, (float)rectOffset.vertical))); }
public override Vector2 CalculateMinimumSize() { dfAtlas.ItemInfo info = this.getBackgroundSprite(); if (info == null) { return(base.CalculateMinimumSize()); } RectOffset border = info.border; if ((border.horizontal <= 0) && (border.vertical <= 0)) { return(base.CalculateMinimumSize()); } return(Vector2.Max(base.CalculateMinimumSize(), new Vector2((float)border.horizontal, (float)border.vertical))); }
public override Vector2 CalculateMinimumSize() { dfAtlas.ItemInfo spriteInfo = this.SpriteInfo; if (spriteInfo == null) { return(Vector2.zero); } RectOffset rectOffset = spriteInfo.border; if (rectOffset == null || rectOffset.horizontal <= 0 || rectOffset.vertical <= 0) { return(base.CalculateMinimumSize()); } return(Vector2.Max(base.CalculateMinimumSize(), new Vector2((float)rectOffset.horizontal, (float)rectOffset.vertical))); }
public void RebuildIndexes() { if (this.map != null) { this.map.Clear(); } else { this.map = new Dictionary <string, dfAtlas.ItemInfo>(); } for (int i = 0; i < this.items.Count; i++) { dfAtlas.ItemInfo item = this.items[i]; this.map[item.name] = item; } }
private static void rebuildVertices(dfRenderData renderData, dfSprite.RenderOptions options) { float single = 0f; float single1 = 0f; float single2 = Mathf.Ceil(options.size.x); float single3 = Mathf.Ceil(-options.size.y); dfAtlas.ItemInfo itemInfo = options.spriteInfo; float single4 = (float)itemInfo.border.left; float single5 = (float)itemInfo.border.top; float single6 = (float)itemInfo.border.right; float single7 = (float)itemInfo.border.bottom; if (options.flip.IsSet(dfSpriteFlip.FlipHorizontal)) { float single8 = single6; single6 = single4; single4 = single8; } if (options.flip.IsSet(dfSpriteFlip.FlipVertical)) { float single9 = single7; single7 = single5; single5 = single9; } dfSlicedSprite.verts[0] = new Vector3(single, single1, 0f) + options.offset; dfSlicedSprite.verts[1] = dfSlicedSprite.verts[0] + new Vector3(single4, 0f, 0f); dfSlicedSprite.verts[2] = dfSlicedSprite.verts[0] + new Vector3(single4, -single5, 0f); dfSlicedSprite.verts[3] = dfSlicedSprite.verts[0] + new Vector3(0f, -single5, 0f); dfSlicedSprite.verts[4] = new Vector3(single2 - single6, single1, 0f) + options.offset; dfSlicedSprite.verts[5] = dfSlicedSprite.verts[4] + new Vector3(single6, 0f, 0f); dfSlicedSprite.verts[6] = dfSlicedSprite.verts[4] + new Vector3(single6, -single5, 0f); dfSlicedSprite.verts[7] = dfSlicedSprite.verts[4] + new Vector3(0f, -single5, 0f); dfSlicedSprite.verts[8] = new Vector3(single, single3 + single7, 0f) + options.offset; dfSlicedSprite.verts[9] = dfSlicedSprite.verts[8] + new Vector3(single4, 0f, 0f); dfSlicedSprite.verts[10] = dfSlicedSprite.verts[8] + new Vector3(single4, -single7, 0f); dfSlicedSprite.verts[11] = dfSlicedSprite.verts[8] + new Vector3(0f, -single7, 0f); dfSlicedSprite.verts[12] = new Vector3(single2 - single6, single3 + single7, 0f) + options.offset; dfSlicedSprite.verts[13] = dfSlicedSprite.verts[12] + new Vector3(single6, 0f, 0f); dfSlicedSprite.verts[14] = dfSlicedSprite.verts[12] + new Vector3(single6, -single7, 0f); dfSlicedSprite.verts[15] = dfSlicedSprite.verts[12] + new Vector3(0f, -single7, 0f); for (int i = 0; i < (int)dfSlicedSprite.verts.Length; i++) { renderData.Vertices.Add((dfSlicedSprite.verts[i] * options.pixelsToUnits).Quantize(options.pixelsToUnits)); } }
private static void rebuildVertices(dfRenderData renderData, dfSprite.RenderOptions options) { float x = 0f; float y = 0f; float num3 = Mathf.Ceil(options.size.x); float num4 = Mathf.Ceil(-options.size.y); dfAtlas.ItemInfo spriteInfo = options.spriteInfo; float left = spriteInfo.border.left; float top = spriteInfo.border.top; float right = spriteInfo.border.right; float bottom = spriteInfo.border.bottom; if (options.flip.IsSet(dfSpriteFlip.FlipHorizontal)) { float num9 = right; right = left; left = num9; } if (options.flip.IsSet(dfSpriteFlip.FlipVertical)) { float num10 = bottom; bottom = top; top = num10; } verts[0] = new Vector3(x, y, 0f) + options.offset; verts[1] = verts[0] + new Vector3(left, 0f, 0f); verts[2] = verts[0] + new Vector3(left, -top, 0f); verts[3] = verts[0] + new Vector3(0f, -top, 0f); verts[4] = new Vector3(num3 - right, y, 0f) + options.offset; verts[5] = verts[4] + new Vector3(right, 0f, 0f); verts[6] = verts[4] + new Vector3(right, -top, 0f); verts[7] = verts[4] + new Vector3(0f, -top, 0f); verts[8] = new Vector3(x, num4 + bottom, 0f) + options.offset; verts[9] = verts[8] + new Vector3(left, 0f, 0f); verts[10] = verts[8] + new Vector3(left, -bottom, 0f); verts[11] = verts[8] + new Vector3(0f, -bottom, 0f); verts[12] = new Vector3(num3 - right, num4 + bottom, 0f) + options.offset; verts[13] = verts[12] + new Vector3(right, 0f, 0f); verts[14] = verts[12] + new Vector3(right, -bottom, 0f); verts[15] = verts[12] + new Vector3(0f, -bottom, 0f); for (int i = 0; i < verts.Length; i++) { renderData.Vertices.Add(((Vector3)(verts[i] * options.pixelsToUnits)).Quantize(options.pixelsToUnits)); } }
private void renderProgressFill() { if (this.Atlas != null) { dfAtlas.ItemInfo info = this.Atlas[this.progressSprite]; if (info != null) { Vector3 vector = new Vector3((float)this.padding.left, (float)-this.padding.top); Vector2 vector2 = new Vector2(this.size.x - this.padding.horizontal, this.size.y - this.padding.vertical); float num = 1f; float num2 = this.maxValue - this.minValue; float num3 = (this.rawValue - this.minValue) / num2; dfProgressFillMode fillMode = this.fillMode; if ((fillMode == dfProgressFillMode.Stretch) && ((vector2.x * num3) < info.border.horizontal)) { } if (fillMode == dfProgressFillMode.Fill) { num = num3; } else { vector2.x = Mathf.Max((float)info.border.horizontal, vector2.x * num3); } Color32 color = base.ApplyOpacity(!base.IsEnabled ? base.DisabledColor : this.ProgressColor); dfSprite.RenderOptions options = new dfSprite.RenderOptions { atlas = this.atlas, color = color, fillAmount = num, offset = base.pivot.TransformToUpperLeft(base.Size) + vector, pixelsToUnits = base.PixelsToUnits(), size = vector2, spriteInfo = info }; if ((info.border.horizontal == 0) && (info.border.vertical == 0)) { dfSprite.renderSprite(base.renderData, options); } else { dfSlicedSprite.renderSprite(base.renderData, options); } } } }
protected internal override dfAtlas.ItemInfo getBackgroundSprite() { if (base.Atlas == null) { return(null); } dfAtlas.ItemInfo item = null; switch (this.state) { case dfButton.ButtonState.Default: { item = this.atlas[this.backgroundSprite]; break; } case dfButton.ButtonState.Focus: { item = this.atlas[this.focusSprite]; break; } case dfButton.ButtonState.Hover: { item = this.atlas[this.hoverSprite]; break; } case dfButton.ButtonState.Pressed: { item = this.atlas[this.pressedSprite]; break; } case dfButton.ButtonState.Disabled: { item = this.atlas[this.disabledSprite]; break; } } if (item == null) { item = this.atlas[this.backgroundSprite]; } return(item); }
private string buildItemTooltip(dfAtlas.ItemInfo sprite) { if (sprite == null) { return(""); } var width = (int)sprite.sizeInPixels.x; var height = (int)sprite.sizeInPixels.y; return(string.Format( "Atlas: {3} Sprite: {0} Size: {1}x{2}", sprite.name, width, height, atlas.name )); }
protected override void OnRebuildRenderData() { if (base.Atlas == null) { return; } dfAtlas.ItemInfo spriteInfo = base.SpriteInfo; if (spriteInfo == null) { return; } this.renderData.Material = base.Atlas.Material; dfList <Vector3> vertices = this.renderData.Vertices; dfList <Vector2> uV = this.renderData.UV; dfList <Color32> colors = this.renderData.Colors; dfList <int> triangles = this.renderData.Triangles; Vector2[] vector2Array = this.buildQuadUV(); Vector2 vector2 = Vector2.Scale(spriteInfo.sizeInPixels, this.tileScale); Vector2 vector21 = new Vector2(this.tileScroll.x % 1f, this.tileScroll.y % 1f); for (float i = -Mathf.Abs(vector21.y * vector2.y); i < this.size.y; i = i + vector2.y) { for (float j = -Mathf.Abs(vector21.x * vector2.x); j < this.size.x; j = j + vector2.x) { int count = vertices.Count; vertices.Add(new Vector3(j, -i)); vertices.Add(new Vector3(j + vector2.x, -i)); vertices.Add(new Vector3(j + vector2.x, -i + -vector2.y)); vertices.Add(new Vector3(j, -i + -vector2.y)); this.addQuadTriangles(triangles, count); this.addQuadUV(uV, vector2Array); this.addQuadColors(colors); } } this.clipQuads(vertices, uV); float units = base.PixelsToUnits(); Vector3 upperLeft = this.pivot.TransformToUpperLeft(this.size); for (int k = 0; k < vertices.Count; k++) { vertices[k] = (vertices[k] + upperLeft) * units; } }
public static Font DFFontToUnityFont(dfFont font, int scale = 1) { Font f = new Font(font.name); f.material = new Material(UnityEngine.GUI.skin.font.material); f.material.mainTexture = font.Texture; f.material.mainTexture.wrapMode = TextureWrapMode.Repeat; CharacterInfo[] chars = new CharacterInfo[font.Glyphs.Count]; for (int i = 0; i < chars.Length; i++) { CharacterInfo inf = new CharacterInfo(); dfFont.GlyphDefinition glyphDefinition = font.Glyphs[i]; dfAtlas.ItemInfo itemInfo = font.Atlas[font.Sprite]; inf.glyphWidth = glyphDefinition.width * scale; inf.glyphHeight = glyphDefinition.height * scale; inf.size = 0; // Must be 0 inf.index = glyphDefinition.id; float left = itemInfo.region.x + glyphDefinition.x * f.material.mainTexture.texelSize.x; float right = left + glyphDefinition.width * f.material.mainTexture.texelSize.x; float top = itemInfo.region.yMax - glyphDefinition.y * f.material.mainTexture.texelSize.y; float bottom = top - glyphDefinition.height * f.material.mainTexture.texelSize.y; inf.uvTopLeft = new Vector2(left, top); inf.uvTopRight = new Vector2(right, top); inf.uvBottomLeft = new Vector2(left, bottom); inf.uvBottomRight = new Vector2(right, bottom); inf.advance = glyphDefinition.xadvance * scale; inf.minY = scale * -glyphDefinition.height / 2; inf.maxY = scale * glyphDefinition.height / 2; chars[i] = inf; } f.characterInfo = chars; return(f); }
private void renderSprite(dfMarkupToken token, Color32 color, Vector3 position, dfRenderData destination) { try { dfList <Vector3> vertices = destination.Vertices; dfList <int> triangles = destination.Triangles; dfList <Color32> colors = destination.Colors; dfList <Vector2> uV = destination.UV; dfFont font = (dfFont)base.Font; string value = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo item = font.Atlas[value]; if (item != null) { float height = (float)token.Height * base.TextScale * base.PixelRatio; float width = (float)token.Width * base.TextScale * base.PixelRatio; float single = position.x; float single1 = position.y; int count = vertices.Count; vertices.Add(new Vector3(single, single1)); vertices.Add(new Vector3(single + width, single1)); vertices.Add(new Vector3(single + width, single1 - height)); vertices.Add(new Vector3(single, single1 - height)); triangles.Add(count); triangles.Add(count + 1); triangles.Add(count + 3); triangles.Add(count + 3); triangles.Add(count + 1); triangles.Add(count + 2); Color32 color32 = (!base.ColorizeSymbols ? this.applyOpacity(base.DefaultColor) : this.applyOpacity(color)); colors.Add(color32); colors.Add(color32); colors.Add(color32); colors.Add(color32); Rect rect = item.region; uV.Add(new Vector2(rect.x, rect.yMax)); uV.Add(new Vector2(rect.xMax, rect.yMax)); uV.Add(new Vector2(rect.xMax, rect.y)); uV.Add(new Vector2(rect.x, rect.y)); } } finally { } }
private void renderSprite(dfMarkupToken token, Color32 color, Vector3 position, dfRenderData destination) { try { dfList <Vector3> vertices = destination.Vertices; dfList <int> triangles = destination.Triangles; dfList <Color32> colors = destination.Colors; dfList <Vector2> uV = destination.UV; dfFont font = (dfFont)base.Font; string str = token.GetAttribute(0).Value.Value; dfAtlas.ItemInfo info = font.Atlas[str]; if (info != null) { float num = (token.Height * base.TextScale) * base.PixelRatio; float num2 = (token.Width * base.TextScale) * base.PixelRatio; float x = position.x; float y = position.y; int count = vertices.Count; vertices.Add(new Vector3(x, y)); vertices.Add(new Vector3(x + num2, y)); vertices.Add(new Vector3(x + num2, y - num)); vertices.Add(new Vector3(x, y - num)); triangles.Add(count); triangles.Add(count + 1); triangles.Add(count + 3); triangles.Add(count + 3); triangles.Add(count + 1); triangles.Add(count + 2); Color32 item = !base.ColorizeSymbols ? this.applyOpacity(base.DefaultColor) : this.applyOpacity(color); colors.Add(item); colors.Add(item); colors.Add(item); colors.Add(item); Rect region = info.region; uV.Add(new Vector2(region.x, region.yMax)); uV.Add(new Vector2(region.xMax, region.yMax)); uV.Add(new Vector2(region.xMax, region.y)); uV.Add(new Vector2(region.x, region.y)); } } finally { } }
public static void HandleFacecards(PlayerController player, CustomCharacterData data) { var atlas = uiAtlas; var atlasTex = atlas.Texture; dfAtlas.ItemInfo info = new dfAtlas.ItemInfo(); info.name = player.name + "_facecard"; info.region = TextureStitcher.AddFaceCardToAtlas(data.faceCard, atlasTex, uiFaceCards.Count, uiFacecardBounds); info.sizeInPixels = faceCardSizeInPixels; atlas.AddItem(info); if (atlas.Replacement) { atlas.Replacement.Material.mainTexture = atlasTex; } uiFaceCards.Add(info); }
public static void HandlePunchoutFaceCards(CustomCharacterData data) { var atlas = uiAtlas; var atlasTex = atlas.Texture; if (data.punchoutFaceCards != null) { Tools.Print("Adding punchout facecards"); int count = Mathf.Min(data.punchoutFaceCards.Count, 3); for (int i = 0; i < count; i++) { dfAtlas.ItemInfo info = new dfAtlas.ItemInfo(); info.name = data.nameInternal + "_punchout_facecard" + (i + 1); info.region = TextureStitcher.AddFaceCardToAtlas(data.punchoutFaceCards[i], atlasTex, uiFaceCards.Count, uiFacecardBounds); info.sizeInPixels = faceCardSizeInPixels; atlas.AddItem(info); uiFaceCards.Add(info); } } }
protected override void OnRebuildRenderData() { if (base.Atlas != null) { dfAtlas.ItemInfo spriteInfo = base.SpriteInfo; if (spriteInfo != null) { base.renderData.Material = base.Atlas.Material; dfList <Vector3> vertices = base.renderData.Vertices; dfList <Vector2> uV = base.renderData.UV; dfList <Color32> colors = base.renderData.Colors; dfList <int> triangles = base.renderData.Triangles; Vector2[] spriteUV = this.buildQuadUV(); Vector2 vector = Vector2.Scale(spriteInfo.sizeInPixels, this.tileScale); Vector2 vector2 = new Vector2(this.tileScroll.x % 1f, this.tileScroll.y % 1f); for (float i = -Mathf.Abs((float)(vector2.y * vector.y)); i < this.size.y; i += vector.y) { for (float k = -Mathf.Abs((float)(vector2.x * vector.x)); k < this.size.x; k += vector.x) { int count = vertices.Count; vertices.Add(new Vector3(k, -i)); vertices.Add(new Vector3(k + vector.x, -i)); vertices.Add(new Vector3(k + vector.x, -i + -vector.y)); vertices.Add(new Vector3(k, -i + -vector.y)); this.addQuadTriangles(triangles, count); this.addQuadUV(uV, spriteUV); this.addQuadColors(colors); } } this.clipQuads(vertices, uV); float num4 = base.PixelsToUnits(); Vector3 vector3 = base.pivot.TransformToUpperLeft(base.size); for (int j = 0; j < vertices.Count; j++) { vertices[j] = (Vector3)((vertices[j] + vector3) * num4); } } } }
private void renderSelection() { if (base.Atlas == null || this.selectedIndex < 0) { return; } dfAtlas.ItemInfo item = base.Atlas[this.ItemHighlight]; if (item == null) { return; } float units = base.PixelsToUnits(); Vector3 upperLeft = this.pivot.TransformToUpperLeft(base.Size); Vector3 vector3 = new Vector3(upperLeft.x + (float)this.listPadding.left, upperLeft.y - (float)this.listPadding.top + this.scrollPosition, 0f) { y = vector3.y - (float)(this.selectedIndex * this.itemHeight) }; Color32 color32 = base.ApplyOpacity(this.color); dfSprite.RenderOptions renderOption = new dfSprite.RenderOptions(); dfSprite.RenderOptions vector31 = renderOption; vector31.atlas = this.atlas; vector31.color = color32; vector31.fillAmount = 1f; vector31.pixelsToUnits = units; vector31.size = new Vector3(this.size.x - (float)this.listPadding.horizontal, (float)this.itemHeight); vector31.spriteInfo = item; vector31.offset = vector3; renderOption = vector31; if (item.border.horizontal > 0 || item.border.vertical > 0) { dfSlicedSprite.renderSprite(this.renderData, renderOption); } else { dfSprite.renderSprite(this.renderData, renderOption); } }
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; }
public static void CreateAtlasFromSelection() { try { EditorUtility.DisplayProgressBar( "Creating Texture Atlas", "Adding selected textures to the Texture Atlas", 0 ); var selection = Selection .GetFiltered( typeof( Texture2D ), SelectionMode.Assets ) .Cast<Texture2D>() .Where( t => isReadable( t ) ) .OrderByDescending( t => t.width * t.height ) .ToArray(); if( selection.Length == 0 ) { EditorUtility.DisplayDialog( "Create Texture Atlas", "Either no textures selected or none of the selected textures has Read/Write enabled", "OK" ); return; } var saveFolder = Path.GetDirectoryName( AssetDatabase.GetAssetPath( selection[ 0 ] ) ); var prefabPath = EditorUtility.SaveFilePanel( "Create Texture Atlas", saveFolder, "Texture Atlas", "prefab" ); if( string.IsNullOrEmpty( prefabPath ) ) return; prefabPath = prefabPath.MakeRelativePath(); var padding = EditorPrefs.GetInt( "DaikonForge.AtlasDefaultPadding", 2 ); var texture = new Texture2D( 1, 1, TextureFormat.ARGB32, false ); var rects = texture.PackTextures2( selection, padding, dfTextureAtlasInspector.MaxAtlasSize, dfTextureAtlasInspector.ForceSquare ); var texturePath = Path.ChangeExtension( prefabPath, "png" ); byte[] bytes = texture.EncodeToPNG(); System.IO.File.WriteAllBytes( texturePath, bytes ); bytes = null; DestroyImmediate( texture ); setAtlasTextureSettings( texturePath, true ); texture = AssetDatabase.LoadAssetAtPath( texturePath, typeof( Texture2D ) ) as Texture2D; if( texture == null ) Debug.LogError( "Failed to find texture at " + texturePath ); var sprites = new List<dfAtlas.ItemInfo>(); for( int i = 0; i < rects.Length; i++ ) { var pixelCoords = rects[ i ]; var size = new Vector2( selection[ i ].width, selection[ i ].height ); var spritePath = AssetDatabase.GetAssetPath( selection[ i ] ); var guid = AssetDatabase.AssetPathToGUID( spritePath ); var item = new dfAtlas.ItemInfo() { name = selection[ i ].name, region = pixelCoords, rotated = false, textureGUID = guid, sizeInPixels = size }; sprites.Add( item ); } sprites.Sort(); var shader = Shader.Find( "Daikon Forge/Default UI Shader" ); var atlasMaterial = new Material( shader ); atlasMaterial.mainTexture = texture; AssetDatabase.CreateAsset( atlasMaterial, Path.ChangeExtension( texturePath, "mat" ) ); var go = new GameObject() { name = Path.GetFileNameWithoutExtension( prefabPath ) }; var atlas = go.AddComponent<dfAtlas>(); atlas.Material = atlasMaterial; atlas.AddItems( sprites ); var prefab = PrefabUtility.CreateEmptyPrefab( prefabPath ); prefab.name = atlas.name; PrefabUtility.ReplacePrefab( go, prefab ); DestroyImmediate( go ); AssetDatabase.Refresh(); #region Delay execution of object selection to work around a Unity issue // Declared with null value to eliminate "uninitialized variable" // compiler error in lambda below. EditorApplication.CallbackFunction callback = null; callback = () => { EditorUtility.FocusProjectWindow(); go = AssetDatabase.LoadMainAssetAtPath( prefabPath ) as GameObject; Selection.objects = new Object[] { go }; EditorGUIUtility.PingObject( go ); Debug.Log( "Texture Atlas prefab created at " + prefabPath, prefab ); EditorApplication.delayCall -= callback; }; EditorApplication.delayCall += callback; #endregion } catch( Exception err ) { Debug.LogError( err.ToString() ); EditorUtility.DisplayDialog( "Error Creating Texture Atlas", err.Message, "OK" ); } finally { EditorUtility.ClearProgressBar(); } }
private void doImport() { try { var texturePath = AssetDatabase.GetAssetPath( textureFile ); //var textureGUID = AssetDatabase.AssetPathToGUID( texturePath ); var uvx = 1f / textureFile.width; var uvy = 1f / textureFile.height; var sprites = new List<dfAtlas.ItemInfo>(); var data = JSON.JsonDecode( dataFile.text ) as DICT; var frames = data[ "frames" ] as DICT; foreach( var key in frames.Keys ) { var itemData = frames[ key ] as DICT; var spriteName = Path.GetFileNameWithoutExtension( key ); var isRotated = (bool)itemData[ "rotated" ]; if( isRotated ) { Debug.LogError( string.Format( "Sprite '{0}' is rotated. Rotated sprites are not yet supported", spriteName ) ); continue; } var frameRect = extractUVRect( itemData[ "frame" ] as DICT, textureFile ); var spriteSize = new Vector2( frameRect.width / uvx, frameRect.height / uvy ); var sprite = new dfAtlas.ItemInfo() { name = spriteName, border = new RectOffset(), deleted = false, region = frameRect, rotated = false, sizeInPixels = spriteSize }; sprites.Add( sprite ); } sprites.Sort(); var dataFilePath = AssetDatabase.GetAssetPath( dataFile ); var defaultFilename = Path.GetFileNameWithoutExtension( dataFilePath ); var saveFolder = Path.GetDirectoryName( texturePath ); var prefabPath = EditorUtility.SaveFilePanel( "Import Texture Atlas", saveFolder, defaultFilename, "prefab" ); if( string.IsNullOrEmpty( prefabPath ) ) return; prefabPath = prefabPath.MakeRelativePath(); dfTextureAtlasInspector.setAtlasTextureSettings( texturePath, true ); var texture = AssetDatabase.LoadAssetAtPath( texturePath, typeof( Texture2D ) ) as Texture2D; if( texture == null ) Debug.LogError( "Failed to find texture at " + texturePath ); var shader = Shader.Find( "Daikon Forge/Default UI Shader" ); var atlasMaterial = new Material( shader ); atlasMaterial.mainTexture = texture; AssetDatabase.CreateAsset( atlasMaterial, Path.ChangeExtension( texturePath, "mat" ) ); var go = new GameObject() { name = Path.GetFileNameWithoutExtension( prefabPath ) }; var atlas = go.AddComponent<dfAtlas>(); atlas.Material = atlasMaterial; atlas.AddItems( sprites ); atlas.generator = dfAtlas.TextureAtlasGenerator.TexturePacker; atlas.imageFileGUID = AssetDatabase.AssetPathToGUID( texturePath ); atlas.dataFileGUID = AssetDatabase.AssetPathToGUID( dataFilePath ); var prefab = PrefabUtility.CreateEmptyPrefab( prefabPath ); prefab.name = atlas.name; PrefabUtility.ReplacePrefab( go, prefab ); DestroyImmediate( go ); AssetDatabase.Refresh(); #region Delay execution of object selection to work around a Unity issue // Declared with null value to eliminate "uninitialized variable" // compiler error in lambda below. EditorApplication.CallbackFunction callback = null; callback = () => { EditorUtility.FocusProjectWindow(); go = AssetDatabase.LoadMainAssetAtPath( prefabPath ) as GameObject; Selection.objects = new UnityEngine.Object[] { go }; EditorGUIUtility.PingObject( go ); Debug.Log( "Texture Atlas prefab created at " + prefabPath, prefab ); EditorApplication.delayCall -= callback; }; EditorApplication.delayCall += callback; #endregion } catch( Exception err ) { EditorUtility.DisplayDialog( "Failed to parse data file", "Unable to parse the data file: " + err.Message, "CANCEL" ); } }
internal static void Reimport( dfAtlas atlas ) { var textureFilePath = AssetDatabase.GUIDToAssetPath( atlas.imageFileGUID ); if( !File.Exists( textureFilePath ) ) { Debug.LogError( string.Format( "The image file for atlas {0} could not be found", atlas.name ), atlas ); return; } var dataFilePath = AssetDatabase.GUIDToAssetPath( atlas.dataFileGUID ); if( !File.Exists( dataFilePath ) ) { Debug.LogError( string.Format( "The data file for atlas {0} could not be found", atlas.name ), atlas ); return; } var dataFile = AssetDatabase.LoadAssetAtPath( dataFilePath, typeof( TextAsset ) ) as TextAsset; if( dataFile == null ) { Debug.LogError( string.Format( "Faile to open the data file for the {0} atlas", atlas.name ), atlas ); return; } var textureFile = AssetDatabase.LoadAssetAtPath( textureFilePath, typeof( Texture2D ) ) as Texture2D; if( textureFile == null ) { Debug.LogError( string.Format( "Faile to open the image file for the {0} atlas", atlas.name ), atlas ); return; } dfTextureAtlasInspector.setAtlasTextureSettings( textureFilePath, false ); var uvx = 1f / textureFile.width; var uvy = 1f / textureFile.height; var newSprites = new List<dfAtlas.ItemInfo>(); var oldSprites = atlas.Items; var data = JSON.JsonDecode( dataFile.text ) as DICT; var frames = data[ "frames" ] as DICT; foreach( var key in frames.Keys ) { var itemData = frames[ key ] as DICT; var spriteName = Path.GetFileNameWithoutExtension( key ); var isRotated = (bool)itemData[ "rotated" ]; if( isRotated ) { Debug.LogError( string.Format( "Sprite '{0}' is rotated. Rotated sprites are not yet supported", spriteName ) ); continue; } var frameRect = extractUVRect( itemData[ "frame" ] as DICT, textureFile ); var spriteSize = new Vector2( frameRect.width / uvx, frameRect.height / uvy ); var sprite = new dfAtlas.ItemInfo() { name = spriteName, border = new RectOffset(), deleted = false, region = frameRect, rotated = false, sizeInPixels = spriteSize }; newSprites.Add( sprite ); for( int i = 0; i < oldSprites.Count; i++ ) { var old = oldSprites[ i ]; if( string.Equals( old.name, spriteName, StringComparison.OrdinalIgnoreCase ) ) { sprite.border = old.border; break; } } } newSprites.Sort(); atlas.Items.Clear(); atlas.Items.AddRange( newSprites ); var prefabPath = AssetDatabase.GetAssetPath( atlas ); var go = atlas.gameObject; #region Delay execution of object selection to work around a Unity issue // Declared with null value to eliminate "uninitialized variable" // compiler error in lambda below. EditorApplication.CallbackFunction callback = null; callback = () => { EditorUtility.FocusProjectWindow(); go = AssetDatabase.LoadMainAssetAtPath( prefabPath ) as GameObject; Selection.objects = new UnityEngine.Object[] { go }; EditorGUIUtility.PingObject( go ); Debug.Log( "Texture Atlas prefab re-imported at " + prefabPath, go ); EditorApplication.delayCall -= callback; }; EditorApplication.delayCall += callback; #endregion }