12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- using FL.Battle.Buffs;
- using System.Collections.Generic;
- using UnityEngine;
- using XGame.Framework.Map;
- namespace FL.Map.UI
- {
- /// <summary>
- /// TODO 临时代码
- /// </summary>
- public class BattleInfoBuffList : MonoBehaviour
- {
- [SerializeField]
- private BattleInfoBuffListItem _template;
- [SerializeField]
- private int _itemLimit = 4;
- private BattleInfoBuffListItem[] _items;
- private void Awake()
- {
- _items = new BattleInfoBuffListItem[_itemLimit];
- }
- private void OnDisable()
- {
- foreach (var item in _items)
- {
- if (item == null)
- break;
- item.gameObject.SetActive(false);
- }
- }
- public void Refresh(List<IBuff> buffs, MapContext context)
- { // item 显示顺序为: 4 2 0 1 3 5
- if (buffs.Count > 1)
- {
- buffs.Sort((a, b) =>
- {
- return b.Layers - a.Layers;
- });
- }
- var showCount = Mathf.Clamp(buffs.Count, 0, _itemLimit);
- for (var i = 0; i < _itemLimit; i++)
- {
- var item = _items[i];
- if (i < showCount)
- { // 需要显示
- var buff = buffs[i];
- if (item == null)
- {
- item = GameObject.Instantiate<BattleInfoBuffListItem>(_template, _template.transform.parent);
- _items[i] = item;
- var size = (item.transform as RectTransform).sizeDelta;
- var sign = i % 2 == 0 ? -1 : 1; // 偶数在左边
- var localPosition = item.transform.localPosition;
- localPosition.x = size.x * (0.5f + i / 2) * sign;
- }
- var loadAsync = context.Asset.LoadAsync<Sprite>(buff.IconName);
- loadAsync.On(_ =>
- {
- item.buffIcon.sprite = loadAsync.Result;
- });
- var layers = buff.Layers;
- item.buffNumTxt.text = layers > 1 ? layers.ToString() : string.Empty;
- item.vfxLayer5.SetActive(layers == buff.LayerLimit);
- item.vfxLayer3.SetActive(layers == 3);
- item.gameObject.SetActive(true);
- }
- else
- { // 隐藏
- if (item == null)
- {
- break;
- }
- item.gameObject.SetActive(false);
- }
- }
- }
- }
- }
|