150 lines
4.6 KiB
C#
150 lines
4.6 KiB
C#
using System.Collections.Generic;
|
|
using Runtime;
|
|
using UnityEngine.UI;
|
|
using TMPro;
|
|
using UnityEngine;
|
|
using ZGame;
|
|
|
|
namespace HK
|
|
{
|
|
public class ShoppingCartUI : UIBase
|
|
{
|
|
[SerializeField] private UpMenuItem goUpMenu;
|
|
[SerializeField] private TMP_Text txtQuantity;
|
|
[SerializeField] private TMP_Text txtSubtotal;
|
|
[SerializeField] private TMP_Text txtDiscount;
|
|
[SerializeField] private TMP_Text txtAmount;
|
|
[SerializeField] private Button btnConfirm;
|
|
[SerializeField] ScrollRect scrollRect;
|
|
[SerializeField] GameObject commodityItem;
|
|
List<CommodityItem> items = new List<CommodityItem>();
|
|
|
|
public override void OnInit()
|
|
{
|
|
base.OnInit();
|
|
|
|
#region AutoGen_Init
|
|
|
|
goUpMenu = GetValue<UpMenuItem>("goUpMenu");
|
|
txtQuantity = GetValue<TMP_Text>("txtQuantity");
|
|
txtSubtotal = GetValue<TMP_Text>("txtSubtotal");
|
|
txtDiscount = GetValue<TMP_Text>("txtDiscount");
|
|
txtAmount = GetValue<TMP_Text>("txtAmount");
|
|
btnConfirm = GetValue<Button>("btnConfirm");
|
|
scrollRect = GetValue<ScrollRect>("scrollRect");
|
|
|
|
btnConfirm.onClick.AddListener(OnClickbtnConfirm);
|
|
|
|
#endregion
|
|
|
|
goUpMenu.OnClickReturn += ClickReturn;
|
|
EventManager.Instance.Subscribe(AddCommodityToShoppingCartEventArgs.EventId,
|
|
AddCommodityToShoppingCartEvent);
|
|
EventManager.Instance.Subscribe(ClearShoppingCartEventArgs.EventId, ClearShoppingCartEvent);
|
|
|
|
commodityItem = ResourcesManager.Instance.Load<GameObject>(AssetConst
|
|
.Assets_Res_Prefab_UI_Item_CommodityItem_prefab);
|
|
}
|
|
|
|
private void ClearShoppingCartEvent(object sender, GameEventArgs e)
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
GameObject.Destroy(item.gameObject);
|
|
}
|
|
|
|
items.Clear();
|
|
}
|
|
|
|
private void AddCommodityToShoppingCartEvent(object sender, GameEventArgs e)
|
|
{
|
|
var args = e as AddCommodityToShoppingCartEventArgs;
|
|
}
|
|
|
|
private void ClickReturn()
|
|
{
|
|
UIManager.Instance.BackLast();
|
|
}
|
|
|
|
public override void OnOpen()
|
|
{
|
|
base.OnOpen();
|
|
goUpMenu.OnShow();
|
|
UpdateShoppingCart();
|
|
}
|
|
|
|
void UpdateShoppingCart()
|
|
{
|
|
foreach (var item in items)
|
|
{
|
|
GameObject.Destroy(item.gameObject);
|
|
}
|
|
|
|
items.Clear();
|
|
|
|
var shoppingCart = ShoppingCartManager.Instance.ShoppingCart;
|
|
|
|
foreach (var cartData in shoppingCart.datas)
|
|
{
|
|
Gen(cartData);
|
|
}
|
|
|
|
txtQuantity.text = $"{shoppingCart.totalQuantity}";
|
|
txtSubtotal.text = $"{shoppingCart.itemsSubTotal}";
|
|
txtDiscount.text = $"{shoppingCart.discount}";
|
|
txtAmount.text = $"{shoppingCart.totalAmount}";
|
|
}
|
|
|
|
void OnlyUpdateMsg()
|
|
{
|
|
var shoppingCart = ShoppingCartManager.Instance.ShoppingCart;
|
|
|
|
txtQuantity.text = $"{shoppingCart.totalQuantity}";
|
|
txtSubtotal.text = $"{shoppingCart.itemsSubTotal}";
|
|
txtDiscount.text = $"{shoppingCart.discount}";
|
|
txtAmount.text = $"{shoppingCart.totalAmount}";
|
|
}
|
|
|
|
void Gen(ShoppingCartData shoppingCartData)
|
|
{
|
|
var go = GameObject.Instantiate(commodityItem, scrollRect.content);
|
|
var item = go.GetComponent<CommodityItem>();
|
|
item.SetData(shoppingCartData);
|
|
item.OnUpdateCallback += OnlyUpdateMsg;
|
|
go.SetActive(true);
|
|
items.Add(item);
|
|
}
|
|
|
|
#region AutoGen_Method
|
|
|
|
private void OnClickbtnConfirm()
|
|
{
|
|
UIManager.Instance.ShowUIOnly(nameof(SubmitMessageUI));
|
|
}
|
|
|
|
#endregion
|
|
|
|
public override void OnDispose()
|
|
{
|
|
base.OnDispose();
|
|
goUpMenu.OnClickReturn -= ClickReturn;
|
|
|
|
EventManager.Instance.Unsubscribe(AddCommodityToShoppingCartEventArgs.EventId,
|
|
AddCommodityToShoppingCartEvent);
|
|
EventManager.Instance.Unsubscribe(ClearShoppingCartEventArgs.EventId, ClearShoppingCartEvent);
|
|
|
|
#region AutoGen_Dispose
|
|
|
|
btnConfirm.onClick.RemoveListener(OnClickbtnConfirm);
|
|
|
|
goUpMenu = null;
|
|
txtQuantity = null;
|
|
txtSubtotal = null;
|
|
txtDiscount = null;
|
|
txtAmount = null;
|
|
btnConfirm = null;
|
|
|
|
#endregion
|
|
}
|
|
}
|
|
} |