void HandleDragPerformEvent(Editor[] editors, Event evt, ref int targetIndex) { if (targetIndex != -1) { var draggingMode = DragAndDrop.GetGenericData(k_DraggingModeKey) as DraggingMode ? ; if (!draggingMode.HasValue || draggingMode.Value == DraggingMode.NotApplicable) { targetIndex = -1; return; } if (!editors[targetIndex].targets.All(t => t is Component)) { return; } var targetComponents = editors[targetIndex].targets.Cast <Component>().ToArray(); if (draggingMode.Value == DraggingMode.Script) { var scripts = DragAndDrop.objectReferences.Cast <MonoScript>(); // Ensure all script components can be added var valid = true; foreach (var targetComponent in targetComponents) { var gameObject = targetComponent.gameObject; if (scripts.Any(s => !ComponentUtility.WarnCanAddScriptComponent(gameObject, s))) { valid = false; break; } } if (valid) { Undo.IncrementCurrentGroup(); var undoGroup = Undo.GetCurrentGroup(); // Add script components var index = 0; var addedComponents = new Component[targetComponents.Length * scripts.Count()]; for (int i = 0; i < targetComponents.Length; i++) { var targetComponent = targetComponents[i]; var gameObject = targetComponent.gameObject; bool targetIsTransform = targetComponent is Transform; foreach (var script in scripts) { addedComponents[index++] = ObjectFactory.AddComponent(gameObject, script.GetClass()); } // If the target is a Transform, the AddComponent might have replaced it with a RectTransform. // Handle this possibility by updating the target component. if (targetIsTransform) { targetComponents[i] = gameObject.transform; } } // Move added components relative to target components if (!ComponentUtility.MoveComponentsRelativeToComponents(addedComponents, targetComponents, m_TargetAbove)) { // Ensure we have the same selection after calling RevertAllDownToGroup below (MoveComponentsRelativeToComponents can have opened a Prefab in Prefab Mode and changed selection to that root) var wantedSelectedGameObject = Selection.activeGameObject; // Revert added components if move operation fails (e.g. user has been shown the dialog with 'prefab instance restructuring is not posssible' or object is not editable) Undo.RevertAllDownToGroup(undoGroup); if (wantedSelectedGameObject != Selection.activeGameObject) { Selection.activeGameObject = wantedSelectedGameObject; } } } } else { // Handle dragging components var sourceComponents = DragAndDrop.objectReferences.Cast <Component>().ToArray(); if (sourceComponents.Length == 0 || targetComponents.Length == 0) { return; } MoveOrCopyComponents(sourceComponents, targetComponents, EditorUtility.EventHasDragCopyModifierPressed(evt), false); } targetIndex = -1; DragAndDrop.AcceptDrag(); evt.Use(); EditorGUIUtility.ExitGUI(); } }