66 lines
1.8 KiB
C#
66 lines
1.8 KiB
C#
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using UnityEngine;
|
|
using UnityEngine.Events;
|
|
|
|
namespace Beyond
|
|
{
|
|
public class TimeController : AutoSingleton<TimeController>
|
|
{
|
|
|
|
public UnityEvent<float> m_OnTimeScaleChanged;
|
|
|
|
private float m_startingFixedDelta = 0.02f;
|
|
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);
|
|
}
|
|
[Sirenix.OdinInspector.Button]
|
|
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;
|
|
}
|
|
|
|
}
|
|
}
|