Ejemplo n.º 1
0
        public void DragDropArea <T>(
            string label, int labelSize, GUIStyle style,
            Predicate <UnityObject[]> canSetVisualModeToCopy, MouseCursor cursor,
            Action <T> onDrop, Action onMouseUp,
            float preSpace, float postSpace = 0f, float height = 0f) where T : UnityEngine.Object
        {
            using (Horizontal())
            {
                Space(preSpace);

                Box(label, style);

                var dropArea = LastRect;
                {
                    // cache the current event
                    Event currentEvent = Event.current;

                    // if our mouse isn't contained within that box area, exit out
                    if (dropArea.Contains(currentEvent.mousePosition))
                    {
                        GUIHelper.AddCursorRect(dropArea, cursor);

                        if (onMouseUp != null && currentEvent.type == EventType.MouseUp)
                        {
                            onMouseUp();
                        }

                        if (onDrop != null)
                        {
                            if (currentEvent.type == EventType.DragUpdated ||
                                currentEvent.type == EventType.DragPerform)
                            {
                                // set the visual mode to copy
                                DragAndDrop.visualMode = canSetVisualModeToCopy(DragAndDrop.objectReferences) ?
                                                         DragAndDropVisualMode.Copy : DragAndDropVisualMode.Rejected;

                                // if we dropped something
                                if (currentEvent.type == EventType.DragPerform)
                                {
                                    // register that this drag-drop event has been handled by this control
                                    DragAndDrop.AcceptDrag();

                                    // loop over the dropped items and notify onDrop of each object
                                    for (int i = 0; i < DragAndDrop.objectReferences.Length; i++)
                                    {
                                        onDrop(DragAndDrop.objectReferences[i] as T);
                                    }
                                }
                                // since we've used the DragPerform event, we'll mark it as used
                                // (its type will change to EventType.Used)
                                // so that other Controls ignore it
                                Event.current.Use();
                            }
                        }
                    }
                }
                Space(postSpace);
            }
        }