From 8131033510c711248de1904649cfa1dbe4bbe69f Mon Sep 17 00:00:00 2001 From: chai Date: Mon, 25 Apr 2022 10:09:11 +0800 Subject: *rename item to prop --- .../Scripts/UI/Panel/PanelPropBar/PropWidget.cs | 123 +++++++++++++++++++++ 1 file changed, 123 insertions(+) create mode 100644 SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs (limited to 'SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs') diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs new file mode 100644 index 0000000..863e89e --- /dev/null +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs @@ -0,0 +1,123 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public struct PropWidgetParam +{ + // Ö÷¶¯µã»÷ + public System.Action onSelected; + public PropBase prop; +} + +public class PropWidget : UIGridPropBase +{ + public Image Image_Icon; + public Image Image_SelectBg; + + public Image Image_Use; + + private System.Action onSelected; + + public PropBase prop { get { return m_Prop; } } + private PropBase m_Prop; + + 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) + { + PropWidgetParam info = (PropWidgetParam)param; + onSelected = info.onSelected; + m_Prop = info.prop; + + Image_Icon.sprite = ResourceManager.Instance.Load(info.prop.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 = 7f; + + 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); + } + +} -- cgit v1.1-26-g67d0