A game about forced loneliness, made by TACStudios
1using System.Diagnostics;
2using System.Threading;
3using UnityEngine.Profiling;
4
5namespace Unity.VisualScripting
6{
7 public static class ProfilingUtility
8 {
9 static ProfilingUtility()
10 {
11 currentSegment = rootSegment = new ProfiledSegment(null, "Root");
12 }
13
14 private static readonly object @lock = new object();
15
16 public static ProfiledSegment rootSegment { get; private set; }
17 public static ProfiledSegment currentSegment { get; set; }
18
19 [Conditional("ENABLE_PROFILER")]
20 public static void Clear()
21 {
22 currentSegment = rootSegment = new ProfiledSegment(null, "Root");
23 }
24
25 public static ProfilingScope SampleBlock(string name)
26 {
27 return new ProfilingScope(name);
28 }
29
30 [Conditional("ENABLE_PROFILER")]
31 public static void BeginSample(string name)
32 {
33 Monitor.Enter(@lock);
34
35 if (!currentSegment.children.Contains(name))
36 {
37 currentSegment.children.Add(new ProfiledSegment(currentSegment, name));
38 }
39
40 currentSegment = currentSegment.children[name];
41 currentSegment.calls++;
42 currentSegment.stopwatch.Start();
43
44 if (UnityThread.allowsAPI)
45 {
46 Profiler.BeginSample(name);
47 }
48 }
49
50 [Conditional("ENABLE_PROFILER")]
51 public static void EndSample()
52 {
53 currentSegment.stopwatch.Stop();
54
55 if (currentSegment.parent != null)
56 {
57 currentSegment = currentSegment.parent;
58 }
59
60 if (UnityThread.allowsAPI)
61 {
62 Profiler.EndSample();
63 }
64
65 Monitor.Exit(@lock);
66 }
67 }
68}