-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTracker.cs
More file actions
80 lines (60 loc) · 2.08 KB
/
Copy pathTracker.cs
File metadata and controls
80 lines (60 loc) · 2.08 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
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
using UnityEditor;
using UnityEngine;
namespace MapStreaming
{
/// <summary>
/// A component to attach to the camera or player to determine what chunks to show.
/// </summary>
[DisallowMultipleComponent]
[ExecuteAlways]
public class Tracker : MonoBehaviour
{
private const float TICK_RATE = 0.1f;
private static Tracker _instance;
private double _counter;
private void Update()
{
if ((_counter += Time.deltaTime) < TICK_RATE) return;
_counter = 0;
SetTransform(transform);
}
private void OnEnable()
{
if ((_instance ??= this) != this) return;
if (Application.isPlaying) return;
#if UNITY_EDITOR
if (Manager.Config.verbose) Debug.Log("Registering editor callbacks for MapStreaming.Tracker.");
EditorApplication.update -= OnEditorApplicationUpdate;
EditorApplication.update += OnEditorApplicationUpdate;
#endif
}
private void OnDisable()
{
if (Manager.Config.verbose) Debug.Log("Cleaning editor callbacks for MapStreaming.Tracker.");
#if UNITY_EDITOR
EditorApplication.update -= OnEditorApplicationUpdate;
#endif
}
private void OnDestroy()
{
if (Manager.Config.verbose) Debug.Log("Cleaning editor callbacks for MapStreaming.Tracker.");
#if UNITY_EDITOR
EditorApplication.update -= OnEditorApplicationUpdate;
#endif
}
#if UNITY_EDITOR
private void OnEditorApplicationUpdate()
{
if (Mathf.Abs((float)(_counter - EditorApplication.timeSinceStartup)) < TICK_RATE) return;
_counter = EditorApplication.timeSinceStartup;
if (SceneView.lastActiveSceneView is null) return;
if (SceneView.lastActiveSceneView.camera is null) return;
SetTransform(SceneView.lastActiveSceneView.camera.transform);
}
#endif
private void SetTransform(Transform next)
{
if (Manager.Tracker is null) Manager.Tracker = next;
}
}
}