public static Texture2D GetItemOrigin(MadAtlas.Item item) { var path = AssetDatabase.GUIDToAssetPath(item.textureGUID); if (string.IsNullOrEmpty(path)) { return(null); } return(AssetDatabase.LoadAssetAtPath(path, typeof(Texture2D)) as Texture2D); }
private static MadAtlas.Item CreateItem(Texture2D texture, Rect region) { var item = new MadAtlas.Item(); item.name = texture.name; item.pixelsWidth = texture.width; item.pixelsHeight = texture.height; item.region = region; item.textureGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(texture)); return(item); }
Vector2 FixUV(Vector2 uv, MadAtlas.Item item) { if (item != null) { var region = item.region; return(new Vector2(region.x + region.width * uv.x, region.y + region.height * uv.y)); } else { return(uv); } }
void OnEnable() { noneItem = new MadAtlas.Item(); noneItem.name = "None"; EditorApplication.update += () => { // ReSharper disable once ConditionIsAlwaysTrueOrFalse if (parent == null && this != null) { Close(); } }; }
public static void RemoveFromAtlas(MadAtlas atlas, MadAtlas.Item item) { var liveItems = LiveItems(atlas); var newItems = (from el in liveItems where el != item select el).ToList(); atlas.ClearItems(); var allTextures = from el in newItems select MadAtlasUtil.GetItemOrigin(el); string atlasTexturePath = AssetDatabase.GetAssetPath(atlas.atlasTexture); PackTextures(allTextures.ToArray(), atlasTexturePath, ref newItems); atlas.ClearItems(); atlas.AddItemRange(newItems); }
public static void RemoveFromAtlas(MadAtlas atlas, MadAtlas.Item item) { var liveItems = LiveItems(atlas); var newItems = (from el in liveItems where el != item select el).ToList(); atlas.ClearItems(); var allTextures = from el in newItems select MadAtlasUtil.GetItemOrigin(el); var modified = MakeReadable(allTextures); try { string atlasTexturePath = AssetDatabase.GetAssetPath(atlas.atlasTexture); PackTextures(allTextures.ToArray(), atlasTexturePath, ref newItems); atlas.ClearItems(); atlas.AddItemRange(newItems); EditorUtility.SetDirty(atlas); } finally { RevertReadable(modified); AssetDatabase.Refresh(); } }
public override void DrawOn(ref MadList <Vector3> vertices, ref MadList <Color32> colors, ref MadList <Vector2> uv, ref MadList <int> triangles, out Material material) { UpdateTextIfNeeded(); UpdatePivotPoint(); var matrix = TransformMatrix(); var bounds = GetBounds(); MadAtlas.Item spriteItem = null; if (atlas != null) { spriteItem = atlas.GetItem(fontTextureGUID); } material = GetMaterial(); float x = 0; float y = linesCount * scale - scale; for (int i = 0; i < linesCount; ++i) { string text = lines[i]; float lineWidth = lineWidths[i]; switch (align) { case Align.Left: x = 0; break; case Align.Center: x = (bounds.width - lineWidth) / 2; break; case Align.Right: x = bounds.width - lineWidth; break; default: Debug.LogError("Unknown align: " + align); x = 0; break; } for (int j = 0; j < text.Length; ++j) { char c = text[j]; int offset = vertices.Count; var glyph = font.GlyphFor(c); if (glyph == null) { Debug.LogWarning("Glyph not found: '" + c + "' (code " + (int)c + ")"); continue; } // float w = (scale / glyph.height) * glyph.width * font.textureAspect; float xAdvance; var gBounds = GlyphBounds(glyph, out xAdvance); if (c != ' ') // render anything but space { float left = x + gBounds.x; float bottom = y + gBounds.y; float right = left + gBounds.width; float top = bottom + gBounds.height; vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, bottom, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(left, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, top, 0), bounds))); vertices.Add(matrix.MultiplyPoint(PivotPointTranslate(new Vector3(right, bottom, 0), bounds))); colors.Add(tint); colors.Add(tint); colors.Add(tint); colors.Add(tint); uv.Add(FixUV(new Vector2(glyph.uMin, glyph.vMin), spriteItem)); uv.Add(FixUV(new Vector2(glyph.uMin, glyph.vMax), spriteItem)); uv.Add(FixUV(new Vector2(glyph.uMax, glyph.vMax), spriteItem)); uv.Add(FixUV(new Vector2(glyph.uMax, glyph.vMin), spriteItem)); triangles.Add(0 + offset); triangles.Add(1 + offset); triangles.Add(2 + offset); triangles.Add(0 + offset); triangles.Add(2 + offset); triangles.Add(3 + offset); // x += gBounds.width + letterSpacing; } else { // x += gBounds.width; // no letter spacing for blank characters } x += xAdvance; } // end of line y -= scale; } }
void OnEnable() { noneItem = new MadAtlas.Item(); noneItem.name = "None"; }
Vector2 DrawTile(MadAtlas.Item item, Vector2 position, ref Vector2 bounds) { // apply search filter if (!string.IsNullOrEmpty(searchFilter)) { if (!item.name.ToUpper().Contains(searchFilter.ToUpper())) { return(position); } } // check for click event var allRect = new Rect(position.x, position.y, tileSize.x, tileSize.y); if (Event.current.type == EventType.MouseDown && Event.current.button == 0) { if (allRect.Contains(Event.current.mousePosition)) { // handle double click if (selectedItemGUID == item.textureGUID && (DateTime.Now - lastClickTime).TotalMilliseconds < 200) { Close(); } lastClickTime = DateTime.Now; Select(item.textureGUID); } } // draw texture if (item != noneItem) { var textureRect = new Rect(position.x, position.y, textureSize.x, textureSize.y); var texturesCoords = item.region; textureRect = KeepRatio(textureRect, item.pixelsWidth, item.pixelsHeight); GUI.DrawTextureWithTexCoords(textureRect, atlas.atlasTexture, texturesCoords); } // draw label var labelRect = new Rect(position.x, position.y + textureSize.y, labelSize.x, labelSize.y); if (selectedItemGUID == item.textureGUID) { if (EditorGUIUtility.isProSkin) { GUI.color = new Color(63 / 255f, 92 / 255f, 133 / 255f); } else { GUI.color = new Color(63 / 255f, 132 / 255f, 230 / 255f); } GUI.DrawTexture(labelRect, whiteTexture); GUI.color = Color.white; } GUI.Label(labelRect, item.name); // update bounds bounds = new Vector2(Mathf.Max(bounds.x, labelRect.xMax), Mathf.Max(bounds.y, labelRect.yMax)); // calculate next position Vector2 newPosition = position + new Vector2(tileSize.x + tileMargin.x, 0); if (newPosition.x + tileSize.x > Screen.width) { newPosition = new Vector2(0, position.y + tileSize.y + tileMargin.y); } return(newPosition); }
private static MadAtlas.Item CreateItem(Texture2D texture, Rect region) { var item = new MadAtlas.Item(); item.name = texture.name; item.pixelsWidth = texture.width; item.pixelsHeight = texture.height; item.region = region; item.textureGUID = AssetDatabase.AssetPathToGUID(AssetDatabase.GetAssetPath(texture)); return item; }
// =========================================================== // Constants // =========================================================== // =========================================================== // Fields // =========================================================== // =========================================================== // Methods for/from SuperClass/Interfaces // =========================================================== // =========================================================== // Methods // =========================================================== public static string GetItemOriginPath(MadAtlas.Item item) { var path = AssetDatabase.GUIDToAssetPath(item.textureGUID); return(path); }