summaryrefslogtreecommitdiff
path: root/SurvivalTest/Assets/Scripts/UI
diff options
context:
space:
mode:
authorchai <chaifix@163.com>2022-04-23 13:24:05 +0800
committerchai <chaifix@163.com>2022-04-23 13:24:05 +0800
commitbdb737230b30e8fb9be63d95b792e8c8ba531ea0 (patch)
tree9e1fe83c65af581d6b3411bcf1b00af4bde8990a /SurvivalTest/Assets/Scripts/UI
parent747a355b8a7d273264ce1d8f1848633f8fa52c47 (diff)
* ui staff
Diffstat (limited to 'SurvivalTest/Assets/Scripts/UI')
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs10
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs.meta11
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Panel/PanelBase.cs35
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Panel/PanelEquipBar.cs16
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/ItemWidget.cs118
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/PanelItemBar.cs70
-rw-r--r--SurvivalTest/Assets/Scripts/UI/UIManager_Panels.cs37
-rw-r--r--SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs13
-rw-r--r--SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs.meta11
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets.meta (renamed from SurvivalTest/Assets/Scripts/UI/Widget.meta)0
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs (renamed from SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs)0
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs.meta (renamed from SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs.meta)0
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs18
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs.meta11
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs22
-rw-r--r--SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs.meta11
16 files changed, 350 insertions, 33 deletions
diff --git a/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs b/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs
new file mode 100644
index 0000000..8e5ff24
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs
@@ -0,0 +1,10 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public abstract class UIGridItemBase : MonoBehaviour
+{
+
+ public abstract void Set(object param);
+
+}
diff --git a/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs.meta b/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs.meta
new file mode 100644
index 0000000..a3f1f69
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Common/UIGridItemBase.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 9e490364ec1d9c8419f9bece4e2ab365
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelBase.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelBase.cs
index 3081d97..9d1157c 100644
--- a/SurvivalTest/Assets/Scripts/UI/Panel/PanelBase.cs
+++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelBase.cs
@@ -2,26 +2,41 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
+[ExecuteInEditMode]
[DisallowMultipleComponent]
-public class PanelBase : MonoBehaviour
+public abstract class PanelBase : MonoBehaviour
{
- public virtual void Set(object param)
+ public abstract void Set(object param);
+
+ public virtual void OnEnable()
{
+ InitRectTransform();
}
- void Start()
- {
-
- }
+ public void InitRectTransform()
+ {
+ RectTransform rect = gameObject.GetComponent<RectTransform>();
+ rect.anchorMin = new Vector2(0, 0);
+ rect.anchorMax = new Vector2(1, 1);
+
+ rect.localScale = new Vector3(1, 1, 1);
+
+ rect.anchoredPosition3D = Vector3.zero;
+
+ rect.offsetMin = new Vector2(0, 0);
+ rect.offsetMax = new Vector2(0, 0);
- protected virtual void Update()
- {
- }
+ rect.pivot = new Vector2(0.5f, 0.5f);
+ }
+
+ protected virtual void Update()
+ {
+ }
protected virtual void OnSecondUpdate()
{
}
-}
+} \ No newline at end of file
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelEquipBar.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelEquipBar.cs
index 61f99e5..dbec362 100644
--- a/SurvivalTest/Assets/Scripts/UI/Panel/PanelEquipBar.cs
+++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelEquipBar.cs
@@ -2,17 +2,9 @@ using System.Collections;
using System.Collections.Generic;
using UnityEngine;
-public class PanelEquipBar : MonoBehaviour
+public class PanelEquipBar : PanelBase
{
- // Start is called before the first frame update
- void Start()
- {
-
- }
-
- // Update is called once per frame
- void Update()
- {
-
- }
+ public override void Set(object param)
+ {
+ }
}
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);
+ }
}
diff --git a/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/PanelItemBar.cs b/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/PanelItemBar.cs
index 00acafc..74f90c3 100644
--- a/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/PanelItemBar.cs
+++ b/SurvivalTest/Assets/Scripts/UI/Panel/PanelItemBar/PanelItemBar.cs
@@ -1,13 +1,31 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
+using UnityEngine.UI;
public class PanelItemBar : PanelBase
{
- /// <summary>
- /// 当前持有的Items
- /// </summary>
- public List<ItemWidget> m_Items = new List<ItemWidget>();
+ public ItemWidget m_ItemTempalte;
+
+ public UISimpleGrid m_ItemGrid;
+
+ public Text m_TextName;
+
+ // 当前持有的Items
+ private List<ItemWidget> m_Items = new List<ItemWidget>();
+
+ private int m_CurrentIndex = 0;
+
+ public override void Set(object param)
+ {
+ 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()
{
@@ -19,14 +37,58 @@ public class PanelItemBar : PanelBase
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);
+
+ ItemWidgetParam param = new ItemWidgetParam();
+ //param.onSelected = OnSelectItemWidget;
+ param.item = item;
+ widget.Set(param);
+ return widget;
+ }
+
protected override 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.UseCurrentItem();
+ }
+ }
+
+ 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_CurrentIndex = index;
+
+ PlayerManager.Instance.SetCurrentItem(m_Items[index].item);
}
}
diff --git a/SurvivalTest/Assets/Scripts/UI/UIManager_Panels.cs b/SurvivalTest/Assets/Scripts/UI/UIManager_Panels.cs
index 049f733..d29c24a 100644
--- a/SurvivalTest/Assets/Scripts/UI/UIManager_Panels.cs
+++ b/SurvivalTest/Assets/Scripts/UI/UIManager_Panels.cs
@@ -1,18 +1,27 @@
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
+using UnityEngine.UI;
public enum PanelType
{
- None,
+ None,
- PanelLevelBar,
+ PanelLevelBar,
PanelItemBar,
}
public partial class UIManager : Singleton<UIManager>
{
private Dictionary<PanelType, string> m_Panels = new Dictionary<PanelType, string>();
+ private Dictionary<PanelType, PanelBase> m_OpenedPanels = new Dictionary<PanelType, PanelBase>();
+
+ private Canvas m_Canvas;
+
+ public void SetRootCanvas(Canvas canvas)
+ {
+ m_Canvas = canvas;
+ }
void SetPanels()
{
@@ -25,9 +34,27 @@ public partial class UIManager : Singleton<UIManager>
m_Panels.Add(type, "prefabs/ui/" + path);
}
- void OpenPanel(PanelType type, object param)
+ public PanelBase OpenPanel(PanelType type, object param = null)
{
-
+ if(m_OpenedPanels.ContainsKey(type))
+ {
+ return m_OpenedPanels[type];
+ }
+
+ PanelBase prefab = ResourceManager.Instance.Load<PanelBase>(m_Panels[type]);
+ if (prefab == null)
+ {
+ Debug.LogError("UI Prefab in not available, path=" + m_Panels[type]);
+ return null;
+ }
+ PanelBase panel = UnityEngine.Object.Instantiate<PanelBase>(prefab);
+ panel.name = prefab.name;
+ panel.transform.SetParent(m_Canvas.transform);
+ panel.Set(param);
+ panel.InitRectTransform();
+ panel.gameObject.SetActive(true);
+
+ return panel;
}
-}
+} \ No newline at end of file
diff --git a/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs b/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs
new file mode 100644
index 0000000..541d21a
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs
@@ -0,0 +1,13 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class UIRootCanvas : MonoBehaviour
+{
+
+ private void Awake()
+ {
+ UIManager.Instance.SetRootCanvas(gameObject.GetComponent<Canvas>());
+ }
+
+}
diff --git a/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs.meta b/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs.meta
new file mode 100644
index 0000000..5423e4e
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/UIRootCanvas.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 097b021f29d7fc8468055a35bbc92ca8
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/SurvivalTest/Assets/Scripts/UI/Widget.meta b/SurvivalTest/Assets/Scripts/UI/Widgets.meta
index c0a26ea..c0a26ea 100644
--- a/SurvivalTest/Assets/Scripts/UI/Widget.meta
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets.meta
diff --git a/SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs b/SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs
index 616b69d..616b69d 100644
--- a/SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs
diff --git a/SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs.meta b/SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs.meta
index 41e16d0..41e16d0 100644
--- a/SurvivalTest/Assets/Scripts/UI/Widget/UIButton.cs.meta
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UIButton.cs.meta
diff --git a/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs b/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs
new file mode 100644
index 0000000..5eeee61
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs
@@ -0,0 +1,18 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+
+public class UIGrid : MonoBehaviour
+{
+ // Start is called before the first frame update
+ void Start()
+ {
+
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+
+ }
+}
diff --git a/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs.meta b/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs.meta
new file mode 100644
index 0000000..c3570c0
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UIGrid.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: e0cbe5d6d2aaef54dba4bb837871787f
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant:
diff --git a/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs b/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs
new file mode 100644
index 0000000..a4e480a
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs
@@ -0,0 +1,22 @@
+using System.Collections;
+using System.Collections.Generic;
+using UnityEngine;
+using UnityEngine.UI;
+
+/// <summary>
+/// 简易Grid
+/// </summary>
+public class UISimpleGrid : MonoBehaviour
+{
+ // Start is called before the first frame update
+ void Start()
+ {
+
+ }
+
+ // Update is called once per frame
+ void Update()
+ {
+
+ }
+}
diff --git a/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs.meta b/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs.meta
new file mode 100644
index 0000000..e071d58
--- /dev/null
+++ b/SurvivalTest/Assets/Scripts/UI/Widgets/UISimpleGrid.cs.meta
@@ -0,0 +1,11 @@
+fileFormatVersion: 2
+guid: 4dbae61f498dde342a95489aca8ff68c
+MonoImporter:
+ externalObjects: {}
+ serializedVersion: 2
+ defaultReferences: []
+ executionOrder: 0
+ icon: {instanceID: 0}
+ userData:
+ assetBundleName:
+ assetBundleVariant: