Reorderable List:
- Added support for HorizontalLayout and GridLayout - Change Property RectTransform Content to LayoutGroup ContentLayout --HG-- branch : develop_5.2pull/413/head
parent
dfb423b6d0
commit
953770528d
|
@ -1,10 +1,11 @@
|
|||
using System;
|
||||
using UnityEngine;
|
||||
using UnityEngine.Events;
|
||||
using UnityEngine.UI;
|
||||
|
||||
public class ReorderableList : MonoBehaviour
|
||||
{
|
||||
public RectTransform Content;
|
||||
public LayoutGroup ContentLayout;
|
||||
|
||||
public bool IsDraggable = true;
|
||||
public RectTransform DraggableArea;
|
||||
|
@ -15,13 +16,26 @@ public class ReorderableList : MonoBehaviour
|
|||
|
||||
public ReorderableListHandler OnElementDropped = new ReorderableListHandler();
|
||||
|
||||
private RectTransform _content;
|
||||
private ReorderableListContent _listContent;
|
||||
|
||||
public RectTransform Content
|
||||
{
|
||||
get
|
||||
{
|
||||
if (_content == null)
|
||||
{
|
||||
_content = ContentLayout.GetComponent<RectTransform>();
|
||||
}
|
||||
return _content;
|
||||
}
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
if (Content == null)
|
||||
if (ContentLayout == null)
|
||||
{
|
||||
Debug.LogError("You need to set the content for the list", gameObject);
|
||||
Debug.LogError("You need to have a LayoutGroup content set for the list", gameObject);
|
||||
return;
|
||||
}
|
||||
if (DraggableArea == null)
|
||||
|
@ -29,10 +43,12 @@ public class ReorderableList : MonoBehaviour
|
|||
Debug.LogError("You need to set a draggable area for the list", gameObject);
|
||||
return;
|
||||
}
|
||||
_listContent = Content.gameObject.AddComponent<ReorderableListContent>();
|
||||
_listContent = ContentLayout.gameObject.AddComponent<ReorderableListContent>();
|
||||
_listContent.Init(this);
|
||||
}
|
||||
|
||||
#region Nested type: ReorderableListEventStruct
|
||||
|
||||
[Serializable]
|
||||
public struct ReorderableListEventStruct
|
||||
{
|
||||
|
@ -45,8 +61,14 @@ public class ReorderableList : MonoBehaviour
|
|||
public ReorderableList ToList;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Nested type: ReorderableListHandler
|
||||
|
||||
[Serializable]
|
||||
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct>
|
||||
{
|
||||
}
|
||||
|
||||
#endregion
|
||||
}
|
Binary file not shown.
|
@ -9,12 +9,16 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
private readonly List<RaycastResult> _raycastResults = new List<RaycastResult>();
|
||||
private ReorderableList _currentReorderableListRaycasted;
|
||||
private RectTransform _draggingObject;
|
||||
private ReorderableList _reorderableList;
|
||||
private LayoutElement _draggingObjectLE;
|
||||
private Vector2 _draggingObjectOriginalSize;
|
||||
private RectTransform _fakeElement;
|
||||
private LayoutElement _fakeElementLE;
|
||||
private int _fromIndex;
|
||||
private bool _isDragging;
|
||||
private RectTransform _rect;
|
||||
private ReorderableList _reorderableList;
|
||||
|
||||
#region IBeginDragHandler Members
|
||||
|
||||
public void OnBeginDrag(PointerEventData eventData)
|
||||
{
|
||||
|
@ -39,21 +43,29 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
{
|
||||
GameObject clone = Instantiate(gameObject);
|
||||
_draggingObject = clone.GetComponent<RectTransform>();
|
||||
_draggingObject.sizeDelta = gameObject.GetComponent<RectTransform>().sizeDelta;
|
||||
}
|
||||
|
||||
//Put _dragging object into the draggin area
|
||||
_draggingObjectOriginalSize = gameObject.GetComponent<RectTransform>().rect.size;
|
||||
_draggingObjectLE = _draggingObject.GetComponent<LayoutElement>();
|
||||
_draggingObject.SetParent(_reorderableList.DraggableArea, false);
|
||||
_draggingObject.SetAsLastSibling();
|
||||
|
||||
//Create a fake element for previewing placement
|
||||
_fakeElement = new GameObject("Fake").AddComponent<RectTransform>();
|
||||
_fakeElement.gameObject.AddComponent<LayoutElement>().preferredHeight = _rect.rect.height;
|
||||
_fakeElementLE = _fakeElement.gameObject.AddComponent<LayoutElement>();
|
||||
|
||||
|
||||
RefreshSizes();
|
||||
|
||||
|
||||
_isDragging = true;
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IDragHandler Members
|
||||
|
||||
public void OnDrag(PointerEventData eventData)
|
||||
{
|
||||
if (!_isDragging)
|
||||
|
@ -77,7 +89,9 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
//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
|
||||
|
@ -87,10 +101,17 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
|
||||
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>();
|
||||
float dist = Mathf.Abs(c.position.y - eventData.position.y);
|
||||
|
||||
if (_currentReorderableListRaycasted.ContentLayout is VerticalLayoutGroup)
|
||||
dist = Mathf.Abs(c.position.y - eventData.position.y);
|
||||
else if (_currentReorderableListRaycasted.ContentLayout is HorizontalLayoutGroup)
|
||||
dist = Mathf.Abs(c.position.x - eventData.position.x);
|
||||
else if (_currentReorderableListRaycasted.ContentLayout is GridLayoutGroup)
|
||||
dist = (Mathf.Abs(c.position.x - eventData.position.x) + Mathf.Abs(c.position.y - eventData.position.y));
|
||||
|
||||
if (dist < minDistance)
|
||||
{
|
||||
|
@ -99,11 +120,17 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
}
|
||||
}
|
||||
|
||||
RefreshSizes();
|
||||
_fakeElement.SetSiblingIndex(targetIndex);
|
||||
_fakeElement.gameObject.SetActive(true);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region IEndDragHandler Members
|
||||
|
||||
public void OnEndDrag(PointerEventData eventData)
|
||||
{
|
||||
_isDragging = false;
|
||||
|
@ -114,9 +141,11 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
//Put the dragged object into the content and at the right index
|
||||
if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable)
|
||||
{
|
||||
RefreshSizes();
|
||||
_draggingObject.SetParent(_currentReorderableListRaycasted.Content, false);
|
||||
_draggingObject.SetSiblingIndex(_fakeElement.GetSiblingIndex());
|
||||
|
||||
|
||||
//Send OnelementDropped Event
|
||||
_reorderableList.OnElementDropped.Invoke(new ReorderableList.ReorderableListEventStruct
|
||||
{
|
||||
|
@ -140,6 +169,7 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
//Else replace the draggedObject to his first place
|
||||
else
|
||||
{
|
||||
RefreshSizes();
|
||||
_draggingObject.SetParent(_reorderableList.Content, false);
|
||||
_draggingObject.SetSiblingIndex(_fromIndex);
|
||||
}
|
||||
|
@ -151,6 +181,26 @@ public class ReorderableListElement : MonoBehaviour, IDragHandler, IBeginDragHan
|
|||
Destroy(_fakeElement.gameObject);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
private void RefreshSizes()
|
||||
{
|
||||
Vector2 size = _draggingObjectOriginalSize;
|
||||
|
||||
if (_currentReorderableListRaycasted != null && _currentReorderableListRaycasted.IsDropable)
|
||||
{
|
||||
var firstChild = _currentReorderableListRaycasted.Content.GetChild(0);
|
||||
if (firstChild != null)
|
||||
{
|
||||
size = firstChild.GetComponent<RectTransform>().rect.size;
|
||||
}
|
||||
}
|
||||
|
||||
_draggingObject.sizeDelta = size;
|
||||
_fakeElementLE.preferredHeight = _draggingObjectLE.preferredHeight = size.y;
|
||||
_fakeElementLE.preferredWidth = _draggingObjectLE.preferredWidth = size.x;
|
||||
}
|
||||
|
||||
public void Init(ReorderableList reorderableList)
|
||||
{
|
||||
_reorderableList = reorderableList;
|
||||
|
|
Loading…
Reference in New Issue