@@ -36,80 +36,124 @@ public class UwbWebView : MonoBehaviour, IWebBrowserClient
3636
3737 public async UniTask Init ( int engineStartupTimeoutMs , bool redactTokensInLogs , Func < string , string > redactionHandler )
3838 {
39- GameObject persistentObject = new GameObject ( "UWB_Bridge" ) ;
40-
41- var rawImage = persistentObject . AddComponent < RawImage > ( ) ;
42- rawImage . color = Color . clear ;
43-
44- var ui = persistentObject . AddComponent < WebBrowserUIFull > ( ) ;
45-
46- // Assign a basic input handler so UWB doesn't throw
47- var inputHandler = ScriptableObject . CreateInstance < WebBrowserOldInputHandler > ( ) ;
48- ui . inputHandler = inputHandler ;
49-
50- DontDestroyOnLoad ( persistentObject ) ;
51-
52- WebBrowserClient browserClient = ui . browserClient ;
53- browserClient . engineStartupTimeout = engineStartupTimeoutMs ;
54-
55- // Disable sandbox for Windows VM compatibility
56- browserClient . noSandbox = true ;
57-
58- // Apply Passport logging preferences to the UWB client
59- UwbLogConfig . ApplyTo ( browserClient ) ;
60-
61- // Js
62- browserClient . jsMethodManager = new JsMethodManager { jsMethodsEnable = true } ;
63- browserClient . RegisterJsMethod < string > ( "callback" ,
64- ( message ) => { OnUnityPostMessage ? . Invoke ( message ) ; } ) ;
65-
66- // Cache
67- var browserEngineMainDir = WebBrowserUtils . GetAdditionFilesDirectory ( ) ;
68- browserClient . CachePath = new FileInfo ( Path . Combine ( browserEngineMainDir , "ImmutableSDK/UWBCache" ) ) ;
69-
70- // Start local HTTP server to serve index.html
71- gameBridgeServer = new GameBridgeServer ( GameBridge . GetFileSystemPath ( ) ) ;
72- browserClient . initialUrl = gameBridgeServer . Start ( ) ;
73-
74- // Set up engine from standard UWB configuration asset
75- var engineConfigAsset = Resources . Load < EngineConfiguration > ( "Cef Engine Configuration" ) ;
76- if ( engineConfigAsset == null )
39+ try
7740 {
78- Debug . LogError ( "[UwbWebView] Could not find 'Cef Engine Configuration' Resources asset. " +
79- "Ensure the UnityWebBrowser engine package is installed." ) ;
41+ Debug . Log ( "[UwbWebView] Init started..." ) ;
42+
43+ GameObject persistentObject = new GameObject ( "UWB_Bridge" ) ;
44+ Debug . Log ( "[UwbWebView] GameObject created" ) ;
45+
46+ var rawImage = persistentObject . AddComponent < RawImage > ( ) ;
47+ rawImage . color = Color . clear ;
48+
49+ var ui = persistentObject . AddComponent < WebBrowserUIFull > ( ) ;
50+ Debug . Log ( "[UwbWebView] WebBrowserUIFull component added" ) ;
51+
52+ // Assign a basic input handler so UWB doesn't throw
53+ var inputHandler = ScriptableObject . CreateInstance < WebBrowserOldInputHandler > ( ) ;
54+ ui . inputHandler = inputHandler ;
55+
56+ DontDestroyOnLoad ( persistentObject ) ;
57+
58+ WebBrowserClient browserClient = ui . browserClient ;
59+ browserClient . engineStartupTimeout = engineStartupTimeoutMs ;
60+ Debug . Log ( $ "[UwbWebView] Engine startup timeout set to { engineStartupTimeoutMs } ms") ;
61+
62+ // Disable sandbox for Windows VM compatibility
63+ browserClient . noSandbox = true ;
64+
65+ // Apply Passport logging preferences to the UWB client
66+ UwbLogConfig . ApplyTo ( browserClient ) ;
67+
68+ // Js
69+ browserClient . jsMethodManager = new JsMethodManager { jsMethodsEnable = true } ;
70+ browserClient . RegisterJsMethod < string > ( "callback" ,
71+ ( message ) => { OnUnityPostMessage ? . Invoke ( message ) ; } ) ;
72+ Debug . Log ( "[UwbWebView] JavaScript callback method registered" ) ;
73+
74+ // Cache
75+ var browserEngineMainDir = WebBrowserUtils . GetAdditionFilesDirectory ( ) ;
76+ browserClient . CachePath = new FileInfo ( Path . Combine ( browserEngineMainDir , "ImmutableSDK/UWBCache" ) ) ;
77+ Debug . Log ( $ "[UwbWebView] Cache path set to: { browserClient . CachePath . FullName } ") ;
78+
79+ // Start local HTTP server to serve index.html
80+ Debug . Log ( "[UwbWebView] Starting GameBridgeServer..." ) ;
81+ gameBridgeServer = new GameBridgeServer ( GameBridge . GetFileSystemPath ( ) ) ;
82+ string serverUrl = gameBridgeServer . Start ( ) ;
83+ browserClient . initialUrl = serverUrl ;
84+ Debug . Log ( $ "[UwbWebView] GameBridgeServer started at: { serverUrl } ") ;
85+
86+ // Set up engine from standard UWB configuration asset
87+ var engineConfigAsset = Resources . Load < EngineConfiguration > ( "Cef Engine Configuration" ) ;
88+ if ( engineConfigAsset == null )
89+ {
90+ Debug . LogError ( "[UwbWebView] Could not find 'Cef Engine Configuration' Resources asset. " +
91+ "Ensure the UnityWebBrowser engine package is installed." ) ;
92+ }
93+ else
94+ {
95+ var engineConfig = ScriptableObject . Instantiate ( engineConfigAsset ) ;
96+ browserClient . engine = engineConfig ;
97+ Debug . Log ( "[UwbWebView] CEF Engine configuration loaded" ) ;
98+ }
99+
100+ // Find available ports
101+ TCPCommunicationLayer tcpCommunicationLayer = ScriptableObject . CreateInstance < TCPCommunicationLayer > ( ) ;
102+ var rnd = new System . Random ( ) ;
103+ do
104+ {
105+ tcpCommunicationLayer . inPort = rnd . Next ( 1024 , 65353 ) ;
106+ tcpCommunicationLayer . outPort = tcpCommunicationLayer . inPort + 1 ;
107+ } while ( ! CheckAvailableServerPort ( tcpCommunicationLayer . inPort ) || ! CheckAvailableServerPort ( tcpCommunicationLayer . outPort ) ) ;
108+
109+ browserClient . communicationLayer = tcpCommunicationLayer ;
110+ Debug . Log ( $ "[UwbWebView] Communication layer configured - inPort: { tcpCommunicationLayer . inPort } , outPort: { tcpCommunicationLayer . outPort } ") ;
111+
112+ Debug . Log ( "[UwbWebView] Waiting for browser client to connect..." ) ;
113+ await WaitForClientConnected ( browserClient ) ;
114+ Debug . Log ( "[UwbWebView] Browser client connected successfully!" ) ;
115+
116+ this . webBrowserClient = browserClient ;
117+ Debug . Log ( "[UwbWebView] Init completed successfully" ) ;
80118 }
81- else
119+ catch ( Exception ex )
82120 {
83- var engineConfig = ScriptableObject . Instantiate ( engineConfigAsset ) ;
84- browserClient . engine = engineConfig ;
121+ Debug . LogError ( $ "[UwbWebView] Init failed with exception: { ex . GetType ( ) . Name } : { ex . Message } ") ;
122+ Debug . LogError ( $ "[UwbWebView] Stack trace: { ex . StackTrace } ") ;
123+ throw ;
85124 }
86-
87- // Find available ports
88- TCPCommunicationLayer tcpCommunicationLayer = ScriptableObject . CreateInstance < TCPCommunicationLayer > ( ) ;
89- var rnd = new System . Random ( ) ;
90- do
91- {
92- tcpCommunicationLayer . inPort = rnd . Next ( 1024 , 65353 ) ;
93- tcpCommunicationLayer . outPort = tcpCommunicationLayer . inPort + 1 ;
94- } while ( ! CheckAvailableServerPort ( tcpCommunicationLayer . inPort ) || ! CheckAvailableServerPort ( tcpCommunicationLayer . outPort ) ) ;
95-
96- browserClient . communicationLayer = tcpCommunicationLayer ;
97-
98- await WaitForClientConnected ( browserClient ) ;
99-
100- this . webBrowserClient = browserClient ;
101125 }
102126
103- private UniTask WaitForClientConnected ( WebBrowserClient webBrowserClient )
127+ private async UniTask WaitForClientConnected ( WebBrowserClient webBrowserClient )
104128 {
105129 var tcs = new TaskCompletionSource < bool > ( ) ;
130+ var timeoutSeconds = 30 ;
106131
107132 webBrowserClient . OnLoadFinish += OnLoadFinish ;
108133
109- return tcs . Task . AsUniTask ( ) ;
134+ // Create timeout task
135+ var timeoutTask = UniTask . Delay ( TimeSpan . FromSeconds ( timeoutSeconds ) ) ;
136+ var completionTask = tcs . Task . AsUniTask ( ) ;
137+
138+ Debug . Log ( $ "[UwbWebView] Waiting up to { timeoutSeconds } seconds for OnLoadFinish event...") ;
139+
140+ // Race between completion and timeout
141+ var completedTask = await UniTask . WhenAny ( completionTask , timeoutTask ) ;
142+
143+ if ( completedTask == 1 ) // Timeout won
144+ {
145+ webBrowserClient . OnLoadFinish -= OnLoadFinish ;
146+ var errorMsg = $ "[UwbWebView] Timeout after { timeoutSeconds } seconds waiting for browser OnLoadFinish event. " +
147+ $ "The browser failed to load the initial URL. This may indicate a CEF initialization failure.";
148+ Debug . LogError ( errorMsg ) ;
149+ throw new TimeoutException ( errorMsg ) ;
150+ }
151+
152+ Debug . Log ( $ "[UwbWebView] OnLoadFinish event received successfully") ;
110153
111154 void OnLoadFinish ( string url )
112155 {
156+ Debug . Log ( $ "[UwbWebView] OnLoadFinish fired for URL: { url } ") ;
113157 webBrowserClient . OnLoadFinish -= OnLoadFinish ;
114158 tcs . SetResult ( true ) ;
115159 }
0 commit comments