99// --------------------------------------------------------------------------------------------------------------------
1010
1111using System ;
12+ using System . Collections . Generic ;
1213using System . Reflection ;
1314using System . Text ;
1415using Supyrb . Attributes ;
@@ -23,13 +24,15 @@ namespace Supyrb
2324 /// </summary>
2425 public class WebBridge : WebCommands
2526 {
27+ private const string WebBridgeGameObjectName = "WebBridge" ;
28+
2629 private static GameObject bridgeInstance ;
2730#if UNITY_WEBGL
2831 [ RuntimeInitializeOnLoadMethod ( RuntimeInitializeLoadType . BeforeSceneLoad ) ]
2932 private static void OnBeforeSceneLoadRuntimeMethod ( )
3033 {
3134 SetGlobalVariables ( ) ;
32- bridgeInstance = new GameObject ( "Web" ) ;
35+ bridgeInstance = new GameObject ( WebBridgeGameObjectName ) ;
3336 DontDestroyOnLoad ( bridgeInstance ) ;
3437 AddAllWebCommands ( ) ;
3538 }
@@ -38,18 +41,25 @@ private static void OnBeforeSceneLoadRuntimeMethod()
3841 private static void AddAllWebCommands ( )
3942 {
4043 Assembly [ ] assemblies = AppDomain . CurrentDomain . GetAssemblies ( ) ;
44+ List < Type > webCommandTypes = new List < Type > ( ) ;
4145 foreach ( var assembly in assemblies )
4246 {
4347 Type [ ] types = assembly . GetTypes ( ) ;
4448 foreach ( var type in types )
4549 {
4650 if ( type . IsSubclassOf ( typeof ( WebCommands ) ) )
4751 {
48- bridgeInstance . AddComponent ( type ) ;
52+ webCommandTypes . Add ( type ) ;
4953 }
5054 }
5155 }
5256
57+ // Sort the commands to make the output deterministic
58+ webCommandTypes . Sort ( ( a , b ) => a . Name . CompareTo ( b . Name ) ) ;
59+ foreach ( var webCommandType in webCommandTypes )
60+ {
61+ bridgeInstance . AddComponent ( webCommandType ) ;
62+ }
5363 }
5464
5565 private static void SetGlobalVariables ( )
@@ -82,7 +92,7 @@ private static void SetGlobalVariables()
8292
8393 private void Start ( )
8494 {
85- Debug . Log ( "Unity WebGL Bridge ready -> Run 'unityGame.SendMessage( \" WebGL \" , \" Help\" )' in the browser console to see usage" ) ;
95+ Debug . Log ( $ "Unity WebGL Bridge ready -> Run 'runUnityCommand( \" Help\" )' in the browser console to see usage") ;
8696 }
8797
8898 [ WebCommand ( Description = "Log all available commands" ) ]
@@ -103,7 +113,7 @@ public void Help()
103113 WebCommandAttribute commandAttribute = method . GetCustomAttribute < WebCommandAttribute > ( ) ;
104114 if ( commandAttribute != null )
105115 {
106- sb . Append ( $ "unityGame.SendMessage( \" WebGL \" , \" { method . Name } \" ") ;
116+ sb . Append ( $ "runUnityCommand( \" { method . Name } \" ") ;
107117 ParameterInfo [ ] parameters = method . GetParameters ( ) ;
108118 for ( int j = 0 ; j < parameters . Length ; j ++ )
109119 {
@@ -124,7 +134,7 @@ public void Help()
124134
125135 }
126136
127- sb . AppendLine ( "\n Run a command with 'unityGame.SendMessage( \" WebGL \" , \" COMMAND_NAME\" ,PARAMETER);'" ) ;
137+ sb . AppendLine ( $ "\n Run a command with 'runUnityCommand( \" COMMAND_NAME\" ,PARAMETER);'") ;
128138 Debug . Log ( sb . ToString ( ) ) ;
129139 }
130140 }
0 commit comments