Tanoda
ReorderableListContent.cs
Go to the documentation of this file.
1
3
4using System.Collections;
5using System.Collections.Generic;
6
8{
9 public class ReorderableListContent : MonoBehaviour
10 {
11 private List<Transform> _cachedChildren;
12 private List<ReorderableListElement> _cachedListElement;
13 private ReorderableListElement _ele;
14 private ReorderableList _extList;
15 private RectTransform _rect;
16
17 private void OnEnable()
18 {
19 if(_rect)StartCoroutine(RefreshChildren());
20 }
21
22
24 {
25 if(this.isActiveAndEnabled)StartCoroutine(RefreshChildren());
26 }
27
28 public void Init(ReorderableList extList)
29 {
30 _extList = extList;
31 _rect = GetComponent<RectTransform>();
32 _cachedChildren = new List<Transform>();
33 _cachedListElement = new List<ReorderableListElement>();
34
35 StartCoroutine(RefreshChildren());
36 }
37
38 private IEnumerator RefreshChildren()
39 {
40 //Handle new children
41 for (int i = 0; i < _rect.childCount; i++)
42 {
43 if (_cachedChildren.Contains(_rect.GetChild(i)))
44 continue;
45
46 //Get or Create ReorderableListElement
47 _ele = _rect.GetChild(i).gameObject.GetComponent<ReorderableListElement>() ??
48 _rect.GetChild(i).gameObject.AddComponent<ReorderableListElement>();
49 _ele.Init(_extList);
50
51 _cachedChildren.Add(_rect.GetChild(i));
52 _cachedListElement.Add(_ele);
53 }
54
55 //HACK a little hack, if I don't wait one frame I don't have the right deleted children
56 yield return 0;
57
58 //Remove deleted child
59 for (int i = _cachedChildren.Count - 1; i >= 0; i--)
60 {
61 if (_cachedChildren[i] == null)
62 {
63 _cachedChildren.RemoveAt(i);
64 _cachedListElement.RemoveAt(i);
65 }
66 }
67 }
68 }
69}
Credit Erdener Gonenc - @PixelEnvision.