DemoPrefabController.cs 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. using UnityEngine;
  2. namespace Assets.ImagyVFX.Scripts.Utils
  3. {
  4. internal sealed class DemoPrefabController : MonoBehaviour
  5. {
  6. public int StartNum = 0;
  7. public GameObject[] Prefabs;
  8. private GameObject _currentInstance;
  9. private int _currentPrefabNum;
  10. public void SelectNextPrefab()
  11. {
  12. if (Prefabs.Length == 0)
  13. return;
  14. _currentPrefabNum++;
  15. if (_currentPrefabNum >= Prefabs.Length)
  16. _currentPrefabNum = 0;
  17. ChangePrefab(_currentPrefabNum);
  18. }
  19. public void SelectePrevPrefab()
  20. {
  21. if (Prefabs.Length == 0)
  22. return;
  23. _currentPrefabNum--;
  24. if (_currentPrefabNum < 0)
  25. _currentPrefabNum = Prefabs.Length - 1;
  26. ChangePrefab(_currentPrefabNum);
  27. }
  28. private void Start()
  29. {
  30. _currentPrefabNum = StartNum;
  31. ChangePrefab(_currentPrefabNum);
  32. }
  33. private void ChangePrefab(int num)
  34. {
  35. if (_currentInstance != null)
  36. Destroy(_currentInstance);
  37. _currentInstance = Instantiate(Prefabs[num]);
  38. _currentInstance.SetActive(true);
  39. }
  40. }
  41. }