using UnityEngine; using UnityEngine.UI; using System; using UIWidgets; namespace UIWidgetsSamples { /// /// ListViewIcons item component. /// public class TileViewComponentSample : ListViewItem { /// /// The icon. /// [SerializeField] public Image Icon; /// /// The text. /// [SerializeField] public Text Name; /// /// The capital. /// [SerializeField] public Text Capital; /// /// The area. /// [SerializeField] public Text Area; /// /// The population. /// [SerializeField] public Text Population; /// /// The density. /// [SerializeField] public Text Density; /// /// Set icon native size. /// public bool SetNativeSize = true; /// /// TileView. /// public TileViewSample Tiles; /// /// Current item. /// public TileViewItemSample Item; /// /// Duplicate current item in TileView.DataSource. /// public void Duplicate() { Tiles.DataSource.Add(Item); } /// /// Remove current item from TileView.DataSource. /// public void Remove() { Tiles.DataSource.RemoveAt(Index); } /// /// Sets component data with specified item. /// /// Item. public void SetData(TileViewItemSample item) { Item = item; if (Item==null) { if (Icon!=null) { Icon.sprite = null; } if (Name!=null) { Name.text = string.Empty; } if (Capital!=null) { Capital.text = string.Empty; } if (Area!=null) { Area.text = string.Empty; } if (Population!=null) { Population.text = string.Empty; } if (Density!=null) { Density.text = string.Empty; } } else { if (Icon!=null) { Icon.sprite = Item.Icon; } if (Name!=null) { Name.text = Item.Name; } if (Capital!=null) { Capital.text = "Capital: " + Item.Capital; } if (Area!=null) { Area.text = "Area: " + Item.Area.ToString("N0") + " sq. km"; } if (Population!=null) { Population.text = "Population: " + Item.Population.ToString("N0"); } if (Density!=null) { var density = Item.Area==0 ? "n/a" : Mathf.CeilToInt(((float)Item.Population) / Item.Area).ToString("N") + " / sq. km"; Density.text = "Density: " + density; } } if (Icon!=null) { if (SetNativeSize) { Icon.SetNativeSize(); } //set transparent color if no icon Icon.color = (Icon.sprite==null) ? Color.clear : Color.white; } } } }