-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathAreaUserCounter.cs
87 lines (69 loc) · 2.3 KB
/
AreaUserCounter.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
83
84
85
86
87
using UdonSharp;
using UnityEngine;
using UnityEngine.UI;
using VRC.SDKBase;
using VRC.Udon;
[UdonBehaviourSyncMode(BehaviourSyncMode.None)]
public class AreaUserCounter : UdonSharpBehaviour
{
[Header("Trigger region user counter")]
private GameObject trigger;
[Tooltip("The UI text field that will display the amount of users")]
public Text textField;
[Tooltip("How often you want the counter to recount the users in the trigger area")]
public float recheckInterval = 100;
private bool _resetting;
private float startY;
private int UserCount;
void Start()
{
trigger = this.gameObject;
startY = trigger.transform.position.y;
_resetting = false;
SendCustomEventDelayedSeconds("MoveAreaAreaDown", recheckInterval, VRC.Udon.Common.Enums.EventTiming.Update);
}
public void MoveAreaAreaDown()
{
_resetting = true;
//Move trigger low into the void
var pos = trigger.transform.position;
pos.y = startY - 100;
trigger.transform.position = pos;
SendCustomEventDelayedFrames("ResetArea", 5, VRC.Udon.Common.Enums.EventTiming.Update);
}
public void ResetArea()
{
// Make sure counter reads zero
UserCount = 0;
_resetting = false;
// Move the trigger pack into position, which will trigger on enter events for all the users in the region
var pos = trigger.transform.position;
pos.y = startY;
trigger.transform.position = pos;
UpdateCounter();
SendCustomEventDelayedSeconds("MoveAreaAreaDown", recheckInterval, VRC.Udon.Common.Enums.EventTiming.Update);
}
private void UpdateCounter()
{
if (textField.text == UserCount.ToString()) return;
//Debug.Log("Updating Text Field");
textField.text = UserCount.ToString();
}
public override void OnPlayerTriggerEnter(VRCPlayerApi player)
{
//Debug.Log("OnPlayerTriggerEnter triggered");
UserCount++;
UpdateCounter();
}
public override void OnPlayerTriggerExit(VRCPlayerApi player)
{
if (_resetting) return;
//Debug.Log("OnPlayerTriggerExit triggered");
UserCount--;
UpdateCounter();
}
public override void OnDeserialization()
{
UpdateCounter();
}
}