2015-10-30 00:07:17 +08:00
|
|
|
|
/// Credit Ziboo
|
|
|
|
|
/// Sourced from - http://forum.unity3d.com/threads/free-reorderable-list.364600/
|
|
|
|
|
|
|
|
|
|
using System;
|
2015-10-29 07:07:35 +08:00
|
|
|
|
using UnityEngine.Events;
|
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
namespace UnityEngine.UI.Extensions
|
2015-10-29 07:07:35 +08:00
|
|
|
|
{
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[RequireComponent(typeof(RectTransform)), DisallowMultipleComponent]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
[AddComponentMenu("UI/Extensions/Re-orderable list")]
|
|
|
|
|
public class ReorderableList : MonoBehaviour
|
|
|
|
|
{
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Tooltip("Child container with re-orderable items in a layout group")]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public LayoutGroup ContentLayout;
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Tooltip("Parent area to draw the dragged element on top of containers. Defaults to the root Canvas")]
|
|
|
|
|
public RectTransform DraggableArea;
|
2015-10-29 07:07:35 +08:00
|
|
|
|
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Tooltip("Can items be dragged from the container?")]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public bool IsDraggable = true;
|
2019-02-23 01:34:55 +08:00
|
|
|
|
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Tooltip("Should the draggable components be removed or cloned?")]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public bool CloneDraggedObject = false;
|
2015-10-29 21:12:26 +08:00
|
|
|
|
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Tooltip("Can new draggable items be dropped in to the container?")]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public bool IsDropable = true;
|
2019-02-23 01:34:55 +08:00
|
|
|
|
|
|
|
|
|
[Tooltip("Should dropped items displace a current item if the list is full?\n " +
|
|
|
|
|
"Depending on the dropped items origin list, the displaced item may be added, dropped in space or deleted.")]
|
|
|
|
|
public bool IsDisplacable = false;
|
2019-04-10 21:43:17 +08:00
|
|
|
|
|
|
|
|
|
// This sets every item size (when being dragged over this list) to the current size of the first element of this list
|
|
|
|
|
[Tooltip("Should items being dragged over this list have their sizes equalized?")]
|
2023-01-02 00:13:40 +08:00
|
|
|
|
public bool EqualizeSizesOnDrag = false;
|
2019-02-23 01:34:55 +08:00
|
|
|
|
|
2023-01-02 00:13:40 +08:00
|
|
|
|
[Tooltip("Maximum number of items this container can hold")]
|
2019-02-23 01:34:55 +08:00
|
|
|
|
public int maxItems = int.MaxValue;
|
|
|
|
|
|
2015-10-30 05:01:19 +08:00
|
|
|
|
[Header("UI Re-orderable Events")]
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public ReorderableListHandler OnElementDropped = new ReorderableListHandler();
|
2015-10-30 05:01:19 +08:00
|
|
|
|
public ReorderableListHandler OnElementGrabbed = new ReorderableListHandler();
|
|
|
|
|
public ReorderableListHandler OnElementRemoved = new ReorderableListHandler();
|
2017-05-05 21:06:52 +08:00
|
|
|
|
public ReorderableListHandler OnElementAdded = new ReorderableListHandler();
|
2019-02-23 01:34:55 +08:00
|
|
|
|
public ReorderableListHandler OnElementDisplacedFrom = new ReorderableListHandler();
|
|
|
|
|
public ReorderableListHandler OnElementDisplacedTo = new ReorderableListHandler();
|
|
|
|
|
public ReorderableListHandler OnElementDisplacedFromReturned = new ReorderableListHandler();
|
|
|
|
|
public ReorderableListHandler OnElementDisplacedToReturned = new ReorderableListHandler();
|
2019-04-10 08:35:34 +08:00
|
|
|
|
public ReorderableListHandler OnElementDroppedWithMaxItems = new ReorderableListHandler();
|
2015-10-29 07:07:35 +08:00
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
private RectTransform _content;
|
|
|
|
|
private ReorderableListContent _listContent;
|
|
|
|
|
|
|
|
|
|
public RectTransform Content
|
2015-10-29 21:12:26 +08:00
|
|
|
|
{
|
2015-10-30 00:07:17 +08:00
|
|
|
|
get
|
2015-10-29 21:12:26 +08:00
|
|
|
|
{
|
2015-10-30 00:07:17 +08:00
|
|
|
|
if (_content == null)
|
|
|
|
|
{
|
|
|
|
|
_content = ContentLayout.GetComponent<RectTransform>();
|
|
|
|
|
}
|
|
|
|
|
return _content;
|
2015-10-29 21:12:26 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 00:13:40 +08:00
|
|
|
|
public Canvas GetCanvas()
|
2016-05-24 04:30:06 +08:00
|
|
|
|
{
|
|
|
|
|
Transform t = transform;
|
|
|
|
|
Canvas canvas = null;
|
2019-02-23 01:34:55 +08:00
|
|
|
|
|
2016-05-24 04:30:06 +08:00
|
|
|
|
|
|
|
|
|
int lvlLimit = 100;
|
|
|
|
|
int lvl = 0;
|
|
|
|
|
|
|
|
|
|
while (canvas == null && lvl < lvlLimit)
|
|
|
|
|
{
|
2023-01-02 00:13:40 +08:00
|
|
|
|
if (!t.gameObject.TryGetComponent<Canvas>(out canvas))
|
2016-05-24 04:30:06 +08:00
|
|
|
|
{
|
|
|
|
|
t = t.parent;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
lvl++;
|
|
|
|
|
}
|
|
|
|
|
return canvas;
|
|
|
|
|
}
|
2019-02-23 01:34:55 +08:00
|
|
|
|
|
2018-03-31 01:00:26 +08:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Refresh related list content
|
|
|
|
|
/// </summary>
|
|
|
|
|
public void Refresh()
|
|
|
|
|
{
|
2020-10-03 04:20:17 +08:00
|
|
|
|
_listContent = ContentLayout.gameObject.GetOrAddComponent<ReorderableListContent>();
|
2018-03-31 01:00:26 +08:00
|
|
|
|
_listContent.Init(this);
|
|
|
|
|
}
|
2016-05-24 04:30:06 +08:00
|
|
|
|
|
2019-02-23 01:34:55 +08:00
|
|
|
|
private void Start()
|
2015-10-29 07:07:35 +08:00
|
|
|
|
{
|
2015-10-30 00:07:17 +08:00
|
|
|
|
if (ContentLayout == null)
|
|
|
|
|
{
|
2015-10-30 05:01:19 +08:00
|
|
|
|
Debug.LogError("You need to have a child LayoutGroup content set for the list: " + name, gameObject);
|
2015-10-30 00:07:17 +08:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
if (DraggableArea == null)
|
|
|
|
|
{
|
2015-10-30 05:01:19 +08:00
|
|
|
|
DraggableArea = transform.root.GetComponentInChildren<Canvas>().GetComponent<RectTransform>();
|
2015-10-30 00:07:17 +08:00
|
|
|
|
}
|
2015-10-30 05:01:19 +08:00
|
|
|
|
if (IsDropable && !GetComponent<Graphic>())
|
|
|
|
|
{
|
|
|
|
|
Debug.LogError("You need to have a Graphic control (such as an Image) for the list [" + name + "] to be droppable", gameObject);
|
|
|
|
|
return;
|
2016-05-24 04:30:06 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-31 01:00:26 +08:00
|
|
|
|
Refresh();
|
2015-10-29 07:07:35 +08:00
|
|
|
|
}
|
2015-10-30 00:07:17 +08:00
|
|
|
|
|
|
|
|
|
#region Nested type: ReorderableListEventStruct
|
|
|
|
|
|
|
|
|
|
[Serializable]
|
|
|
|
|
public struct ReorderableListEventStruct
|
2015-10-29 07:07:35 +08:00
|
|
|
|
{
|
2015-10-30 05:01:19 +08:00
|
|
|
|
public GameObject DroppedObject;
|
2015-10-30 00:07:17 +08:00
|
|
|
|
public int FromIndex;
|
|
|
|
|
public ReorderableList FromList;
|
|
|
|
|
public bool IsAClone;
|
|
|
|
|
public GameObject SourceObject;
|
|
|
|
|
public int ToIndex;
|
|
|
|
|
public ReorderableList ToList;
|
2017-05-05 21:06:52 +08:00
|
|
|
|
|
|
|
|
|
public void Cancel()
|
|
|
|
|
{
|
|
|
|
|
SourceObject.GetComponent<ReorderableListElement>().isValid = false;
|
|
|
|
|
}
|
2015-10-29 07:07:35 +08:00
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
#endregion
|
2015-10-29 07:07:35 +08:00
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
#region Nested type: ReorderableListHandler
|
2015-10-29 21:12:26 +08:00
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
[Serializable]
|
2023-01-02 00:13:40 +08:00
|
|
|
|
public class ReorderableListHandler : UnityEvent<ReorderableListEventStruct> { }
|
2015-10-29 21:12:26 +08:00
|
|
|
|
|
2015-10-30 05:01:19 +08:00
|
|
|
|
public void TestReOrderableListTarget(ReorderableListEventStruct item)
|
|
|
|
|
{
|
|
|
|
|
Debug.Log("Event Received");
|
|
|
|
|
Debug.Log("Hello World, is my item a clone? [" + item.IsAClone + "]");
|
|
|
|
|
}
|
|
|
|
|
|
2015-10-30 00:07:17 +08:00
|
|
|
|
#endregion
|
2015-10-29 07:07:35 +08:00
|
|
|
|
}
|
2023-01-02 00:13:40 +08:00
|
|
|
|
}
|