ObjectsSwitcher.cs 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.Events;
  5. namespace Sveta
  6. {
  7. [System.Serializable]
  8. public class UnityEventString : UnityEvent<string>
  9. {
  10. }
  11. public class ObjectsSwitcher : MonoBehaviour
  12. {
  13. public UnityEventString outputName;
  14. public List<GameObject> list;
  15. private int index = 0;
  16. public void Switch(int delta)
  17. {
  18. index += delta;
  19. if (index > list.Count - 1)
  20. {
  21. index = 0;
  22. }
  23. if (index < 0)
  24. {
  25. index = list.Count - 1;
  26. }
  27. SwitchTo(index);
  28. }
  29. private void SwitchTo(int _index) {
  30. for (int i = 0; i < list.Count; i++)
  31. {
  32. list[i].SetActive(i == _index);
  33. }
  34. outputName?.Invoke(list[_index].name);
  35. }
  36. public void Awake()
  37. {
  38. /*
  39. for (int i = 0; i < list.Count; i++)
  40. {
  41. list[i].gameObject.SetActive(false);
  42. }
  43. list[0].SetActive(true);
  44. */
  45. SwitchTo(0);
  46. }
  47. }
  48. }