diff --git a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs
index 93967c444..f71a9e020 100644
--- a/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs
+++ b/Source/Editor/CustomEditors/Dedicated/UIControlEditor.cs
@@ -655,7 +655,9 @@ namespace FlaxEditor.CustomEditors.Dedicated
if (uiControl.Name.StartsWith(previousName))
{
string newName = controlType.Name + uiControl.Name.Substring(previousName.Length);
- uiControl.Name = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null);
+ if (uiControl.Parent != null)
+ newName = StringUtils.IncrementNameNumber(newName, x => uiControl.Parent.GetChild(x) == null);
+ uiControl.Name = newName;
}
}
diff --git a/Source/Editor/Editor.cs b/Source/Editor/Editor.cs
index 67d89786d..e4e945f31 100644
--- a/Source/Editor/Editor.cs
+++ b/Source/Editor/Editor.cs
@@ -351,11 +351,19 @@ namespace FlaxEditor
{
// Check if prefab root control is this UIControl
var loadingPreview = Viewport.Previews.PrefabPreview.LoadingPreview;
- if (loadingPreview != null)
+ var activePreviews = Viewport.Previews.PrefabPreview.ActivePreviews;
+ if (activePreviews != null)
{
- // Link it to the prefab preview to see it in the editor
- loadingPreview.customControlLinked = control;
- return loadingPreview;
+ foreach (var preview in activePreviews)
+ {
+ if (preview == loadingPreview ||
+ (preview.Instance != null && (preview.Instance == control || preview.Instance.HasActorInHierarchy(control))))
+ {
+ // Link it to the prefab preview to see it in the editor
+ preview.customControlLinked = control;
+ return preview;
+ }
+ }
}
return null;
}
diff --git a/Source/Editor/Viewport/Previews/PrefabPreview.cs b/Source/Editor/Viewport/Previews/PrefabPreview.cs
index b02cd6f4a..514a3e50b 100644
--- a/Source/Editor/Viewport/Previews/PrefabPreview.cs
+++ b/Source/Editor/Viewport/Previews/PrefabPreview.cs
@@ -1,6 +1,7 @@
// Copyright (c) 2012-2023 Wojciech Figat. All rights reserved.
using System;
+using System.Collections.Generic;
using FlaxEngine;
using Object = FlaxEngine.Object;
@@ -13,10 +14,15 @@ namespace FlaxEditor.Viewport.Previews
public class PrefabPreview : AssetPreview
{
///
- /// The preview that is during prefab instance spawning. Used to link some actors such as UIControl to preview scene and view.
+ /// The currently spawned prefab instance owner. Used to link some actors such as UIControl to preview scene and view.
///
internal static PrefabPreview LoadingPreview;
+ ///
+ /// The list of active prefab previews. Used to link some actors such as UIControl to preview scene and view.
+ ///
+ internal static List ActivePreviews;
+
private Prefab _prefab;
private Actor _instance;
internal UIControl customControlLinked;
@@ -48,13 +54,13 @@ namespace FlaxEditor.Viewport.Previews
_prefab.WaitForLoaded();
// Spawn prefab
- var prevPreview = LoadingPreview;
LoadingPreview = this;
var instance = PrefabManager.SpawnPrefab(_prefab, null);
- LoadingPreview = prevPreview;
+ LoadingPreview = null;
if (instance == null)
{
_prefab = null;
+ ActivePreviews.Remove(this);
throw new Exception("Failed to spawn a prefab for the preview.");
}
@@ -120,6 +126,9 @@ namespace FlaxEditor.Viewport.Previews
public PrefabPreview(bool useWidgets)
: base(useWidgets)
{
+ if (ActivePreviews == null)
+ ActivePreviews = new List();
+ ActivePreviews.Add(this);
}
///
@@ -137,6 +146,7 @@ namespace FlaxEditor.Viewport.Previews
///
public override void OnDestroy()
{
+ ActivePreviews.Remove(this);
Prefab = null;
base.OnDestroy();