AInventoryItemSlotUI

 avatar
user_3183455
csharp
6 days ago
2.5 kB
2
Indexable
Never
using UnityEngine;
using UnityEngine.UI;
 
namespace CuongMono.Item.Inventory
{
    public abstract class AInventoryItemSlotUI : ItemSlotUI
    {
        [Header("Abstract Inventory Item Slot UI")]
        [SerializeField] protected Slider duranceLeftSlider;
        public abstract InventoryItemSlot ItemSlot { get; set; }
        public override Item Item
        {
            get
            {
                return ItemSlot.Item;
            }
            set { }
        }
 
        public override void UpdateUI()
        {
            if (this.ItemSlot.Item != null)
            {
                this.EnableIconImage();
                this.EnableQuanityText();
                this.EnableDurance();
            }
            else
            {
                this.DisableIconImage();
                this.DisableQuanityText();
                this.DisableDurance();
            }
        }
     
        protected void EnableIconImage()
        {
            if (this.iconImage == null) return;
            this.iconImage.sprite = this.ItemSlot.Item.Icon;
            this.canvasGroup.alpha = 1;
        }
        protected void DisableIconImage()
        {
            if (this.iconImage == null) return;
            this.canvasGroup.alpha = 0;
        }
 
        protected void EnableQuanityText()
        {
            if (this.quanityText == null) return;
            this.quanityText.text = this.ItemSlot.Quanity > 1 ?
                this.ItemSlot.Quanity.ToString() : "";
            this.quanityText.enabled = true;
        }
        protected void DisableQuanityText()
        {
            if (this.quanityText == null) return;
            this.quanityText.enabled = false;
        }
        protected void EnableDurance()
        {
            if (this.duranceLeftSlider == null) return;
            if (this.ItemSlot.DuranceLeft <= 0 || this.ItemSlot.Item.MaxDurance <= 0)
                this.duranceLeftSlider.value = 1;
            else this.duranceLeftSlider.value = (float)this.ItemSlot.DuranceLeft / (float)this.ItemSlot.Item.MaxDurance;
            if(this.duranceLeftSlider.value >= 1) this.duranceLeftSlider.gameObject.SetActive(false);
            else this.duranceLeftSlider.gameObject.SetActive(true);
        }
        protected void DisableDurance()
        {
            if (this.duranceLeftSlider == null) return;
            this.duranceLeftSlider.gameObject.SetActive(false);
        }
 
    }
}
Leave a Comment