using System; using System.Collections; using UnityEngine; public class WorldSwitcher : MonoBehaviour { private const float _SCALE_DURATION = 0.5f; private const float _SCALE_FACTOR = 100.0f; [SerializeField] private GameObject _spriteMask; [SerializeField] private GameObject _world1; [SerializeField] private GameObject _world2; private int _currentWorldIndex = 0; public IEnumerator ScaleOverTime(Transform target, Vector3 from, Vector3 to, float duration, Action onComplete) { var elapsed = 0f; target.localScale = from; while (elapsed < duration) { elapsed += Time.deltaTime; var t = Mathf.Clamp01(elapsed / duration); target.localScale = Vector3.Lerp(from, to, t); yield return null; } target.localScale = to; onComplete?.Invoke(); } private void Start() { _spriteMask.transform.localScale = Vector3.zero; SwitchWorld(0); } private void SwitchWorld(int newIndex) { var currentScale = _spriteMask.transform.localScale; var from = _currentWorldIndex == 0 ? currentScale : new Vector3(_SCALE_FACTOR, _SCALE_FACTOR, 1); var to = newIndex == 0 ? Vector3.zero : new Vector3(_SCALE_FACTOR, _SCALE_FACTOR, 1); StartCoroutine(ScaleOverTime(_spriteMask.transform, from, to, _SCALE_DURATION, () => { _currentWorldIndex = newIndex; _spriteMask.transform.localScale = to; _world1.SetActive(_currentWorldIndex == 0); _world2.SetActive(_currentWorldIndex == 1); })); } public void SwitchNext() { var newIndex = _currentWorldIndex == 0 ? 1 : 0; SwitchWorld(newIndex); } }