ResponsiveBase.cs 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. namespace SRF.UI
  2. {
  3. using UnityEngine;
  4. [ExecuteInEditMode]
  5. [RequireComponent(typeof (RectTransform))]
  6. public abstract class ResponsiveBase : SRMonoBehaviour
  7. {
  8. private bool _queueRefresh;
  9. protected RectTransform RectTransform
  10. {
  11. get { return (RectTransform) CachedTransform; }
  12. }
  13. protected void OnEnable()
  14. {
  15. _queueRefresh = true;
  16. }
  17. protected void OnRectTransformDimensionsChange()
  18. {
  19. _queueRefresh = true;
  20. }
  21. protected void Update()
  22. {
  23. #if UNITY_EDITOR
  24. // Refresh whenever we can in the editor, since layout has quirky update behaviour
  25. // when not in play mode
  26. if (!Application.isPlaying)
  27. {
  28. Refresh();
  29. return;
  30. }
  31. #endif
  32. if (_queueRefresh)
  33. {
  34. Refresh();
  35. _queueRefresh = false;
  36. }
  37. }
  38. protected abstract void Refresh();
  39. [ContextMenu("Refresh")]
  40. private void DoRefresh()
  41. {
  42. Refresh();
  43. }
  44. }
  45. }