using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.Events; namespace Beyond { public class TimeController : AutoSingleton { public UnityEvent m_OnTimeScaleChanged; private float m_startingFixedDelta; private float m_currentTimeScale = 1f; public void SetTimeScale(float s) { Time.timeScale = s; Time.fixedDeltaTime = m_startingFixedDelta * s; m_OnTimeScaleChanged?.Invoke(s); } public void SetTimeScaleForSec(float s, float seconds, bool forceStop = false) { if (forceStop) StopAllCoroutines(); StartCoroutine(SetTimeScaleForSecCoroutine(s, seconds)); } public void Reset() { StopAllCoroutines(); SetTimeScale(1f); } public void SetTimeScaleForRealTimeSec(float s, float seconds, bool forceStop = false) { if (forceStop) StopAllCoroutines(); StartCoroutine(SetTimeScaleForRealSecCoroutine(s, seconds)); } private IEnumerator SetTimeScaleForSecCoroutine(float s, float seconds) { SetTimeScale(s); yield return new WaitForSeconds(seconds); SetTimeScale(1f); } private IEnumerator SetTimeScaleForRealSecCoroutine(float s, float seconds) { SetTimeScale(s); yield return new WaitForSecondsRealtime(seconds); SetTimeScale(1f); } protected override void Awake() { base.Awake(); m_startingFixedDelta = Time.fixedDeltaTime; } // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } } }