forked from Facepunch/Rust.Community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommunityEntity.UI.Countdown.cs
82 lines (72 loc) · 1.94 KB
/
CommunityEntity.UI.Countdown.cs
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using Facepunch.Extend;
using System.IO;
public partial class CommunityEntity
{
#if CLIENT
private class Countdown : MonoBehaviour
{
public string command = "";
public int endTime = 0;
public int startTime = 0;
public int step = 1;
private int sign = 1;
private string tempText = "";
private UnityEngine.UI.Text textComponent;
void Start()
{
textComponent = GetComponent<UnityEngine.UI.Text>();
if ( textComponent )
{
tempText = textComponent.text;
textComponent.text = tempText.Replace( "%TIME_LEFT%", startTime.ToString() );
}
if ( startTime == endTime )
{
End();
}
if ( step == 0 )
{
step = 1;
}
if ( startTime > endTime && step > 0 )
{
sign = -1;
}
InvokeRepeating( "UpdateCountdown", step, step );
}
void UpdateCountdown()
{
startTime = startTime + step * sign;
if ( textComponent )
{
textComponent.text = tempText.Replace( "%TIME_LEFT%", startTime.ToString() );
}
if ( startTime == endTime )
{
if ( !string.IsNullOrEmpty( command ) )
{
ConsoleNetwork.ClientRunOnServer( command );
}
End();
}
}
void End()
{
CancelInvoke( "UpdateCountdown" );
var fadeOut = GetComponent<FadeOut>();
if ( fadeOut )
{
fadeOut.FadeOutAndDestroy();
}
else
{
Object.Destroy( gameObject );
}
}
}
#endif
}