123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154 |
- using System;
- using System.Collections.Generic;
- using UnityEngine;
- namespace WS
- {
- [ObjectSystem]
- public class UITabChoiceUpdateSystem : UpdateSystem<UITabChoice>
- {
- public override void Update(UITabChoice self)
- {
- self.Update();
- }
- }
-
- public class UITabChoice : WSComponent
- {
-
- private List<GameObject> UIObjects;
-
- private UnityEngine.EventSystems.EventSystem system;
-
- private Action OnCallback;
-
- private bool IsArbitrary;
-
- private bool IsReverseOrder;
-
-
-
-
-
- public void Init(Action enterkeycallback, bool isarbitrary, bool isreverseorder, params GameObject[] uiobjs)
- {
- system = UnityEngine.EventSystems.EventSystem.current;
- if (UIObjects == null)
- UIObjects = new List<GameObject>();
- UIObjects.AddRange(uiobjs);
- OnCallback = enterkeycallback;
- IsArbitrary = isarbitrary;
- IsReverseOrder = isreverseorder;
- if (UIObjects.Count > 0)
- system.SetSelectedGameObject(UIObjects[0]);
- }
- public void Update()
- {
- if (UIObjects != null)
- {
- if (Input.GetKeyDown(KeyCode.Tab))
- {
- SwitchCurrent();
- }
- if (Input.GetKeyDown(KeyCode.Return) || Input.GetKeyDown(KeyCode.KeypadEnter))
- {
- if (IsArbitrary)
- {
- OnCallback?.Invoke();
- }
- if (UIObjects.Count >= 2)
- {
- int index = IndexNow(system.currentSelectedGameObject);
- if (IsReverseOrder)
- {
- if (index == 1)
- OnCallback?.Invoke();
- }
- else
- {
- if (index == UIObjects.Count - 2)
- OnCallback?.Invoke();
- }
- }
- }
- }
- }
-
- private void SwitchCurrent()
- {
- if (UIObjects.Contains(system.currentSelectedGameObject))
- {
- if (IsReverseOrder)
- {
-
- GameObject last = LastInput(system.currentSelectedGameObject);
- system.SetSelectedGameObject(last);
- }
- else
- {
-
- GameObject next = NextInput(system.currentSelectedGameObject);
- system.SetSelectedGameObject(next);
- }
- }
- }
-
- private GameObject LastInput(GameObject input)
- {
- int indexNow = IndexNow(input);
- if (indexNow - 1 >= 0)
- {
- return UIObjects[indexNow - 1].gameObject;
- }
- else
- {
- return UIObjects[UIObjects.Count - 1].gameObject;
- }
- }
-
- private int IndexNow(GameObject input)
- {
- int indexNow = 0;
- for (int i = 0; i < UIObjects.Count; i++)
- {
- if (input == UIObjects[i])
- {
- indexNow = i;
- break;
- }
- }
- return indexNow;
- }
-
- private GameObject NextInput(GameObject input)
- {
- int indexNow = IndexNow(input);
- if (indexNow + 1 < UIObjects.Count)
- {
- return UIObjects[indexNow + 1].gameObject;
- }
- else
- {
- return UIObjects[0].gameObject;
- }
- }
- public override void Dispose()
- {
- base.Dispose();
- UIObjects.Clear();
- OnCallback = null;
- IsArbitrary = false;
- IsReverseOrder = false;
- }
- }
- }
|