demoSwitcher.cs 970 B

1234567891011121314151617181920212223242526272829303132333435363738
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. public class demoSwitcher : MonoBehaviour
  5. {
  6. int activeChild = 0;
  7. // Start is called before the first frame update
  8. void Start()
  9. {
  10. List<GameObject> children = GetAllChildren();
  11. activeChild %= children.Count;
  12. for (int i = 0; i < children.Count; i++)
  13. {
  14. children[i].transform.position = children[activeChild].transform.position;
  15. children[i].SetActive(i == activeChild);
  16. }
  17. }
  18. // Update is called once per frame
  19. void Update()
  20. {
  21. if (Input.GetKeyUp(KeyCode.E))
  22. {
  23. ++activeChild;
  24. Start();
  25. }
  26. }
  27. List<GameObject> GetAllChildren()
  28. {
  29. List<GameObject> list = new List<GameObject>();
  30. for (int i = 0; i < transform.childCount; i++)
  31. {
  32. list.Add(transform.GetChild(i).gameObject);
  33. }
  34. return list;
  35. }
  36. }