Skip to content

Commit 8ae2f74

Browse files
committed
feat: define vuplex symbol on load
1 parent bd43fd9 commit 8ae2f74

File tree

4 files changed

+122
-36
lines changed

4 files changed

+122
-36
lines changed
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
using UnityEngine;
2+
using UnityEditor;
3+
using System.Linq;
4+
5+
namespace Immutable.Passport.Editor
6+
{
7+
/// <summary>
8+
/// Automatically manages VUPLEX_WEBVIEW scripting define symbol
9+
/// based on whether Vuplex WebView is present in the project
10+
/// </summary>
11+
[InitializeOnLoad]
12+
public static class VuplexSymbolManager
13+
{
14+
private const string VUPLEX_SYMBOL = "VUPLEX_WEBVIEW";
15+
16+
static VuplexSymbolManager()
17+
{
18+
// Run on editor startup and after assembly reload
19+
EditorApplication.delayCall += CheckAndUpdateVuplexSymbol;
20+
}
21+
22+
private static void CheckAndUpdateVuplexSymbol()
23+
{
24+
try
25+
{
26+
// Check if Vuplex WebView assembly exists
27+
bool vuplexExists = DoesVuplexAssemblyExist();
28+
29+
// Get current build target group
30+
var buildTargetGroup = EditorUserBuildSettings.selectedBuildTargetGroup;
31+
32+
// Get current scripting define symbols
33+
var currentSymbols = PlayerSettings.GetScriptingDefineSymbolsForGroup(buildTargetGroup);
34+
var symbolList = currentSymbols.Split(';').Where(s => !string.IsNullOrEmpty(s)).ToList();
35+
36+
bool symbolExists = symbolList.Contains(VUPLEX_SYMBOL);
37+
38+
// Update symbol if needed
39+
if (vuplexExists && !symbolExists)
40+
{
41+
// Add symbol
42+
symbolList.Add(VUPLEX_SYMBOL);
43+
var newSymbols = string.Join(";", symbolList);
44+
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, newSymbols);
45+
Debug.Log($"[VuplexSymbolManager] Added {VUPLEX_SYMBOL} symbol - Vuplex WebView detected");
46+
}
47+
else if (!vuplexExists && symbolExists)
48+
{
49+
// Remove symbol
50+
symbolList.Remove(VUPLEX_SYMBOL);
51+
var newSymbols = string.Join(";", symbolList);
52+
PlayerSettings.SetScriptingDefineSymbolsForGroup(buildTargetGroup, newSymbols);
53+
Debug.Log($"[VuplexSymbolManager] Removed {VUPLEX_SYMBOL} symbol - Vuplex WebView not found");
54+
}
55+
56+
// Log current status (only once per session to avoid spam)
57+
if (!SessionState.GetBool("VuplexSymbolManager.LoggedStatus", false))
58+
{
59+
if (vuplexExists)
60+
{
61+
Debug.Log($"[VuplexSymbolManager] Vuplex WebView available, {VUPLEX_SYMBOL} symbol active");
62+
}
63+
else
64+
{
65+
Debug.Log($"[VuplexSymbolManager] Vuplex WebView not found");
66+
}
67+
SessionState.SetBool("VuplexSymbolManager.LoggedStatus", true);
68+
}
69+
}
70+
catch (System.Exception ex)
71+
{
72+
Debug.LogError($"[VuplexSymbolManager] Error managing Vuplex symbol: {ex.Message}");
73+
}
74+
}
75+
76+
private static bool DoesVuplexAssemblyExist()
77+
{
78+
// Check for Vuplex assembly in multiple ways
79+
80+
// Method 1: Check compiled assemblies
81+
var assemblies = System.AppDomain.CurrentDomain.GetAssemblies();
82+
if (assemblies.Any(a => a.GetName().Name == "Vuplex.WebView"))
83+
{
84+
return true;
85+
}
86+
87+
// Method 2: Check for Vuplex types
88+
var vuplexType = System.Type.GetType("Vuplex.WebView.CanvasWebViewPrefab, Vuplex.WebView");
89+
if (vuplexType != null)
90+
{
91+
return true;
92+
}
93+
94+
// Method 3: Check for Vuplex assets in project
95+
var vuplexAssets = AssetDatabase.FindAssets("CanvasWebViewPrefab");
96+
foreach (var guid in vuplexAssets)
97+
{
98+
var path = AssetDatabase.GUIDToAssetPath(guid);
99+
if (path.Contains("Vuplex"))
100+
{
101+
return true;
102+
}
103+
}
104+
105+
return false;
106+
}
107+
}
108+
}

src/Packages/Passport/Editor/VuplexSymbolManager.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

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

9-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
9+
#if (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX) && VUPLEX_WEBVIEW
1010
using Vuplex.WebView;
11-
#endif
1211

