No Description
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

SerieLabelPool.cs 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. using System.Collections.Generic;
  2. using UnityEngine;
  3. namespace XCharts.Runtime
  4. {
  5. public static class SerieLabelPool
  6. {
  7. private static readonly Stack<GameObject> m_Stack = new Stack<GameObject>(200);
  8. private static Dictionary<int, bool> m_ReleaseDic = new Dictionary<int, bool>(1000);
  9. public static GameObject Get(string name, Transform parent, LabelStyle label, Color color,
  10. float iconWidth, float iconHeight, ThemeStyle theme)
  11. {
  12. GameObject element;
  13. if (m_Stack.Count == 0 || !Application.isPlaying)
  14. {
  15. element = CreateSerieLabel(name, parent, label, color, iconWidth, iconHeight, theme);
  16. }
  17. else
  18. {
  19. element = m_Stack.Pop();
  20. if (element == null)
  21. {
  22. element = CreateSerieLabel(name, parent, label, color, iconWidth, iconHeight, theme);
  23. }
  24. m_ReleaseDic.Remove(element.GetInstanceID());
  25. element.name = name;
  26. element.transform.SetParent(parent);
  27. var text = new ChartText(element);
  28. text.SetColor(color);
  29. text.SetFontAndSizeAndStyle(label.textStyle, theme.common);
  30. ChartHelper.SetActive(element, true);
  31. }
  32. element.transform.localEulerAngles = new Vector3(0, 0, label.rotate);
  33. return element;
  34. }
  35. public static void Release(GameObject element)
  36. {
  37. if (element == null) return;
  38. ChartHelper.SetActive(element, false);
  39. if (!Application.isPlaying) return;
  40. if (!m_ReleaseDic.ContainsKey(element.GetInstanceID()))
  41. {
  42. m_Stack.Push(element);
  43. m_ReleaseDic.Add(element.GetInstanceID(), true);
  44. }
  45. }
  46. public static void ReleaseAll(Transform parent)
  47. {
  48. int count = parent.childCount;
  49. for (int i = 0; i < count; i++)
  50. {
  51. Release(parent.GetChild(i).gameObject);
  52. }
  53. }
  54. public static void ClearAll()
  55. {
  56. m_Stack.Clear();
  57. m_ReleaseDic.Clear();
  58. }
  59. private static GameObject CreateSerieLabel(string name, Transform parent, LabelStyle labelStyle, Color color,
  60. float iconWidth, float iconHeight, ThemeStyle theme)
  61. {
  62. var label = ChartHelper.AddChartLabel(name, parent, labelStyle, theme.common,
  63. "", color, TextAnchor.MiddleCenter);
  64. label.SetActive(labelStyle.show);
  65. return label.gameObject;
  66. }
  67. }
  68. }