com.unity.uiextensions/Scripts/ReorderableList/ReorderableListContent.cs

58 lines
1.8 KiB
C#
Raw Normal View History

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class ReorderableListContent : MonoBehaviour
{
private List<Transform> _cachedChildren;
private List<ReorderableListElement> _cachedListElement;
private ReorderableListElement _ele;
private ReorderableList _extList;
private RectTransform _rect;
public void OnTransformChildrenChanged()
{
StartCoroutine(RefreshChildren());
}
public void Init(ReorderableList extList)
{
_extList = extList;
_rect = GetComponent<RectTransform>();
_cachedChildren = new List<Transform>();
_cachedListElement = new List<ReorderableListElement>();
StartCoroutine(RefreshChildren());
}
private IEnumerator RefreshChildren()
{
//Handle new chilren
for (int i = 0; i < _rect.childCount; i++)
{
if (_cachedChildren.Contains(_rect.GetChild(i)))
continue;
//Get or Create ReorderableListElement
_ele = _rect.GetChild(i).gameObject.GetComponent<ReorderableListElement>() ??
_rect.GetChild(i).gameObject.AddComponent<ReorderableListElement>();
_ele.Init(_extList);
_cachedChildren.Add(_rect.GetChild(i));
_cachedListElement.Add(_ele);
}
//HACK a little hack, if I don't wait one frame I don't have the right deleted children
yield return 0;
//Remove deleted child
for (int i = _cachedChildren.Count - 1; i >= 0; i--)
{
if (_cachedChildren[i] == null)
{
_cachedChildren.RemoveAt(i);
_cachedListElement.RemoveAt(i);
}
}
}
}