summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2022-04-24 15:59:07 +0800
committerchai <chaifix@163.com>2022-04-24 15:59:07 +0800
commit6158af99ad803d9e29096ee6f65026d5e0db0f1f (patch)
tree7e9a58f33ad255c721f92a84d6ded8847f8c5b8f /SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs
parent1ab081709bbba66a426371e07efe38bc36072453 (diff)
*misc
Diffstat (limited to 'SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs')
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs95
1 files changed, 95 insertions, 0 deletions
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs
new file mode 100644
index 0000000..ecb6ea1
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelTopStuffBar/UIItemBar.cs
@@ -0,0 +1,95 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+
+public class UIItemBar : MonoBehaviour
+{
+ public ItemWidget m_ItemTempalte;
+
+ public UISimpleGrid m_ItemGrid;
+
+ public Text m_TextName;
+
+ private List<ItemWidget> m_Items = new List<ItemWidget>();
+
+ private int m_CurrentIndex = 0;
+
+ public void Set()
+ {
+ for (int i = 0; i < PlayerManager.Instance.items.Count; ++i)
+ {
+ ItemWidget widget = MakeItemWidget(PlayerManager.Instance.items[i]);
+ m_Items.Add(widget);
+ }
+
+ SelectItemWidget(0);
+ }
+
+ bool SwitchToLeft()
+ {
+ return Input.GetButtonDown("LeftItem");
+ }
+
+ bool SwitchToRight()
+ {
+ return Input.GetButtonDown("RightItem");
+ }
+
+ bool UseItem()
+ {
+ return Input.GetButtonDown("Fire3");
+ }
+
+ ItemWidget MakeItemWidget(ItemBase item)
+ {
+ ItemWidget widget = Instantiate<ItemWidget>(m_ItemTempalte);
+ widget.transform.SetParent(m_ItemGrid.transform);
+ widget.gameObject.SetActive(true);
+ widget.GetComponent<RectTransform>().anchoredPosition = new Vector2(0, 0);
+#if UNITY_EDITOR
+ widget.name = "item (" + item.name + ")";
+#endif
+
+ ItemWidgetParam param = new ItemWidgetParam();
+ //param.onSelected = OnSelectItemWidget;
+ param.item = item;
+ widget.Set(param);
+ return widget;
+ }
+
+ protected void Update()
+ {
+ if (SwitchToLeft())
+ {
+ int newIndex = Mathf.Clamp(m_CurrentIndex - 1, 0, m_Items.Count - 1);
+ SelectItemWidget(newIndex);
+ }
+ if (SwitchToRight())
+ {
+ int newIndex = Mathf.Clamp(m_CurrentIndex + 1, 0, m_Items.Count - 1);
+ SelectItemWidget(newIndex);
+ }
+ if (UseItem())
+ {
+ m_Items[m_CurrentIndex].OnUseCallback();
+ PlayerManager.Instance.UseItem(m_Items[m_CurrentIndex].item);
+ }
+ }
+
+ void SelectItemWidget(int index)
+ {
+ if (index < 0 || index > m_Items.Count - 1)
+ {
+ return;
+ }
+ m_Items[m_CurrentIndex].OnDeselectCallback();
+ m_Items[index].OnSelectCallback();
+
+ m_TextName.text = m_Items[index].item.name;
+ m_TextName.gameObject.SetActive(false);
+
+ m_CurrentIndex = index;
+ }
+
+}