using System.Collections; using System.Collections.Generic; using UnityEngine; public class ReorderableListContent : MonoBehaviour { private List _cachedChildren; private List _cachedListElement; private ReorderableListElement _ele; private ReorderableList _extList; private RectTransform _rect; public void OnTransformChildrenChanged() { StartCoroutine(RefreshChildren()); } public void Init(ReorderableList extList) { _extList = extList; _rect = GetComponent(); _cachedChildren = new List(); _cachedListElement = new List(); 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() ?? _rect.GetChild(i).gameObject.AddComponent(); _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); } } } }