File size: 1,573 Bytes
35ecd17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
let unityProfiler = (function () {
  let intervalId = 0;
  const storageKey = 'UnityPlayerConnectionAddress';
  let profilerButton;

  return {
    createButton: (unityInstance) => {
      profilerButton = document.createElement('button');
      profilerButton.style = 'background-color: lightgray; border: none; padding: 5px; cursor: pointer';
      profilerButton.innerHTML = unityInstance.IsConnectedToProfiler() ? 'Stop Profiling' : 'Profile';

      const startStatusUpdate = () => {
        if (intervalId === 0)
          intervalId = setInterval(updateProfilingStatus, 1000);
      }

      profilerButton.onclick = () => {
        if (unityInstance.IsConnectedToProfiler()) {
          unityInstance.StopProfiling();
        } else {
          const ipAndPort = prompt(`Please input 'IP:port' of the Unity Profiler.`, localStorage.getItem(storageKey) || '');
          if (ipAndPort) {
            unityInstance.ConnectToProfiler(ipAndPort);
            localStorage.setItem(storageKey, ipAndPort);
            startStatusUpdate();
          }
        }
      };

      const updateProfilingStatus = () => {
        profilerButton.innerHTML = unityInstance.IsConnectedToProfiler() ? 'Stop Profiling' : 'Profile';
      }

      if (unityInstance.IsConnectedToProfiler()) {
        startStatusUpdate();
      }

      return profilerButton;
    },
    shutDown: () => {
      if (profilerButton.parentNode && profilerButton) {
        profilerButton.parentNode.remove(profilerButton);
      }
      clearInterval(intervalId);
      intervalId = 0;
    }
  };
})();