StyleRoot.cs 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. using System;
  2. using UnityEngine;
  3. namespace SRF.UI
  4. {
  5. [ExecuteInEditMode]
  6. [AddComponentMenu("SRF/UI/Style Root")]
  7. public sealed class StyleRoot : SRMonoBehaviour
  8. {
  9. public Style GetStyle(string key)
  10. {
  11. if (this.StyleSheet == null)
  12. {
  13. UnityEngine.Debug.LogWarning("[StyleRoot] StyleSheet is not set.", this);
  14. return null;
  15. }
  16. return this.StyleSheet.GetStyle(key, true);
  17. }
  18. private void OnEnable()
  19. {
  20. this._activeStyleSheet = null;
  21. if (this.StyleSheet != null)
  22. {
  23. this.OnStyleSheetChanged();
  24. }
  25. }
  26. private void OnDisable()
  27. {
  28. this.OnStyleSheetChanged();
  29. }
  30. private void Update()
  31. {
  32. if (this._activeStyleSheet != this.StyleSheet)
  33. {
  34. this.OnStyleSheetChanged();
  35. }
  36. }
  37. private void OnStyleSheetChanged()
  38. {
  39. this._activeStyleSheet = this.StyleSheet;
  40. base.BroadcastMessage("SRStyleDirty", SendMessageOptions.DontRequireReceiver);
  41. }
  42. public void SetDirty()
  43. {
  44. this._activeStyleSheet = null;
  45. }
  46. private StyleSheet _activeStyleSheet;
  47. public StyleSheet StyleSheet;
  48. }
  49. }