diff options
author | chai <chaifix@163.com> | 2022-04-23 13:24:05 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-04-23 13:24:05 +0800 |
commit | bdb737230b30e8fb9be63d95b792e8c8ba531ea0 (patch) | |
tree | 9e1fe83c65af581d6b3411bcf1b00af4bde8990a /SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs | |
parent | 747a355b8a7d273264ce1d8f1848633f8fa52c47 (diff) |
* ui staff
Diffstat (limited to 'SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs')
-rw-r--r-- | SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs | 118 |
1 files changed, 116 insertions, 2 deletions
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs index 55898f2..552672a 100644 --- a/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs @@ -1,9 +1,123 @@ using System.Collections; using System.Collections.Generic; using UnityEngine; +using UnityEngine.UI; -public class ItemWidget : MonoBehaviour +public struct ItemWidgetParam { - + // Ö÷¶¯µã»÷ + public System.Action<ItemWidget> onSelected; + public ItemBase item; +} + +public class ItemWidget : UIGridItemBase +{ + public Image Image_Icon; + public Image Image_SelectBg; + + public Image Image_Use; + + private System.Action<ItemWidget> onSelected; + + public ItemBase item { get { return m_Item; } } + private ItemBase m_Item; + + private Coroutine m_CoUse; + + private int m_PendingUse = 0; + + public void SetSelectBg(bool selected) + { + Image_SelectBg.gameObject.SetActive(selected); + } + + public override void Set(object param) + { + ItemWidgetParam info = (ItemWidgetParam)param; + onSelected = info.onSelected; + m_Item = info.item; + + Image_Icon.sprite = ResourceManager.Instance.Load<Sprite>(info.item.iconPath); + + SetSelectBg(false); + + Image_Use.gameObject.SetActive(false); + } + + public void OnSelectCallback() + { + SetSelectBg(true); + } + + public void OnDeselectCallback() + { + SetSelectBg(false); + } + + public void OnUseCallback() + { + m_PendingUse++; + PlayUseAnimation(); + } + + private void PlayUseAnimation() + { + if (m_CoUse != null) + return; + Image_Use.gameObject.SetActive(true); + m_CoUse = StartCoroutine(CoUseAnimation()); + } + + IEnumerator CoUseAnimation() + { + float speed = 5f; + + while(m_PendingUse > 0) + { + Image_Use.fillOrigin = (int)Image.OriginVertical.Bottom; + float t = 0; + while (true) + { + t += speed * Time.deltaTime; + + if (t > 1) + break; + + Image_Use.fillAmount = Mathf.Lerp(0, 1, t); + + yield return null; + } + + Image_Use.fillOrigin = (int)Image.OriginVertical.Top; + t = 0; + while (true) + { + t += speed * Time.deltaTime; + + if (t > 1) + break; + + Image_Use.fillAmount = Mathf.Lerp(1, 0, t); + + yield return null; + } + m_PendingUse--; + } + + Image_Use.gameObject.SetActive(false); + + m_CoUse = null; + + yield break; + } + + private void StopUseAnimation() + { + if (m_CoUse != null) + { + StopCoroutine(m_CoUse); + } + Image_Use.gameObject.SetActive(false); + } } |