ResolutionSizer.cs 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using UnityEngine;
  6. using UnityEngine.EventSystems;
  7. using UnityEngine.UI;
  8. namespace TheraBytes.BetterUi
  9. {
  10. #if UNITY_2018_3_OR_NEWER
  11. [ExecuteAlways]
  12. #else
  13. [ExecuteInEditMode]
  14. #endif
  15. public abstract class ResolutionSizer<T> : UIBehaviour, ILayoutController, ILayoutSelfController, IResolutionDependency
  16. {
  17. protected abstract ScreenDependentSize<T> sizer { get; }
  18. public virtual void SetLayoutHorizontal()
  19. {
  20. UpdateSize();
  21. }
  22. public virtual void SetLayoutVertical()
  23. {
  24. UpdateSize();
  25. }
  26. protected override void OnEnable()
  27. {
  28. base.OnEnable();
  29. UpdateSize();
  30. }
  31. #if UNITY_EDITOR
  32. protected override void OnValidate()
  33. {
  34. this.SetDirty();
  35. }
  36. #endif
  37. protected void SetDirty()
  38. {
  39. if (!(this.isActiveAndEnabled))
  40. {
  41. return;
  42. }
  43. this.UpdateSize();
  44. }
  45. protected override void OnRectTransformDimensionsChange()
  46. {
  47. base.OnRectTransformDimensionsChange();
  48. UpdateSize();
  49. }
  50. void UpdateSize()
  51. {
  52. if(!(isActiveAndEnabled))
  53. {
  54. return;
  55. }
  56. T newSize = sizer.CalculateSize(this);
  57. this.ApplySize(newSize);
  58. }
  59. protected abstract void ApplySize(T newSize);
  60. public void OnResolutionChanged()
  61. {
  62. UpdateSize();
  63. }
  64. }
  65. }