- Home /
Question by
joendigiagentti · Feb 21, 2019 at 07:34 AM ·
c#timescaleinvoke
About timescale and invoke
Hello,
I was wondering about timescale and lets just say you pause the whole game using the Time.timeScale = 0f;
and you want to use invoke
public float levelDelay = 2f; and
Invoke("Name", levelDelay);
to lenghten the scene a bit so it doesn't abrubtly go to the next one after meeting the requirements for win.
So what will happen?
Comment
Invoke runs on scaled Time unfortunately, but you can do this.
public class InvokerClass : $$anonymous$$onoBehaviour
{
public class InvokeFunction
{
public System.Action action;
public float timer;
}
public static InvokerClass Instance
{
get
{
if (instance == null)
{
instance = FindObjectOfType<InvokerClass>();
}
return instance;
}
private set
{
instance = value;
}
}
private static InvokerClass instance;
private List<InvokeFunction> invokes, completed;
void Awake()
{
invokes = new List,InvokeFunction>();
completed = new List,InvokeFunction>();
}
public static void InvokeDelayed(System.Action event, float delay)
{
Instance.invokes.Add(new InvokeFunction() { action = event, timer = delay};
}
void Update()
{
foreach (InvokeFunction invokeFunction in invokes)
{
invokeFunction.timer -=Time.unscaledDeltaTime;
if (invokeFunction.timer <= 0)
{
completed.Add(InvokeFunction);
}
}
if (completed.Count > 0)
{
foreach (InvokeFunction invokeFunction in completed)
{
invokeFunction .Invoke();
invokes.Remove(invokeFunction);
}
completed.Clear();
}
}
}
Put it an empty gameobject in your scene and call out to it like this.
public void $$anonymous$$yPausedFunction
{
Time.scale = 1;
}
void Awake()
{
Time.scale = 0;
InvokerClass.InvokeDelayed($$anonymous$$yPausedFunction, 2.5f);
}
Interesting I will give it a try. Thank you.