//-------------------------------------------------
// NGUI: Next-Gen UI kit
// Copyright © 2011-2019 Tasharen Entertainment Inc
//-------------------------------------------------
using UnityEngine;
using System.Collections.Generic;
///
/// Sprite collection is a widget that contains a bunch of sprites that don't create their own game objects and colliders.
/// Its best usage is to replace the need to create individual game objects while still maintaining full visualization
/// and interaction functionality of NGUI's sprites. For example: a world map with thousands of individual icons.
/// The thousands of individual icons can be a single Sprite Collection. Its downside is that the sprites can't be
/// interacted with in the Editor window, as this is meant to be a fast, programmable solution.
///
[ExecuteInEditMode]
[AddComponentMenu("NGUI/UI/Sprite Collection")]
public class UISpriteCollection : UIBasicSprite
{
///
/// Sub-sprite entry within the collection.
///
public struct Sprite
{
public UISpriteData sprite;
public Vector2 pos;
public float rot;
public float width;
public float height;
public Color32 color;
public Vector2 pivot;
public Type type;
public Flip flip;
public bool enabled;
///
/// Calculate the sprite's drawing dimensions.
///
public Vector4 GetDrawingDimensions (float pixelSize)
{
var x0 = -pivot.x * width;
var y0 = -pivot.y * height;
var x1 = x0 + width;
var y1 = y0 + height;
if (sprite != null && type != Type.Tiled)
{
var padLeft = sprite.paddingLeft;
var padBottom = sprite.paddingBottom;
var padRight = sprite.paddingRight;
var padTop = sprite.paddingTop;
if (type != Type.Simple && pixelSize != 1f)
{
padLeft = Mathf.RoundToInt(pixelSize * padLeft);
padBottom = Mathf.RoundToInt(pixelSize * padBottom);
padRight = Mathf.RoundToInt(pixelSize * padRight);
padTop = Mathf.RoundToInt(pixelSize * padTop);
}
var w = sprite.width + padLeft + padRight;
var h = sprite.height + padBottom + padTop;
var px = 1f;
var py = 1f;
if (w > 0 && h > 0 && (type == Type.Simple || type == Type.Filled))
{
if ((w & 1) != 0) ++padRight;
if ((h & 1) != 0) ++padTop;
px = (1f / w) * width;
py = (1f / h) * height;
}
if (flip == Flip.Horizontally || flip == Flip.Both)
{
x0 += padRight * px;
x1 -= padLeft * px;
}
else
{
x0 += padLeft * px;
x1 -= padRight * px;
}
if (flip == Flip.Vertically || flip == Flip.Both)
{
y0 += padTop * py;
y1 -= padBottom * py;
}
else
{
y0 += padBottom * py;
y1 -= padTop * py;
}
}
return new Vector4(x0, y0, x1, y1);
}
}
[HideInInspector, SerializeField] Object mAtlas;
// List of individual sprites
[System.NonSerialized] Dictionary