DemoPrefabController.cs 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. using UnityEngine;
  2. namespace MaterializationFX.Scripts.Utils
  3. {
  4. internal sealed class DemoPrefabController : MonoBehaviour
  5. {
  6. public int StartNum;
  7. public GameObject[] Prefabs;
  8. private GameObject _currentInstance;
  9. private int _currentPrefabNum;
  10. public void Next()
  11. {
  12. if (Prefabs.Length == 0)
  13. return;
  14. _currentPrefabNum++;
  15. if (_currentPrefabNum >= Prefabs.Length)
  16. _currentPrefabNum = 0;
  17. ChangePrefab(_currentPrefabNum);
  18. }
  19. private void Start()
  20. {
  21. _currentPrefabNum = StartNum;
  22. ChangePrefab(_currentPrefabNum);
  23. }
  24. private void ChangePrefab(int num)
  25. {
  26. if (_currentInstance != null)
  27. Destroy(_currentInstance);
  28. var newPrefab = Prefabs[num];
  29. _currentInstance = Instantiate(newPrefab, newPrefab.transform.position, newPrefab.transform.transform.rotation);
  30. _currentInstance.SetActive(true);
  31. }
  32. }
  33. }