forked from Unity-Technologies/UnityCsReference
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaitUntil.cs
More file actions
50 lines (42 loc) · 1.91 KB
/
Copy pathWaitUntil.cs
File metadata and controls
50 lines (42 loc) · 1.91 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
// Unity C# reference source
// Copyright (c) Unity Technologies. For terms of use, see
// https://unity3d.com/legal/licenses/Unity_Reference_Only_License
using System;
using System.Runtime.CompilerServices;
namespace UnityEngine
{
public sealed class WaitUntil : CustomYieldInstruction
{
readonly Func<bool> m_Predicate;
readonly Action m_TimeoutCallback;
readonly WaitTimeoutMode m_TimeoutMode;
readonly double m_MaxExecutionTime = -1;
public override bool keepWaiting
{
get
{
if (m_MaxExecutionTime == -1)
return !m_Predicate();
if (GetTime() > m_MaxExecutionTime)
{
m_TimeoutCallback();
return false;
}
return !m_Predicate();
}
}
public WaitUntil(Func<bool> predicate) { m_Predicate = predicate; }
public WaitUntil(Func<bool> predicate, TimeSpan timeout, Action onTimeout, WaitTimeoutMode timeoutMode = WaitTimeoutMode.Realtime) : this(predicate)
{
if (timeoutMode is WaitTimeoutMode.InGameTime && !Application.isPlaying)
throw new ArgumentException($"{nameof(WaitTimeoutMode.InGameTime)} mode is not supported in Editor in edit mode", nameof(timeoutMode));
if (timeout <= TimeSpan.Zero)
throw new ArgumentException("Timeout must be greater than zero", nameof(timeout));
m_TimeoutCallback = onTimeout ?? throw new ArgumentNullException(nameof(onTimeout), "Timeout callback must be specified");
m_TimeoutMode = timeoutMode;
m_MaxExecutionTime = GetTime() + timeout.TotalSeconds;
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
double GetTime() => m_TimeoutMode is WaitTimeoutMode.InGameTime ? Time.timeAsDouble : Time.realtimeSinceStartupAsDouble;
}
}