Hi, I want to seperate a mix-mash script into a build / runtime script and editor script, but I have difficulties getting the correct method calls at the correct moments. How would you approach this?
Take a look at my Init() function for example:
private async void Init()
{
if (_initialized) return;
WebView = Web.CreateWebView();
#if UNITY_EDITOR
await EditorInit();
#endif
// Inject PageLoadScrips from properties
if (HideScrollbars) AddPageLoadScript(HideScrollbarsJS);
WebView.PageLoadScripts.AddRange(_pageLoadScripts);
Native2DWebView = WebView as IWithNative2DMode;
Rect webViewSpace = GetAspectRatioRect(GetViewportRect(Viewport), AspectRatio);
Native2DWebView?.InitInNative2DMode(webViewSpace);
if (!string.IsNullOrEmpty(InitialUrl))
WebView.LoadUrl(InitialUrl);
_initialized = true;
WebView.LoadProgressChanged += OnLoadProgressChanged;
}
You can see that the Editor code must be called after the WebView was created, but before the WebView.LoadUrl() function is triggered. For now I have the wildest #if UNITY_EDITOR inside this script and it really bugs me. But I can’t find a way to seperate editor code into a seperate class like that.
The editor code on Init() won’t be called at the right time, and the runtime script also can’t call it, since the Editor Code is in another non-linked assembly. The only thing I could think of is to add a public event at this position that is only used by the editor script, but more confusing for anyone using this class…
This Property illustrates my problem also very good:
public RectTransform Viewport
{
get
{
if (!viewport)
viewport = transform.ToRectTransform();
return viewport;
}
set
{
viewport = value;
#if UNITY_EDITOR
_editorWebViewTrans.SetParent(Viewport.transform, false);
#endif
}
}
Any suggestions on how to do this? I might stick with all these compiler tags if I can’t get this refactored.