暫無描述
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.

UIComponent.cs 2.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. using System;
  2. using UnityEngine;
  3. using UnityEngine.UI;
  4. namespace XCharts.Runtime
  5. {
  6. /// <summary>
  7. /// UI组件基类。
  8. /// </summary>
  9. [ExecuteInEditMode]
  10. [RequireComponent(typeof(RectTransform))]
  11. [DisallowMultipleComponent]
  12. public class UIComponent : BaseGraph
  13. {
  14. [SerializeField] private bool m_DebugModel = false;
  15. [SerializeField] protected UIComponentTheme m_Theme = new UIComponentTheme();
  16. [SerializeField] private ImageStyle m_Background = new ImageStyle() { show = false };
  17. protected bool m_DataDirty;
  18. public override HideFlags chartHideFlags { get { return m_DebugModel ? HideFlags.None : HideFlags.HideInHierarchy; } }
  19. public UIComponentTheme theme { get { return m_Theme; } set { m_Theme = value; } }
  20. /// <summary>
  21. /// 背景样式。
  22. /// </summary>
  23. public ImageStyle background { get { return m_Background; } set { m_Background = value; color = Color.white; } }
  24. /// <summary>
  25. /// Update chart theme.
  26. /// |切换内置主题。
  27. /// </summary>
  28. /// <param name="theme">theme</param>
  29. public bool UpdateTheme(ThemeType theme)
  30. {
  31. if (theme == ThemeType.Custom)
  32. {
  33. Debug.LogError("UpdateTheme: not support switch to Custom theme.");
  34. return false;
  35. }
  36. if (m_Theme.sharedTheme == null)
  37. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  38. m_Theme.sharedTheme.CopyTheme(theme);
  39. m_Theme.SetAllDirty();
  40. return true;
  41. }
  42. protected override void InitComponent()
  43. {
  44. base.InitComponent();
  45. if (m_Theme.sharedTheme == null)
  46. m_Theme.sharedTheme = XCThemeMgr.GetTheme(ThemeType.Default);
  47. UIHelper.InitBackground(this);
  48. }
  49. protected override void CheckComponent()
  50. {
  51. base.CheckComponent();
  52. if (m_Theme.anyDirty)
  53. {
  54. if (m_Theme.componentDirty)
  55. {
  56. SetAllComponentDirty();
  57. }
  58. if (m_Theme.vertsDirty) RefreshGraph();
  59. m_Theme.ClearDirty();
  60. }
  61. }
  62. protected override void SetAllComponentDirty()
  63. {
  64. base.SetAllComponentDirty();
  65. InitComponent();
  66. }
  67. protected override void OnDrawPainterBase(VertexHelper vh, Painter painter)
  68. {
  69. vh.Clear();
  70. UIHelper.DrawBackground(vh, this);
  71. }
  72. protected override void Update()
  73. {
  74. base.Update();
  75. if (m_DataDirty)
  76. {
  77. m_DataDirty = false;
  78. DataDirty();
  79. }
  80. }
  81. protected virtual void DataDirty()
  82. {
  83. }
  84. }
  85. }