BattleInfoBuffList.cs 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using FL.Battle.Buffs;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using XGame.Framework.Map;
  5. namespace FL.Map.UI
  6. {
  7. /// <summary>
  8. /// TODO 临时代码
  9. /// </summary>
  10. public class BattleInfoBuffList : MonoBehaviour
  11. {
  12. [SerializeField]
  13. private BattleInfoBuffListItem _template;
  14. [SerializeField]
  15. private int _itemLimit = 4;
  16. private BattleInfoBuffListItem[] _items;
  17. private void Awake()
  18. {
  19. _items = new BattleInfoBuffListItem[_itemLimit];
  20. }
  21. private void OnDisable()
  22. {
  23. foreach (var item in _items)
  24. {
  25. if (item == null)
  26. break;
  27. item.gameObject.SetActive(false);
  28. }
  29. }
  30. public void Refresh(List<IBuff> buffs, MapContext context)
  31. { // item 显示顺序为: 4 2 0 1 3 5
  32. if (buffs.Count > 1)
  33. {
  34. buffs.Sort((a, b) =>
  35. {
  36. return b.Layers - a.Layers;
  37. });
  38. }
  39. var showCount = Mathf.Clamp(buffs.Count, 0, _itemLimit);
  40. for (var i = 0; i < _itemLimit; i++)
  41. {
  42. var item = _items[i];
  43. if (i < showCount)
  44. { // 需要显示
  45. var buff = buffs[i];
  46. if (item == null)
  47. {
  48. item = GameObject.Instantiate<BattleInfoBuffListItem>(_template, _template.transform.parent);
  49. _items[i] = item;
  50. var size = (item.transform as RectTransform).sizeDelta;
  51. var sign = i % 2 == 0 ? -1 : 1; // 偶数在左边
  52. var localPosition = item.transform.localPosition;
  53. localPosition.x = size.x * (0.5f + i / 2) * sign;
  54. }
  55. var loadAsync = context.Asset.LoadAsync<Sprite>(buff.IconName);
  56. loadAsync.On(_ =>
  57. {
  58. item.buffIcon.sprite = loadAsync.Result;
  59. });
  60. var layers = buff.Layers;
  61. item.buffNumTxt.text = layers > 1 ? layers.ToString() : string.Empty;
  62. item.vfxLayer5.SetActive(layers == buff.LayerLimit);
  63. item.vfxLayer3.SetActive(layers == 3);
  64. item.gameObject.SetActive(true);
  65. }
  66. else
  67. { // 隐藏
  68. if (item == null)
  69. {
  70. break;
  71. }
  72. item.gameObject.SetActive(false);
  73. }
  74. }
  75. }
  76. }
  77. }