From 227e79c20bc71628b18899c6b1af3abb5b033e5f Mon Sep 17 00:00:00 2001 From: Christopher Date: Wed, 19 Oct 2016 15:31:31 +0100 Subject: [PATCH] Fixed Command properties not copied when copying commands #546 --- Assets/Fungus/Scripts/Editor/BlockEditor.cs | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/Assets/Fungus/Scripts/Editor/BlockEditor.cs b/Assets/Fungus/Scripts/Editor/BlockEditor.cs index 5da11b69..fab0a64b 100644 --- a/Assets/Fungus/Scripts/Editor/BlockEditor.cs +++ b/Assets/Fungus/Scripts/Editor/BlockEditor.cs @@ -906,12 +906,25 @@ namespace Fungus.EditorUtils { if (flowchart.SelectedCommands.Contains(command)) { - System.Type type = command.GetType(); + var type = command.GetType(); Command newCommand = Undo.AddComponent(commandCopyBuffer.gameObject, type) as Command; - System.Reflection.FieldInfo[] fields = type.GetFields(); - foreach (System.Reflection.FieldInfo field in fields) + var fields = type.GetFields(BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.FlattenHierarchy); + foreach (var field in fields) { - field.SetValue(newCommand, field.GetValue(command)); + // Copy all public fields + bool copy = field.IsPublic; + + // Copy non-public fields that have the SerializeField attribute + var attributes = field.GetCustomAttributes(typeof(SerializeField), true); + if (attributes.Length > 0) + { + copy = true; + } + + if (copy) + { + field.SetValue(newCommand, field.GetValue(command)); + } } } }