Demo.cs 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. using UnityEngine.UI;
  5. namespace Knife.HologramEffect
  6. {
  7. public class Demo : MonoBehaviour
  8. {
  9. [SerializeField] private GameObjectsGroup[] groups;
  10. [SerializeField] private Button previousButton;
  11. [SerializeField] private Button nextButton;
  12. private int currentGroup;
  13. private void Start()
  14. {
  15. currentGroup = 0;
  16. OpenCurrent();
  17. previousButton.onClick.AddListener(Previous);
  18. nextButton.onClick.AddListener(Next);
  19. }
  20. private void Update()
  21. {
  22. if(Input.GetKeyDown(KeyCode.RightArrow))
  23. {
  24. Next();
  25. }
  26. if (Input.GetKeyDown(KeyCode.LeftArrow))
  27. {
  28. Previous();
  29. }
  30. }
  31. private void Next()
  32. {
  33. currentGroup++;
  34. if (currentGroup >= groups.Length)
  35. {
  36. currentGroup = 0;
  37. }
  38. OpenCurrent();
  39. }
  40. private void Previous()
  41. {
  42. currentGroup--;
  43. if (currentGroup < 0)
  44. {
  45. currentGroup = groups.Length - 1;
  46. }
  47. OpenCurrent();
  48. }
  49. private void OpenCurrent()
  50. {
  51. foreach (var g in groups)
  52. {
  53. g.SetActive(false);
  54. }
  55. groups[currentGroup].SetActive(true);
  56. }
  57. [System.Serializable]
  58. private class GameObjectsGroup
  59. {
  60. [SerializeField] private GameObject[] gameObjects;
  61. public void SetActive(bool enabled)
  62. {
  63. foreach(var g in gameObjects)
  64. {
  65. g.SetActive(enabled);
  66. }
  67. }
  68. }
  69. }
  70. }