diff --git a/Source/Editor/Viewport/Previews/PrefabPreview.cs b/Source/Editor/Viewport/Previews/PrefabPreview.cs index 066e98456..09ff34d0d 100644 --- a/Source/Editor/Viewport/Previews/PrefabPreview.cs +++ b/Source/Editor/Viewport/Previews/PrefabPreview.cs @@ -121,6 +121,18 @@ namespace FlaxEditor.Viewport.Previews { } + /// + public override void Update(float deltaTime) + { + base.Update(deltaTime); + + if (_instance != null) + { + // Link UI canvases to the preview (eg. after canvas added to the prefab) + LinkCanvas(_instance); + } + } + /// public override void OnDestroy() { diff --git a/Source/Engine/UI/UICanvas.cpp b/Source/Engine/UI/UICanvas.cpp index ccd0561ad..ab1841acc 100644 --- a/Source/Engine/UI/UICanvas.cpp +++ b/Source/Engine/UI/UICanvas.cpp @@ -15,6 +15,7 @@ MMethod* UICanvas_PostDeserialize = nullptr; MMethod* UICanvas_OnEnable = nullptr; MMethod* UICanvas_OnDisable = nullptr; MMethod* UICanvas_EndPlay = nullptr; +MMethod* UICanvas_ParentChanged = nullptr; #define UICANVAS_INVOKE(event) \ auto instance = GetManagedInstance(); \ @@ -43,6 +44,7 @@ UICanvas::UICanvas(const SpawnParams& params) UICanvas_OnEnable = mclass->GetMethod("OnEnable"); UICanvas_OnDisable = mclass->GetMethod("OnDisable"); UICanvas_EndPlay = mclass->GetMethod("EndPlay"); + UICanvas_ParentChanged = mclass->GetMethod("ParentChanged"); } } @@ -133,6 +135,14 @@ void UICanvas::EndPlay() Actor::EndPlay(); } +void UICanvas::OnParentChanged() +{ + // Base + Actor::OnParentChanged(); + + UICANVAS_INVOKE(ParentChanged); +} + void UICanvas::OnEnable() { UICANVAS_INVOKE(OnEnable); diff --git a/Source/Engine/UI/UICanvas.cs b/Source/Engine/UI/UICanvas.cs index 81b3e09dc..0c5a32def 100644 --- a/Source/Engine/UI/UICanvas.cs +++ b/Source/Engine/UI/UICanvas.cs @@ -539,6 +539,16 @@ namespace FlaxEngine Setup(); } + internal void ParentChanged() + { +#if FLAX_EDITOR + if (RenderMode == CanvasRenderMode.ScreenSpace && _editorRoot != null && _guiRoot != null) + { + _guiRoot.Parent = HasParent ? _editorRoot : null; + } +#endif + } + internal void OnEnable() { #if FLAX_EDITOR @@ -587,6 +597,8 @@ namespace FlaxEngine internal void EditorOverride(SceneRenderTask task, ContainerControl root) { + if (_editorTask == task && _editorRoot == root) + return; if (_editorTask != null && _renderer != null) _editorTask.CustomPostFx.Remove(_renderer); if (_editorRoot != null && _guiRoot != null) diff --git a/Source/Engine/UI/UICanvas.h b/Source/Engine/UI/UICanvas.h index 8e8b9b0d1..cb049c684 100644 --- a/Source/Engine/UI/UICanvas.h +++ b/Source/Engine/UI/UICanvas.h @@ -24,6 +24,7 @@ protected: // [Actor] void BeginPlay(SceneBeginData* data) final override; void EndPlay() final override; + void OnParentChanged() override; void OnEnable() override; void OnDisable() override; void OnTransformChanged() final override;