Skip to content

Commit 24f86f6

Browse files
committed
fix: queue webview load when webview is not initialised
1 parent 792e24f commit 24f86f6

File tree

1 file changed

+25
-11
lines changed

1 file changed

+25
-11
lines changed

src/Packages/Passport/Runtime/Scripts/Private/UI/MacOSPassportWebView.cs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
using UnityEngine.UI;
77
using Immutable.Passport.Core.Logging;
88

9-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
9+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
1010
using Vuplex.WebView;
1111
#endif
1212

@@ -21,19 +21,20 @@ public class MacOSPassportWebView : IPassportWebView
2121
{
2222
private const string TAG = "[MacOSPassportWebView]";
2323

24-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
24+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
2525
private CanvasWebViewPrefab? _webViewPrefab;
2626
#endif
2727
private readonly Dictionary<string, Action<string>> _jsHandlers = new Dictionary<string, Action<string>>();
2828
private readonly RawImage _canvasReference;
2929
private bool _isInitialized = false;
30+
private string? _queuedUrl = null; // Queue URL if LoadUrl called before initialization
3031

3132
public event Action<string>? OnJavaScriptMessage;
3233
public event Action? OnLoadFinished;
3334
public event Action? OnLoadStarted;
3435

3536
// Safe access - check initialization
36-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
37+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
3738
public bool IsVisible => _webViewPrefab?.Visible ?? false;
3839
public string CurrentUrl => _webViewPrefab?.WebView?.Url ?? "";
3940
#else
@@ -54,7 +55,7 @@ public void Initialize(PassportWebViewConfig config)
5455
return;
5556
}
5657

57-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
58+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
5859
try
5960
{
6061
PassportLogger.Info($"{TAG} Initializing MacOS WebView...");
@@ -73,13 +74,15 @@ public void Initialize(PassportWebViewConfig config)
7374
#endif
7475
}
7576

76-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
77+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
7778
private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
7879
{
7980
try
8081
{
82+
PassportLogger.Info($"{TAG} Starting Vuplex CanvasWebViewPrefab instantiation...");
8183
// Create WebView prefab and parent to Canvas
8284
_webViewPrefab = CanvasWebViewPrefab.Instantiate();
85+
PassportLogger.Info($"{TAG} CanvasWebViewPrefab created successfully");
8386
_webViewPrefab.Native2DModeEnabled = false; // Use standard mode for better desktop compatibility
8487

8588
// Set higher resolution for desktop - MacOS can handle larger textures
@@ -97,6 +100,7 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
97100

98101
// Wait for WebView initialization
99102
await _webViewPrefab.WaitUntilInitialized();
103+
PassportLogger.Info($"{TAG} Vuplex WebView initialization completed");
100104

101105
// Setup event handlers
102106
_webViewPrefab.WebView.LoadProgressChanged += (sender, progressArgs) =>
@@ -136,6 +140,15 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
136140

137141
_isInitialized = true;
138142
PassportLogger.Info($"{TAG} MacOS WebView initialized successfully");
143+
144+
// Load queued URL if one was requested before initialization completed
145+
if (!string.IsNullOrEmpty(_queuedUrl))
146+
{
147+
PassportLogger.Info($"{TAG} Loading queued URL: {_queuedUrl}");
148+
var urlToLoad = _queuedUrl;
149+
_queuedUrl = null; // Clear the queue
150+
_webViewPrefab.WebView.LoadUrl(urlToLoad);
151+
}
139152
}
140153
catch (Exception ex)
141154
{
@@ -147,10 +160,11 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
147160

148161
public void LoadUrl(string url)
149162
{
150-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
163+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
151164
if (!_isInitialized || _webViewPrefab?.WebView == null)
152165
{
153-
PassportLogger.Error($"{TAG} Cannot load URL - MacOS WebView not initialized");
166+
PassportLogger.Info($"{TAG} WebView not ready, queueing URL: {url}");
167+
_queuedUrl = url; // Queue the URL for later loading
154168
return;
155169
}
156170

@@ -163,7 +177,7 @@ public void LoadUrl(string url)
163177

164178
public void Show()
165179
{
166-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
180+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
167181
if (_webViewPrefab != null)
168182
{
169183
_webViewPrefab.Visible = true;
@@ -176,7 +190,7 @@ public void Show()
176190

177191
public void Hide()
178192
{
179-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
193+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
180194
if (_webViewPrefab != null)
181195
{
182196
_webViewPrefab.Visible = false;
@@ -189,7 +203,7 @@ public void Hide()
189203

190204
public void ExecuteJavaScript(string js)
191205
{
192-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
206+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
193207
if (!_isInitialized || _webViewPrefab?.WebView == null)
194208
{
195209
PassportLogger.Error($"{TAG} Cannot execute JavaScript - MacOS WebView not initialized");
@@ -213,7 +227,7 @@ public void RegisterJavaScriptMethod(string methodName, Action<string> handler)
213227

214228
public void Dispose()
215229
{
216-
#if UNITY_STANDALONE_OSX && !UNITY_EDITOR
230+
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
217231
if (_webViewPrefab != null)
218232
{
219233
PassportLogger.Info($"{TAG} Disposing MacOS WebView");

0 commit comments

Comments
 (0)