Fix UIControl linkage to Prefab window when creating UI within prefab Editor

#1007
This commit is contained in:
Wojtek Figat
2023-05-08 19:22:23 +02:00
parent 3ea771fe22
commit 8edc154501
3 changed files with 28 additions and 8 deletions

View File

@@ -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;
}
}

View File

@@ -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;
}

View File

@@ -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
{
/// <summary>
/// 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.
/// </summary>
internal static PrefabPreview LoadingPreview;
/// <summary>
/// The list of active prefab previews. Used to link some actors such as UIControl to preview scene and view.
/// </summary>
internal static List<PrefabPreview> 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<PrefabPreview>();
ActivePreviews.Add(this);
}
/// <inheritdoc />
@@ -137,6 +146,7 @@ namespace FlaxEditor.Viewport.Previews
/// <inheritdoc />
public override void OnDestroy()
{
ActivePreviews.Remove(this);
Prefab = null;
base.OnDestroy();