namespace UIWidgets { using UIWidgets.l10n; using UnityEngine; /// /// TreeView component. /// public class TreeViewComponent : TreeViewComponentBase { TreeViewItem item; /// /// Gets or sets the item. /// /// The item. public TreeViewItem Item { get { return item; } set { if (item != null) { item.OnChange -= UpdateView; } item = value; if (item != null) { item.OnChange += UpdateView; } UpdateView(); } } /// /// Sets the data. /// /// Node. /// Depth. public override void SetData(TreeNode node, int depth) { Node = node; base.SetData(Node, depth); Item = (Node == null) ? null : Node.Item; } /// public override void LocaleChanged() { UpdateName(); } /// /// Update display name. /// protected virtual void UpdateName() { if (Item == null) { return; } TextAdapter.text = Item.LocalizedName ?? Localization.GetTranslation(Item.Name); } /// /// Updates the view. /// protected virtual void UpdateView() { if ((Icon == null) || (TextAdapter == null)) { return; } if (Item == null) { Icon.sprite = null; TextAdapter.text = string.Empty; } else { Icon.sprite = Item.Icon; UpdateName(); } if (SetNativeSize) { Icon.SetNativeSize(); } // set transparent color if no icon Icon.color = (Icon.sprite == null) ? Color.clear : Color.white; } /// /// Called when item moved to cache, you can use it free used resources. /// public override void MovedToCache() { if (Icon != null) { Icon.sprite = null; } } /// /// This function is called when the MonoBehaviour will be destroyed. /// protected override void OnDestroy() { if (item != null) { item.OnChange -= UpdateView; } base.OnDestroy(); } } }