Fix errors when changing UIControl type in prefab

#548
This commit is contained in:
Wojtek Figat
2021-06-09 13:27:37 +02:00
parent 384172050d
commit c38b81d682
3 changed files with 27 additions and 27 deletions

View File

@@ -142,6 +142,10 @@ namespace FlaxEditor.CustomEditors
/// </summary>
public void RebuildLayout()
{
// Skip rebuilding during init
if (CurrentCustomEditor == this)
return;
// Special case for root objects to run normal layout build
if (_presenter.Selection == Values)
{

View File

@@ -412,8 +412,6 @@ namespace FlaxEditor.CustomEditors.Dedicated
public override void Initialize(LayoutElementsContainer layout)
{
_cachedType = null;
if (Values.HasNull)
return;
if (HasDifferentTypes)
{
// TODO: support stable editing multiple different control types (via generic way or for transform-only)
@@ -626,8 +624,6 @@ namespace FlaxEditor.CustomEditors.Dedicated
{
RebuildLayout();
}
//RefreshValues();
}
base.Refresh();
@@ -663,39 +659,32 @@ namespace FlaxEditor.CustomEditors.Dedicated
cm.Show(button.Parent, button.BottomLeft);
}
private void SetType(ref ScriptType controlType, UIControl uiControl)
{
string previousName = uiControl.Control?.GetType().Name ?? nameof(UIControl);
uiControl.Control = (Control)controlType.CreateInstance();
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);
}
}
private void SetType(ScriptType controlType)
{
var uiControls = ParentEditor.Values;
if (Presenter.Undo != null)
if (Presenter.Undo?.Enabled ?? false)
{
using (new UndoMultiBlock(Presenter.Undo, uiControls, "Set Control Type"))
{
for (int i = 0; i < uiControls.Count; i++)
{
var uiControl = (UIControl)uiControls[i];
string previousName = uiControl.Control?.GetType().Name ?? nameof(UIControl);
uiControl.Control = (Control)controlType.CreateInstance();
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);
}
}
SetType(ref controlType, (UIControl)uiControls[i]);
}
}
else
{
for (int i = 0; i < uiControls.Count; i++)
{
var uiControl = (UIControl)uiControls[i];
string previousName = uiControl.Control?.GetType().Name ?? nameof(UIControl);
uiControl.Control = (Control)controlType.CreateInstance();
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);
}
}
SetType(ref controlType, (UIControl)uiControls[i]);
}
ParentEditor.RebuildLayout();

View File

@@ -255,8 +255,15 @@ namespace FlaxEditor.CustomEditors
if (instanceValues._referenceValue == null && !instanceValues.Type.IsValueType)
return;
_referenceValue = Info.GetValue(instanceValues._referenceValue);
_hasReferenceValue = true;
try
{
_referenceValue = Info.GetValue(instanceValues._referenceValue);
_hasReferenceValue = true;
}
catch
{
// Ignore error if reference value has different type or is invalid for this member
}
}
}