ResponsiveEnable.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. namespace SRF.UI
  2. {
  3. using System;
  4. using Internal;
  5. using UnityEngine;
  6. [ExecuteInEditMode]
  7. [RequireComponent(typeof (RectTransform))]
  8. [AddComponentMenu(ComponentMenuPaths.ResponsiveEnable)]
  9. public class ResponsiveEnable : ResponsiveBase
  10. {
  11. public enum Modes
  12. {
  13. EnableAbove,
  14. EnableBelow
  15. }
  16. public Entry[] Entries = new Entry[0];
  17. protected override void Refresh()
  18. {
  19. var rect = RectTransform.rect;
  20. for (var i = 0; i < Entries.Length; i++)
  21. {
  22. var e = Entries[i];
  23. var enable = true;
  24. switch (e.Mode)
  25. {
  26. case Modes.EnableAbove:
  27. {
  28. if (e.ThresholdHeight > 0)
  29. {
  30. enable = rect.height >= e.ThresholdHeight && enable;
  31. }
  32. if (e.ThresholdWidth > 0)
  33. {
  34. enable = rect.width >= e.ThresholdWidth && enable;
  35. }
  36. break;
  37. }
  38. case Modes.EnableBelow:
  39. {
  40. if (e.ThresholdHeight > 0)
  41. {
  42. enable = rect.height <= e.ThresholdHeight && enable;
  43. }
  44. if (e.ThresholdWidth > 0)
  45. {
  46. enable = rect.width <= e.ThresholdWidth && enable;
  47. }
  48. break;
  49. }
  50. default:
  51. throw new IndexOutOfRangeException();
  52. }
  53. if (e.GameObjects != null)
  54. {
  55. for (var j = 0; j < e.GameObjects.Length; j++)
  56. {
  57. var go = e.GameObjects[j];
  58. if (go != null)
  59. {
  60. go.SetActive(enable);
  61. }
  62. }
  63. }
  64. if (e.Components != null)
  65. {
  66. for (var j = 0; j < e.Components.Length; j++)
  67. {
  68. var go = e.Components[j];
  69. if (go != null)
  70. {
  71. go.enabled = enable;
  72. }
  73. }
  74. }
  75. }
  76. }
  77. [Serializable]
  78. public struct Entry
  79. {
  80. public Behaviour[] Components;
  81. public GameObject[] GameObjects;
  82. public Modes Mode;
  83. public float ThresholdHeight;
  84. public float ThresholdWidth;
  85. }
  86. }
  87. }