Ejemplo n.º 1
0
        public static Object GetAssignableObjectFromDraggedReferenceItem(PointerEventData draggingPointer, Type assignableType)
        {
            if (draggingPointer.pointerDrag == null)
            {
                return(null);
            }

            DraggedReferenceItem draggedReference = draggingPointer.pointerDrag.GetComponent <DraggedReferenceItem>();

            if (!draggedReference.IsNull() && !draggedReference.Reference.IsNull())
            {
                if (assignableType.IsAssignableFrom(draggedReference.Reference.GetType()))
                {
                    return(draggedReference.Reference);
                }
                else if (typeof(Component).IsAssignableFrom(assignableType))
                {
                    Object component = null;
                    if (draggedReference.Reference is Component)
                    {
                        component = ((Component)draggedReference.Reference).GetComponent(assignableType);
                    }
                    else if (draggedReference.Reference is GameObject)
                    {
                        component = ((GameObject)draggedReference.Reference).GetComponent(assignableType);
                    }

                    if (!component.IsNull())
                    {
                        return(component);
                    }
                }
                else if (typeof(GameObject).IsAssignableFrom(assignableType))
                {
                    if (draggedReference.Reference is Component)
                    {
                        return(((Component)draggedReference.Reference).gameObject);
                    }
                }
            }

            return(null);
        }
        private void Update()
        {
            if (draggingPointer != null)
            {
                if (draggedReference.IsNull())
                {
                    draggingPointer = null;
                }
                else if (Input.GetMouseButtonUp(0))
                {
                    ExecuteEvents.Execute(draggedReference.gameObject, draggingPointer, ExecuteEvents.endDragHandler);
                    if (EventSystem.current != null)
                    {
                        List <RaycastResult> hoveredUIElements = new List <RaycastResult>();
                        EventSystem.current.RaycastAll(draggingPointer, hoveredUIElements);

                        int i = 0;
                        while (i < hoveredUIElements.Count && ExecuteEvents.ExecuteHierarchy(hoveredUIElements[i].gameObject, draggingPointer, ExecuteEvents.dropHandler) == null)
                        {
                            i++;
                        }
                    }

                    draggingPointer = null;
                }
                else
                {
                    draggingPointer.position = Input.mousePosition;
                    ExecuteEvents.Execute(draggedReference.gameObject, draggingPointer, ExecuteEvents.dragHandler);
                }
            }
            else
            {
                if (!pointerDown)
                {
                    if (Input.GetMouseButtonDown(0) && EventSystem.current != null && !EventSystem.current.IsPointerOverGameObject())
                    {
                        RaycastHit hit;
                        if (Physics.Raycast(_camera.ScreenPointToRay(Input.mousePosition), out hit, raycastRange, interactableObjectsMask))
                        {
                            hitObject = (ProcessRaycastHit != null) ? ProcessRaycastHit(hit) : hit.collider.gameObject;
                            if (!hitObject.IsNull())
                            {
                                pointerDown     = true;
                                pointerDownTime = Time.realtimeSinceStartup;
                                pointerDownPos  = Input.mousePosition;
                            }
                        }
                    }
                }
                else
                {
                    if (Input.GetMouseButton(0))
                    {
                        if (((Vector2)Input.mousePosition - pointerDownPos).sqrMagnitude >= 100f)
                        {
                            pointerDown = false;
                        }
                        else if (Time.realtimeSinceStartup - pointerDownTime >= holdTime)
                        {
                            pointerDown = false;

                            if (!hitObject.IsNull() && EventSystem.current != null)
                            {
                                draggingPointer = new PointerEventData(EventSystem.current)
                                {
                                    pointerId     = -111,
                                    pressPosition = Input.mousePosition,
                                    position      = Input.mousePosition,
                                    button        = PointerEventData.InputButton.Left
                                };

                                draggedReference = RuntimeInspectorUtils.CreateDraggedReferenceItem(hitObject, draggingPointer, draggedReferenceSkin);
                                if (draggedReference == null)
                                {
                                    pointerDown     = false;
                                    draggingPointer = null;
                                }
                            }
                        }
                    }
                    else if (Input.GetMouseButtonUp(0))
                    {
                        pointerDown = false;
                    }
                }
            }
        }