BattleInfoBuffList.cs 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  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. internal 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. {
  32. var showCount = Mathf.Clamp(buffs.Count, 0, _itemLimit);
  33. for (var i = 0; i < _itemLimit; i++)
  34. {
  35. var item = _items[i];
  36. if (i < showCount)
  37. { // 需要显示
  38. var buff = buffs[i];
  39. if (item == null)
  40. {
  41. item = GameObject.Instantiate<BattleInfoBuffListItem>(_template, _template.transform.parent);
  42. _items[i] = item;
  43. }
  44. var loadAsync = context.Asset.LoadAsync<Sprite>(buff.IconName);
  45. loadAsync.On(_ =>
  46. {
  47. item.buffIcon.sprite = loadAsync.Result;
  48. });
  49. var layers = buff.Layers;
  50. item.buffNumTxt.text = layers > 1 ? layers.ToString() : string.Empty;
  51. item.gameObject.SetActive(true);
  52. }
  53. else
  54. { // 隐藏
  55. if (item == null)
  56. {
  57. break;
  58. }
  59. item.gameObject.SetActive(false);
  60. }
  61. }
  62. }
  63. }
  64. }