1312
namespace Immutable.Passport
1413
{
@@ -21,9 +20,7 @@ public class MacOSPassportWebView : IPassportWebView
2120
{
2221
private const string TAG = "[MacOSPassportWebView]";
2322

24-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
2523
private CanvasWebViewPrefab? _webViewPrefab;
26-
#endif
2724
private readonly Dictionary<string, Action<string>> _jsHandlers = new Dictionary<string, Action<string>>();
2825
private readonly RawImage _canvasReference;
2926
private bool _isInitialized = false;
@@ -33,14 +30,8 @@ public class MacOSPassportWebView : IPassportWebView
3330
public event Action? OnLoadFinished;
3431
public event Action? OnLoadStarted;
3532

36-
// Safe access - check initialization
37-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
3833
public bool IsVisible => _webViewPrefab?.Visible ?? false;
3934
public string CurrentUrl => _webViewPrefab?.WebView?.Url ?? "";
40-
#else
41-
public bool IsVisible => false;
42-
public string CurrentUrl => "";
43-
#endif
4435

4536
public MacOSPassportWebView(RawImage canvasReference)
4637
{
@@ -55,7 +46,6 @@ public void Initialize(PassportWebViewConfig config)
5546
return;
5647
}
5748

58-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
5949
try
6050
{
6151
PassportLogger.Info($"{TAG} Initializing MacOS WebView...");
@@ -68,13 +58,8 @@ public void Initialize(PassportWebViewConfig config)
6858
PassportLogger.Error($"{TAG} Failed to initialize: {ex.Message}");
6959
throw;
7060
}
71-
#else
72-
PassportLogger.Warn($"{TAG} Vuplex WebView is only supported on MacOS builds, not in editor");
73-
_isInitialized = true;
74-
#endif
7561
}
7662

77-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
7863
private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
7964
{
8065
try
@@ -162,11 +147,9 @@ private async UniTaskVoid InitializeAsync(PassportWebViewConfig config)
162147
throw;
163148
}
164149
}
165-
#endif
166150

167151
public void LoadUrl(string url)
168152
{
169-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
170153
if (!_isInitialized || _webViewPrefab?.WebView == null)
171154
{
172155
PassportLogger.Info($"{TAG} WebView not ready, queueing URL: {url}");
@@ -176,50 +159,35 @@ public void LoadUrl(string url)
176159

177160
PassportLogger.Info($"{TAG} Loading URL: {url}");
178161
_webViewPrefab.WebView.LoadUrl(url);
179-
#else
180-
PassportLogger.Warn($"{TAG} LoadUrl not supported in MacOS editor mode");
181-
#endif
182162
}
183163

184164
public void Show()
185165
{
186-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
187166
if (_webViewPrefab != null)
188167
{
189168
_webViewPrefab.Visible = true;
190169
PassportLogger.Info($"{TAG} WebView shown");
191170
}
192-
#else
193-
PassportLogger.Info($"{TAG} Show() called (editor mode)");
194-
#endif
195171
}
196172

197173
public void Hide()
198174
{
199-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
200175
if (_webViewPrefab != null)
201176
{
202177
_webViewPrefab.Visible = false;
203178
PassportLogger.Info($"{TAG} WebView hidden");
204179
}
205-
#else
206-
PassportLogger.Info($"{TAG} Hide() called (editor mode)");
207-
#endif
208180
}
209181

210182
public void ExecuteJavaScript(string js)
211183
{
212-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
213184
if (!_isInitialized || _webViewPrefab?.WebView == null)
214185
{
215186
PassportLogger.Error($"{TAG} Cannot execute JavaScript - MacOS WebView not initialized");
216187
return;
217188
}
218189

219190
_webViewPrefab.WebView.ExecuteJavaScript(js);
220-
#else
221-
PassportLogger.Warn($"{TAG} ExecuteJavaScript not supported in MacOS editor mode");
222-
#endif
223191
}
224192

225193
public void RegisterJavaScriptMethod(string methodName, Action<string> handler)
@@ -233,17 +201,16 @@ public void RegisterJavaScriptMethod(string methodName, Action<string> handler)
233201

234202
public void Dispose()
235203
{
236-
#if UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX
237204
if (_webViewPrefab != null)
238205
{
239206
PassportLogger.Info($"{TAG} Disposing MacOS WebView");
240207
_webViewPrefab.Destroy();
241208
_webViewPrefab = null;
242209
}
243-
#endif
244210

245211
_jsHandlers.Clear();
246212
_isInitialized = false;
247213
}
248214
}
249215
}
216+
#endif

src/Packages/Passport/Runtime/Scripts/Public/PassportUI.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ private IPassportWebView CreatePlatformWebView()
408408
#elif UNITY_ANDROID
409409
PassportLogger.Info($"{TAG} Creating Android WebView (Vuplex)");
410410
return new AndroidVuplexWebView(rawImage);
411-
#elif UNITY_STANDALONE_OSX
411+
#elif (UNITY_STANDALONE_OSX || UNITY_EDITOR_OSX) && VUPLEX_WEBVIEW
412412
PassportLogger.Info($"{TAG} Creating MacOS WebView (Vuplex)");
413413
return new MacOSPassportWebView(rawImage);
414414
#else

0 commit comments

Comments
 (0)