diff options
author | chai <chaifix@163.com> | 2022-04-25 10:09:11 +0800 |
---|---|---|
committer | chai <chaifix@163.com> | 2022-04-25 10:09:11 +0800 |
commit | 8131033510c711248de1904649cfa1dbe4bbe69f (patch) | |
tree | 80e0db1f24c6d624ac412f240f54cc18e626c3d0 /SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar | |
parent | 71ae3d755a03a706d93291ca57c6de8e6f0d5926 (diff) |
*rename item to prop
Diffstat (limited to 'SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar')
4 files changed, 240 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs new file mode 100644 index 0000000..99a2c58 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs @@ -0,0 +1,95 @@ +using System.Collections; +using System.Collections.Generic; +using UnityEngine; +using UnityEngine.UI; + +public class PanelPropBar : PanelBase +{ + public PropWidget m_PropTempalte; + + public UISimpleGrid m_PropGrid; + + public Text m_TextName; + + private List<PropWidget> m_Props = new List<PropWidget>(); + + private int m_CurrentIndex = 0; + + public override void Set(object param) + { + for(int i = 0; i < PlayerManager.Instance.props.Count; ++i) + { + PropWidget widget = MakePropWidget(PlayerManager.Instance.props[i]); + m_Props.Add(widget); + } + + SelectPropWidget(0); + } + + bool SwitchToLeft() + { + return Input.GetButtonDown("LeftProp"); + } + + bool SwitchToRight() + { + return Input.GetButtonDown("RightProp"); + } + + bool UseProp() + { + return Input.GetButtonDown("Fire3"); + } + + PropWidget MakePropWidget(PropBase prop) + { + PropWidget widget = Instantiate<PropWidget>(m_PropTempalte); + widget.transform.SetParent(m_PropGrid.transform); + widget.gameObject.SetActive(true); + widget.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0); +#if UNITY_EDITOR + widget.name = "prop (" + prop.name + ")"; +#endif + + PropWidgetParam param = new PropWidgetParam(); + //param.onSelected = OnSelectPropWidget; + param.prop = prop; + widget.Set(param); + return widget; + } + + protected override void Update() + { + if (SwitchToLeft()) + { + int newIndex = Mathf.Clamp(m_CurrentIndex - 1, 0, m_Props.Count - 1); + SelectPropWidget(newIndex); + } + if (SwitchToRight()) + { + int newIndex = Mathf.Clamp(m_CurrentIndex + 1, 0, m_Props.Count - 1); + SelectPropWidget(newIndex); + } + if (UseProp()) + { + m_Props[m_CurrentIndex].OnUseCallback(); + PlayerManager.Instance.UseProp(m_Props[m_CurrentIndex].prop); + } + } + + void SelectPropWidget(int index) + { + if(index < 0 || index > m_Props.Count - 1) + { + return ; + } + m_Props[m_CurrentIndex].OnDeselectCallback(); + m_Props[index].OnSelectCallback(); + + m_TextName.text = m_Props[index].prop.name; + m_TextName.gameObject.SetActive(false); + + m_CurrentIndex = index; + } + +} diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs.meta b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs.meta new file mode 100644 index 0000000..3925db5 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PanelPropBar.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b3e9f15745561dd4f9119ac8f893dbfc +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: 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<PropWidget> onSelected; + public PropBase prop; +} + +public class PropWidget : UIGridPropBase +{ + public Image Image_Icon; + public Image Image_SelectBg; + + public Image Image_Use; + + private System.Action<PropWidget> 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<Sprite>(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); + } + +} diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs.meta b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs.meta new file mode 100644 index 0000000..f9f85a3 --- /dev/null +++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelPropBar/PropWidget.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: 4ccf65ebe78138644a8bc5283d21ae1a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: |