protected static void DuplicateBlock(object obj) { Flowchart flowchart = GetFlowchart(); Block block = obj as Block; Vector2 newPosition = new Vector2(block.nodeRect.position.x + block.nodeRect.width + 20, block.nodeRect.y); Block oldBlock = block; Block newBlock = FlowchartWindow.CreateBlock(flowchart, newPosition); newBlock.blockName = flowchart.GetUniqueBlockKey(oldBlock.blockName + " (Copy)"); Undo.RecordObject(newBlock, "Duplicate Block"); foreach (Command command in oldBlock.commandList) { if (ComponentUtility.CopyComponent(command)) { if (ComponentUtility.PasteComponentAsNew(flowchart.gameObject)) { Command[] commands = flowchart.GetComponents <Command>(); Command pastedCommand = commands.Last <Command>(); if (pastedCommand != null) { pastedCommand.itemId = flowchart.NextItemId(); newBlock.commandList.Add(pastedCommand); } } // This stops the user pasting the command manually into another game object. ComponentUtility.CopyComponent(flowchart.transform); } } if (oldBlock.eventHandler != null) { if (ComponentUtility.CopyComponent(oldBlock.eventHandler)) { if (ComponentUtility.PasteComponentAsNew(flowchart.gameObject)) { EventHandler[] eventHandlers = flowchart.GetComponents <EventHandler>(); EventHandler pastedEventHandler = eventHandlers.Last <EventHandler>(); if (pastedEventHandler != null) { pastedEventHandler.parentBlock = newBlock; newBlock.eventHandler = pastedEventHandler; } } } } }
protected static void DuplicateBlock(object obj) { Flowchart flowchart = GetFlowchart(); Block block = obj as Block; Vector2 newPosition = new Vector2(block.nodeRect.position.x + block.nodeRect.width + 20, block.nodeRect.y); Block oldBlock = block; Block newBlock = FlowchartWindow.CreateBlock(flowchart, newPosition); newBlock.blockName = flowchart.GetUniqueBlockKey(oldBlock.blockName + " (Copy)"); Undo.RecordObject(newBlock, "Duplicate Block"); foreach (Command command in oldBlock.commandList) { System.Type type = command.GetType(); Command newCommand = Undo.AddComponent(flowchart.gameObject, type) as Command; System.Reflection.FieldInfo[] fields = type.GetFields(); foreach (System.Reflection.FieldInfo field in fields) { field.SetValue(newCommand, field.GetValue(command)); } newCommand.itemId = flowchart.NextItemId(); newBlock.commandList.Add(newCommand); } if (oldBlock.eventHandler != null) { EventHandler eventHandler = oldBlock.eventHandler; System.Type type = eventHandler.GetType(); EventHandler newEventHandler = Undo.AddComponent(flowchart.gameObject, type) as EventHandler; System.Reflection.FieldInfo[] fields = type.GetFields(); foreach (System.Reflection.FieldInfo field in fields) { field.SetValue(newEventHandler, field.GetValue(eventHandler)); } newEventHandler.parentBlock = newBlock; newBlock.eventHandler = newEventHandler; } }