迁移剩下的 UI 到 Serialization 方式
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
using System;
|
||||
using SepCore.Event;
|
||||
using TMPro;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class ShopGoodsItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private IconArea _iconArea;
|
||||
|
||||
[SerializeField] private TMP_Text _titleText;
|
||||
|
||||
[SerializeField] private TMP_Text _typeText;
|
||||
|
||||
[SerializeField] private TMP_Text _descriptionText;
|
||||
|
||||
[SerializeField] private TMP_Text _costText;
|
||||
|
||||
private int _index = -1;
|
||||
|
||||
private bool _listenersBound;
|
||||
|
||||
public void Init(GoodsItemContext data, int index)
|
||||
{
|
||||
EnsureListeners();
|
||||
|
||||
_index = index;
|
||||
|
||||
_iconArea.OnInit(data.Icon, data.Rarity);
|
||||
_titleText.text = data.Name;
|
||||
_typeText.text = data.ItemType switch
|
||||
{
|
||||
ItemType.None => "error",
|
||||
ItemType.Weapon => "武器",
|
||||
ItemType.Prop => "道具",
|
||||
_ => throw new ArgumentOutOfRangeException()
|
||||
};
|
||||
_descriptionText.text = data.Description;
|
||||
_costText.text = $"{data.Price} <sprite name=\"coin\" index=0>";
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_index = -1;
|
||||
}
|
||||
|
||||
private void EnsureListeners()
|
||||
{
|
||||
if (_listenersBound)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_listenersBound = true;
|
||||
|
||||
CommonButton button = GetComponentInChildren<CommonButton>(true);
|
||||
button.onClick.AddListener(OnPurchaseClick);
|
||||
}
|
||||
|
||||
private void OnPurchaseClick()
|
||||
{
|
||||
if (_index < 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
GameEntry.Event.Fire(this, ShopPurchaseEventArgs.Create(_index));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user