StyleRoot.cs 1.3 KB

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