ResponsiveEnable.cs 2.0 KB

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