Пример #1
0
 public void Init(ReorderableList reorderableList)
 {
     _reorderableList = reorderableList;
     _rect            = GetComponent <RectTransform>();
 }
Пример #2
0
        public void OnDrag(PointerEventData eventData)
        {
            if (!_isDragging)
            {
                return;
            }
            if (!isValid)
            {
                CancelDrag();
                return;
            }
            //Set dragging object on cursor
            var     canvas = _draggingObject.GetComponentInParent <Canvas>();
            Vector3 worldPoint;

            RectTransformUtility.ScreenPointToWorldPointInRectangle(canvas.GetComponent <RectTransform>(), eventData.position,
                                                                    canvas.worldCamera, out worldPoint);
            _draggingObject.position = worldPoint;

            //Check everything under the cursor to find a ReorderableList
            EventSystem.current.RaycastAll(eventData, _raycastResults);
            for (int i = 0; i < _raycastResults.Count; i++)
            {
                _currentReorderableListRaycasted = _raycastResults[i].gameObject.GetComponent <ReorderableList>();
                if (_currentReorderableListRaycasted != null)
                {
                    break;
                }
            }

            //If nothing found or the list is not dropable, put the fake element outsite
            if (_currentReorderableListRaycasted == null || _currentReorderableListRaycasted.IsDropable == false)
            {
                RefreshSizes();
                _fakeElement.transform.SetParent(_reorderableList.DraggableArea, false);
            }
            //Else find the best position on the list and put fake element on the right index
            else
            {
                if (_fakeElement.parent != _currentReorderableListRaycasted)
                {
                    _fakeElement.SetParent(_currentReorderableListRaycasted.Content, false);
                }

                float minDistance = float.PositiveInfinity;
                int   targetIndex = 0;
                float dist        = 0;
                for (int j = 0; j < _currentReorderableListRaycasted.Content.childCount; j++)
                {
                    var c = _currentReorderableListRaycasted.Content.GetChild(j).GetComponent <RectTransform>();

                    if (_currentReorderableListRaycasted.ContentLayout is VerticalLayoutGroup)
                    {
                        dist = Mathf.Abs(c.position.y - worldPoint.y);
                    }
                    else if (_currentReorderableListRaycasted.ContentLayout is HorizontalLayoutGroup)
                    {
                        dist = Mathf.Abs(c.position.x - worldPoint.x);
                    }
                    else if (_currentReorderableListRaycasted.ContentLayout is GridLayoutGroup)
                    {
                        dist = (Mathf.Abs(c.position.x - worldPoint.x) + Mathf.Abs(c.position.y - worldPoint.y));
                    }

                    if (dist < minDistance)
                    {
                        minDistance = dist;
                        targetIndex = j;
                    }
                }

                RefreshSizes();
                _fakeElement.SetSiblingIndex(targetIndex);
                _fakeElement.gameObject.SetActive(true);
            }
        }