UITabChoice.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. using System;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace WS
  5. {
  6. [ObjectSystem]
  7. public class UITabChoiceUpdateSystem : UpdateSystem<UITabChoice>
  8. {
  9. public override void Update(UITabChoice self)
  10. {
  11. self.Update();
  12. }
  13. }
  14. ///<summary>UI切换选择组件</summary>
  15. public class UITabChoice : WSComponent
  16. {
  17. ///<summary>UI集合</summary>
  18. private List<GameObject> UIObjects;
  19. ///<summary>系统事件</summary>
  20. private UnityEngine.EventSystems.EventSystem system;
  21. ///<summary>回车键-回调</summary>
  22. private Action OnCallback;
  23. ///<summary>任意时候点击回车键触发回调</summary>
  24. private bool IsArbitrary;
  25. ///<summary>倒序</summary>
  26. private bool IsReverseOrder;
  27. /// <summary>初始化</summary>
  28. /// <param name="enterkeycallback">回车键回调</param>
  29. /// <param name="isarbitrary">任意时候点击回车键触发回调</param>
  30. /// <param name="isreverseorder">是否倒序</param>
  31. /// <param name="uiobjs">UI</param>
  32. public void Init(Action enterkeycallback, bool isarbitrary, bool isreverseorder, params GameObject[] uiobjs)
  33. {
  34. system = UnityEngine.EventSystems.EventSystem.current;
  35. if (UIObjects == null)
  36. UIObjects = new List<GameObject>();
  37. UIObjects.AddRange(uiobjs);
  38. OnCallback = enterkeycallback;
  39. IsArbitrary = isarbitrary;
  40. IsReverseOrder = isreverseorder;
  41. if (UIObjects.Count > 0)
  42. system.SetSelectedGameObject(UIObjects[0]);
  43. }
  44. public void Update()
  45. {
  46. if (UIObjects != null)
  47. {
  48. if (Input.GetKeyDown(KeyCode.Tab))
  49. {
  50. SwitchCurrent();
  51. }
  52. if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
  53. {
  54. if (IsArbitrary)
  55. {
  56. OnCallback?.Invoke();
  57. }
  58. if (UIObjects.Count >= 2)
  59. {
  60. int index = IndexNow(system.currentSelectedGameObject);
  61. if (IsReverseOrder)
  62. {
  63. if (index == 1)
  64. OnCallback?.Invoke();
  65. }
  66. else
  67. {
  68. if (index == UIObjects.Count - 2)
  69. OnCallback?.Invoke();
  70. }
  71. }
  72. }
  73. }
  74. }
  75. ///<summary>切换当前选择UI</summary>
  76. private void SwitchCurrent()
  77. {
  78. if (UIObjects.Contains(system.currentSelectedGameObject))
  79. {
  80. if (IsReverseOrder)
  81. {
  82. //倒序
  83. GameObject last = LastInput(system.currentSelectedGameObject);
  84. system.SetSelectedGameObject(last);
  85. }
  86. else
  87. {
  88. //正序
  89. GameObject next = NextInput(system.currentSelectedGameObject);
  90. system.SetSelectedGameObject(next);
  91. }
  92. }
  93. }
  94. ///<summary>获取上一个物体</summary>
  95. private GameObject LastInput(GameObject input)
  96. {
  97. int indexNow = IndexNow(input);
  98. if (indexNow - 1 >= 0)
  99. {
  100. return UIObjects[indexNow - 1].gameObject;
  101. }
  102. else
  103. {
  104. return UIObjects[UIObjects.Count - 1].gameObject;
  105. }
  106. }
  107. ///<summary>获取当前选中物体的序列</summary>
  108. private int IndexNow(GameObject input)
  109. {
  110. int indexNow = 0;
  111. for (int i = 0; i < UIObjects.Count; i++)
  112. {
  113. if (input == UIObjects[i])
  114. {
  115. indexNow = i;
  116. break;
  117. }
  118. }
  119. return indexNow;
  120. }
  121. ///<summary>获取下一个物体</summary>
  122. private GameObject NextInput(GameObject input)
  123. {
  124. int indexNow = IndexNow(input);
  125. if (indexNow + 1 < UIObjects.Count)
  126. {
  127. return UIObjects[indexNow + 1].gameObject;
  128. }
  129. else
  130. {
  131. return UIObjects[0].gameObject;
  132. }
  133. }
  134. public override void Dispose()
  135. {
  136. base.Dispose();
  137. UIObjects.Clear();
  138. OnCallback = null;
  139. IsArbitrary = false;
  140. IsReverseOrder = false;
  141. }
  142. }
  143. }