Fix issue with UI container update

This commit is contained in:
Vova
2023-12-18 10:50:05 +02:00
parent 6bd4971672
commit f6e0b24d81
3 changed files with 58 additions and 28 deletions
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;
@@ -6,7 +7,7 @@ public class ContainerItem : MonoBehaviour
{
[SerializeField]
private ContainerSO _containerSO;
public event EventHandler OnItemsChange;
private List<BaseItemSO> _items = new List<BaseItemSO>();
@@ -21,12 +22,27 @@ public class ContainerItem : MonoBehaviour
{
Debug.Log($"Player put to container a {item.ItemName}");
_items.Add(item);
OnItemsChange?.Invoke(this,EventArgs.Empty);
}
else
{
Debug.Log("Container is full");
}
}
public void Remove(BaseItemSO item)
{
if (_items.Count>0)
{
Debug.Log($"Player remove {item.ItemName} from container");
_items.Remove(item);
OnItemsChange?.Invoke(this, EventArgs.Empty);
}
else
{
Debug.Log("Container is emplty");
}
}
public List<BaseItemSO> GetItems()
{
+12 -5
View File
@@ -1,9 +1,11 @@
using Assets.Scripts.Interfaces;
using System;
using System.Collections.Generic;
using UnityEngine;
public class Player : BaseCharacter
{
public event EventHandler<bool> OnContainerChanged;
public static Player Instance { get; private set; }
[SerializeField]
@@ -118,16 +120,21 @@ public class Player : BaseCharacter
containerItem.transform.parent = _holdPoint;
containerItem.transform.localPosition = Vector3.zero;
_containerItem = containerItem;
OnContainerChanged.Invoke(this,true);
}
public void ClearContainerItem()
{
OnContainerChanged.Invoke(this, false);
Destroy(_containerItem.gameObject);
_containerItem = null;
}
public ContainerItem GetContainerItem()
{
return _containerItem;
}
public void ClearContainerItem()
{
Destroy(_containerItem.gameObject);
_containerItem = null;
}
public bool IsHoldContainerItem()
{
return _containerItem != null;
+29 -22
View File
@@ -10,40 +10,47 @@ public class ContainerItemsUI : MonoBehaviour
[SerializeField]
private Transform _itemUITemplate;
private ContainerItem _playerContainer;
private void Awake()
{
_container.gameObject.SetActive(false);
}
private void Update()
private void Start()
{
if (Player.Instance.IsHoldContainerItem())
Player.Instance.OnContainerChanged += OnContainerChanged;
}
private void OnContainerChanged(object sender, bool e)
{
if (e)
{
var playerContainer = Player.Instance.GetContainerItem();
_container.gameObject.SetActive(true);
if (playerContainer.IsSalebleItems())
{
ClearObject();
var playerItemsList = playerContainer.GetItems();
for (int count = 0; count < playerItemsList.Count; count++)
{
var itemUI = Instantiate(_itemUITemplate, _itemsList);
itemUI.gameObject.SetActive(true);
itemUI.GetComponent<ItemDescriptionUI>().SetItem((SellableItemSO)playerItemsList[count]);
}
}
else
{
ClearObject();
_container.gameObject.SetActive(false);
}
_playerContainer = Player.Instance.GetContainerItem();
_playerContainer.OnItemsChange += OnItemsChange;
}
else
{
ClearObject();
_container.gameObject.SetActive(false);
_playerContainer.OnItemsChange -= OnItemsChange;
_playerContainer = null;
}
}
private void OnItemsChange(object sender, System.EventArgs e)
{
if (_playerContainer.IsSalebleItems())
{
ClearObject();
var playerItemsList = _playerContainer.GetItems();
for (int count = 0; count < playerItemsList.Count; count++)
{
var itemUI = Instantiate(_itemUITemplate, _itemsList);
itemUI.gameObject.SetActive(true);
itemUI.GetComponent<ItemDescriptionUI>().SetItem((SellableItemSO)playerItemsList[count]);
}
}
}