1+ // --------------------------------------------------------------------------------------------------------------------
2+ // <copyright file="WebGlBridge.cs">
3+ // Copyright (c) 2021 Johannes Deml. All rights reserved.
4+ // </copyright>
5+ // <author>
6+ // Johannes Deml
7+ // public@deml.io
8+ // </author>
9+ // --------------------------------------------------------------------------------------------------------------------
10+
11+ using System ;
12+ using UnityEngine ;
13+
14+ namespace Supyrb
15+ {
16+ /// <summary>
17+ /// Bridge to Unity to access unity logic through the browser console
18+ /// </summary>
19+ public class WebGlBridge : MonoBehaviour
20+ {
21+ [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
22+ private static void OnBeforeSceneLoadRuntimeMethod ( )
23+ {
24+ var bridgeInstance = new GameObject ( "WebGL" ) ;
25+ DontDestroyOnLoad ( bridgeInstance ) ;
26+ bridgeInstance . AddComponent < WebGlBridge > ( ) ;
27+ }
28+
29+ private void Start ( )
30+ {
31+ Debug . Log ( "Unity WebGL Bridge ready -> Run 'unityGame.SendMessage(\" WebGL\" , \" Help\" )' in the browser console to see usage" ) ;
32+ }
33+
34+ public void Help ( )
35+ {
36+ Debug . Log ( "Available unity interfaces:\n " +
37+ "- LogMemory() -> logs current memory\n " +
38+ "- SetApplicationRunInBackground(int runInBackground) -> Application.runInBackground\n " +
39+ "- SetApplicationTargetFrameRate(int targetFrameRate) -> Application.targetFrameRate\n " +
40+ "- SetTimeFixedDeltaTime(float fixedDeltaTime) -> Time.fixedDeltaTime\n " +
41+ "- SetTimeTimeScale(float timeScale) -> Time.timeScale\n " +
42+ "\n Run a command through 'unityGame.SendMessage(\" WebGL\" , \" COMMAND_NAME\" ,PARAMETER)'" ) ;
43+ }
44+
45+ /// <summary>
46+ /// Logs the current memory usage
47+ /// Browser Usage: <code>unityGame.SendMessage("WebGL","LogMemory");</code>
48+ /// </summary>
49+ public void LogMemory ( )
50+ {
51+ WebGlPlugins . LogMemory ( ) ;
52+ }
53+
54+ /// <summary>
55+ /// Sets if the application should run while not focused.
56+ /// It is in background while another tab is focused or the console is focused
57+ /// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationRunInBackground", 1);</code>
58+ /// </summary>
59+ /// <param name="runInBackground">1 if it should run in background</param>
60+ public void SetApplicationRunInBackground ( int runInBackground )
61+ {
62+ Application . runInBackground = runInBackground == 1 ;
63+ }
64+
65+ /// <summary>
66+ /// Sets the rendering frame rate, see <see cref="Application.targetFrameRate"/>
67+ /// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetApplicationTargetFrameRate", 15);</code>
68+ /// </summary>
69+ /// <param name="targetFrameRate">frame rate to render in</param>
70+ public void SetApplicationTargetFrameRate ( int targetFrameRate )
71+ {
72+ Application . targetFrameRate = targetFrameRate ;
73+ }
74+
75+ /// <summary>
76+ /// Sets the interval for fixed time updates, see <see cref="Time.fixedDeltaTime"/>
77+ /// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeFixedDeltaTime", 0.02);</code>
78+ /// </summary>
79+ /// <param name="fixedDeltaTime"></param>
80+ public void SetTimeFixedDeltaTime ( float fixedDeltaTime )
81+ {
82+ Time . fixedDeltaTime = fixedDeltaTime ;
83+ }
84+
85+ /// <summary>
86+ /// Sets the global timeScale, see <see cref="Time.timeScale"/>
87+ /// Somehow the timescale also affects the physics refresh rate on WebGL
88+ /// Browser Usage: <code>unityGame.SendMessage("WebGL", "SetTimeTimeScale", 0.2);</code>
89+ /// </summary>
90+ /// <param name="timeScale">new timescale value</param>
91+ public void SetTimeTimeScale ( float timeScale )
92+ {
93+ Time . timeScale = timeScale ;
94+ }
95+ }
96+ }
0 commit comments