using System; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using TMPro; using DG.Tweening; public class BagControl : MonoSingleton { public RemainsBag bag; [Header("空精灵")] public Sprite Blocksprite; public Dictionary remainsModes = new Dictionary();//借出去的 public Action InteractiveEvent; public GameObject DragUI; [HideInInspector] public GraphicRaycaster graphicRaycaster; private RectTransform rectTransform; private Vector2 AwakePos;//背包初始位置 public GameObject Openbtn; public override void Awake() { base.Awake(); graphicRaycaster = transform.parent.GetComponent(); rectTransform = GetComponent(); AwakePos = rectTransform.anchoredPosition; gameObject.SetActive(false); } private void OnEnable() { Refresh(); } private void OnDestroy() { } public void OpenBag(GameObject obj) { RectTransform objRectTransform = obj.GetComponent(); bool isOpening = !gameObject.activeSelf; gameObject.SetActive(true); // 确保 UI 处于激活状态以显示动画 if (isOpening) { // 打开时,UI 向下移动,obj 向右移动 200 个单位 rectTransform.DOAnchorPos(new Vector2(AwakePos.x, AwakePos.y - 600), 0.5f) .SetEase(Ease.OutQuad) .OnComplete(() => gameObject.SetActive(true)); objRectTransform.DOAnchorPos(new Vector2(objRectTransform.anchoredPosition.x, objRectTransform.anchoredPosition.y - 500), 0.5f) .SetEase(Ease.OutQuad); } else { // 关闭时,UI 和 obj 返回初始位置 rectTransform.DOAnchorPos(AwakePos, 0.5f) .SetEase(Ease.OutQuad) .OnComplete(() => gameObject.SetActive(false)); objRectTransform.DOAnchorPos(new Vector2(objRectTransform.anchoredPosition.x, objRectTransform.anchoredPosition.y + 500), 0.5f) .SetEase(Ease.OutQuad); } } public void CloseBagTwo() { YiShiUI.Instance.gameObject.SetActive(false); YiShiUI.Instance.BagActive(false); } public void Refresh() { Transform pa = transform.Find("背包/内容/遗物组"); int childIndex = 0; for (int i = 0; i < pa.childCount; i++, childIndex++) { if (bag.Items.Count > childIndex) { int RealCount = bag.Items[childIndex].Count; if (RealCount > 0)//背包有东西 { if (remainsModes.TryGetValue(bag.Items[childIndex].Rename, out RemainsMode remainsMode))//---借出去的有该类 { Debug.LogWarning("借出去的有该类"); RealCount -= remainsMode.Count; if (RealCount == 0) { i--; continue; } } Transform child = pa.GetChild(i); child.GetComponent().AwkaeItem(bag.Items[childIndex].Rename, RealCount); } else i--; } else { Transform child = pa.GetChild(i); child.GetComponent().Refresh(); } } MiaosuRemains(""); } /// /// 移出背包 /// public void RemoveOut(string name, int count = 1) { RemainsMode remainsMode; Debug.LogWarning("移出背包"); if (remainsModes.TryGetValue(name, out remainsMode)) { remainsMode.Count += count; remainsModes[name] = remainsMode; Debug.LogWarning("移出背包" + name); } else { Debug.LogWarning("移出背包sss" + name); remainsMode = bag.GetRemainsMode(name); remainsMode.Count = 1; remainsModes.Add(name, remainsMode); } InteractiveEvent?.Invoke(); } /// /// 移入背包 /// public void RemoveIn(string Rename, int count = 1) { if (remainsModes.TryGetValue(Rename, out RemainsMode remainsbagmode)) { remainsbagmode.Count -= count; if (remainsbagmode.Count <= 0) { remainsModes.Remove(remainsbagmode.Rename); } else { remainsModes[Rename] = remainsbagmode; } } } public RectTransform ChangeDragUI(string Rename) { DragUI.gameObject.SetActive(true); DragUI.GetComponent().sprite = bag.GetSprite(Rename); return DragUI.GetComponent(); } /// /// 消耗遗物 /// public void CutRemains() { foreach (KeyValuePair item in remainsModes) { bag.CutItems(item.Key, item.Value.Count); } remainsModes.Clear(); Refresh(); } /// /// 增加遗物 /// public void AddRemains(RemainsItem remains) { bag.AddItems(remains); Refresh(); } /// /// 描述遗物 /// public void MiaosuRemains(string remainsName) { if (remainsName == "") { transform.Find("遗物描述").GetComponent().text = ""; transform.Find("功能描述").GetComponent().text = ""; transform.Find("内容描述").GetComponent().text = ""; } else { transform.Find("遗物描述").GetComponent().text = bag.GetRemainsItem(remainsName).Miaosu; transform.Find("功能描述").GetComponent().text = bag.GetRemainsItem(remainsName).BuffMiaosu; transform.Find("内容描述").GetComponent().text = bag.GetRemainsItem(remainsName).ContectMiaosu; } } }