迁移剩下的 UI 到 Serialization 方式
This commit is contained in:
@@ -0,0 +1,80 @@
|
||||
using Cysharp.Threading.Tasks;
|
||||
using SepCore.AsyncTask;
|
||||
using SepCore.Event;
|
||||
using UnityEngine;
|
||||
using UnityEngine.UI;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class DisplayItem : MonoBehaviour
|
||||
{
|
||||
[SerializeField] private IconArea _iconArea;
|
||||
|
||||
[SerializeField] private RectTransform _itemRect;
|
||||
|
||||
[SerializeField] private CommonButton _button;
|
||||
|
||||
private DisplayItemContext _context;
|
||||
|
||||
private int _index = -1;
|
||||
|
||||
private bool _listenersBound;
|
||||
|
||||
public async UniTaskVoid OnInit(DisplayItemContext context, int index)
|
||||
{
|
||||
EnsureListeners();
|
||||
|
||||
_context = context;
|
||||
_index = index;
|
||||
|
||||
var sprite = await SpriteLoader.LoadSpriteByKeyAsync(_context.IconAssetName);
|
||||
_iconArea.OnInit(sprite, _context.Rarity);
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
_context = null;
|
||||
_index = -1;
|
||||
|
||||
_iconArea.OnReset();
|
||||
}
|
||||
|
||||
private void EnsureListeners()
|
||||
{
|
||||
if (_listenersBound)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
_listenersBound = true;
|
||||
|
||||
CommonButton button = GetComponent<CommonButton>();
|
||||
button.onClick.AddListener(OnItemInfoLock);
|
||||
button.RegisterPointerEnterAction(OnItemInfoShow);
|
||||
button.RegisterPointerExitAction(OnItemInfoHide);
|
||||
}
|
||||
|
||||
private void OnItemInfoShow()
|
||||
{
|
||||
if (_index < 0) return;
|
||||
|
||||
Rect rect = _itemRect.rect;
|
||||
Vector3 targetPos = _itemRect.TransformPoint(new Vector3(
|
||||
(rect.xMin + rect.xMax) / 2,
|
||||
rect.yMax,
|
||||
0f)
|
||||
);
|
||||
GameEntry.Event.Fire(this, DisplayItemHoverEventArgs.Create(_index, _context.IsWeapon, targetPos));
|
||||
}
|
||||
|
||||
private void OnItemInfoLock()
|
||||
{
|
||||
GameEntry.Event.Fire(this, DisplayItemLockEventArgs.Create());
|
||||
}
|
||||
|
||||
private void OnItemInfoHide()
|
||||
{
|
||||
GameEntry.Event.Fire(this, DisplayItemHideEventArgs.Create());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7fe0ad0e96348114786974bb02d62914
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,25 @@
|
||||
using SepCore.Definition;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class DisplayItemContext : UIContext
|
||||
{
|
||||
public string IconAssetName;
|
||||
public Rarity Rarity;
|
||||
public bool IsWeapon;
|
||||
|
||||
public DisplayItemContext()
|
||||
{
|
||||
IconAssetName = string.Empty;
|
||||
Rarity = Rarity.None;
|
||||
IsWeapon = false;
|
||||
}
|
||||
|
||||
public DisplayItemContext(DisplayItemRawData rawData)
|
||||
{
|
||||
this.IconAssetName = rawData.IconAssetName;
|
||||
this.Rarity = rawData.Rarity;
|
||||
this.IsWeapon = rawData.IsWeapon;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: df1bccc6ff70fbc4089c7809e35c1f61
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,9 @@
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public class DisplayListContext : UIContext
|
||||
{
|
||||
public int CurrentCount;
|
||||
public int MaxCount = -1;
|
||||
public DisplayItemContext[] ItemContexts;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: a10dfda6cc0f45247bfcdbfb578110bc
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,51 @@
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
public partial class DisplayListForm
|
||||
{
|
||||
private readonly List<DisplayItem> _displayItems = new List<DisplayItem>();
|
||||
|
||||
public void OnInit(DisplayListContext context)
|
||||
{
|
||||
View.titleFormatText.Set(context.CurrentCount, context.MaxCount);
|
||||
|
||||
int targetCount = Mathf.Min(context.CurrentCount, context.ItemContexts.Length);
|
||||
EnsureItemCount(targetCount);
|
||||
|
||||
for (int i = 0; i < _displayItems.Count; i++)
|
||||
{
|
||||
DisplayItem item = _displayItems[i];
|
||||
if (i < targetCount)
|
||||
{
|
||||
item.OnInit(context.ItemContexts[i], i).Forget();
|
||||
item.gameObject.SetActive(true);
|
||||
}
|
||||
else
|
||||
{
|
||||
item.OnReset();
|
||||
item.gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void OnReset()
|
||||
{
|
||||
for (int i = 0; i < _displayItems.Count; i++)
|
||||
{
|
||||
_displayItems[i].OnReset();
|
||||
_displayItems[i].gameObject.SetActive(false);
|
||||
}
|
||||
}
|
||||
|
||||
private void EnsureItemCount(int count)
|
||||
{
|
||||
while (_displayItems.Count < count)
|
||||
{
|
||||
DisplayItem item = Instantiate(View.displayItemTemplate, View.listRoot.transform);
|
||||
_displayItems.Add(item);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 58c6ff306e4be41458bdf4dd82ad92e1
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
// <auto-generated />
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public partial class DisplayListForm : UGuiForm
|
||||
{
|
||||
[SerializeField] private DisplayListView _view = null;
|
||||
|
||||
public DisplayListView View => _view;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 220e0137e2c60eb4084e50234225dd50
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
@@ -0,0 +1,13 @@
|
||||
// <auto-generated />
|
||||
using UnityEngine;
|
||||
|
||||
namespace SepCore.UI
|
||||
{
|
||||
[DisallowMultipleComponent]
|
||||
public partial class DisplayListView : MonoBehaviour
|
||||
{
|
||||
[SerializeField] public global::SepCore.UI.FormatTextUI titleFormatText;
|
||||
[SerializeField] public global::UnityEngine.UI.GridLayoutGroup listRoot;
|
||||
[SerializeField] public global::SepCore.UI.DisplayItem displayItemTemplate;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
fileFormatVersion: 2
|
||||
guid: d9eb9bf8c92339346b767aa476f14f94
|
||||
MonoImporter:
|
||||
externalObjects: {}
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
||||
Reference in New Issue
Block a user