添加商店 UI

This commit is contained in:
2026-03-07 22:50:15 +08:00
parent 2b86582ff7
commit c04c049c6a
35 changed files with 3422 additions and 10 deletions
@@ -0,0 +1,136 @@
using GeometryTD.CustomEvent;
using TMPro;
using UnityEngine;
namespace GeometryTD.UI
{
public class GoodsItem : MonoBehaviour
{
[SerializeField] private IconArea _iconArea;
[SerializeField] private TMP_Text _titleText;
[SerializeField] private TMP_Text _typeText;
[SerializeField] private TMP_Text _descriptionText;
[SerializeField] private Transform _tagsParent;
[SerializeField] private TMP_Text _purchaseButtonText;
[SerializeField] private GameObject _tagItemPrefab;
[SerializeField] private int _index;
private GoodsItemContext _context;
public void OnInit(GoodsItemContext context)
{
_context = context;
_index = context?.GoodsIndex ?? -1;
_iconArea?.OnInit(context?.IconAreaContext ?? new IconAreaContext());
if (_titleText != null)
{
_titleText.text = context?.Title ?? string.Empty;
}
if (_typeText != null)
{
_typeText.text = context?.TypeText ?? string.Empty;
}
if (_descriptionText != null)
{
_descriptionText.text = context?.Description ?? string.Empty;
}
if (_purchaseButtonText != null)
{
_purchaseButtonText.text = context?.PurchaseButtonText ?? string.Empty;
}
RefreshTags(context?.TagTexts);
}
public void OnReset()
{
_context = null;
_index = -1;
_iconArea?.OnReset();
if (_titleText != null)
{
_titleText.text = string.Empty;
}
if (_typeText != null)
{
_typeText.text = string.Empty;
}
if (_descriptionText != null)
{
_descriptionText.text = string.Empty;
}
if (_purchaseButtonText != null)
{
_purchaseButtonText.text = string.Empty;
}
ClearTags();
}
public void OnPurchaseButtonClick()
{
if (_context == null || !_context.CanPurchase || _index < 0)
{
return;
}
GameEntry.Event.Fire(this, ShopPurchaseRequestedEventArgs.Create(_index));
}
private void RefreshTags(string[] tagTexts)
{
ClearTags();
if (_tagsParent == null || _tagItemPrefab == null || tagTexts == null)
{
return;
}
for (int i = 0; i < tagTexts.Length; i++)
{
if (string.IsNullOrEmpty(tagTexts[i]))
{
continue;
}
GameObject tagItemObject = Instantiate(_tagItemPrefab, _tagsParent);
TagItem tagItem = tagItemObject.GetComponent<TagItem>();
if (tagItem != null)
{
tagItem.OnInit(new TagItemContext
{
TagName = tagTexts[i]
});
}
}
}
private void ClearTags()
{
if (_tagsParent == null)
{
return;
}
for (int i = _tagsParent.childCount - 1; i >= 0; i--)
{
Destroy(_tagsParent.GetChild(i).gameObject);
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f1bbd7ef0a6648a45b413b62d3fcea48
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
@@ -0,0 +1,94 @@
using GeometryTD.CustomEvent;
using TMPro;
using UnityEngine;
using UnityGameFramework.Runtime;
namespace GeometryTD.UI
{
public class ShopForm : UGuiForm
{
[SerializeField] private TMP_Text _goldText;
[SerializeField] private GoodsItem[] _goldItems;
private ShopFormContext _context;
public void RefreshUI(ShopFormContext context)
{
_context = context;
if (_goldText != null)
{
_goldText.text = context?.GoldText ?? string.Empty;
}
RefreshGoodsItems(context?.GoodsItems);
}
public void OnInventoryButtonClick()
{
GameEntry.Event.Fire(this, ShopInventoryRequestedEventArgs.Create());
}
public void OnExitButtonClick()
{
GameEntry.Event.Fire(this, ShopExitRequestedEventArgs.Create());
}
protected override void OnOpen(object userData)
{
base.OnOpen(userData);
if (userData is ShopFormContext context)
{
RefreshUI(context);
return;
}
Log.Warning("ShopForm requires ShopFormContext as userData.");
}
protected override void OnClose(bool isShutdown, object userData)
{
_context = null;
if (_goldText != null)
{
_goldText.text = string.Empty;
}
if (_goldItems != null)
{
foreach (var item in _goldItems)
{
item?.OnReset();
}
}
base.OnClose(isShutdown, userData);
}
private void RefreshGoodsItems(GoodsItemContext[] goodsItems)
{
if (_goldItems == null)
{
return;
}
for (int i = 0; i < _goldItems.Length; i++)
{
GoodsItem goodsItem = _goldItems[i];
if (goodsItem == null)
{
continue;
}
if (goodsItems != null && i < goodsItems.Length && goodsItems[i] != null)
{
goodsItem.OnInit(goodsItems[i]);
}
else
{
goodsItem.OnReset();
}
}
}
}
}
@@ -0,0 +1,11 @@
fileFormatVersion: 2
guid: f6dd3b528f5963a4db79ed9f9ffea2ed
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant: