ListManager.cs
2.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.EventSystems;
using System.IO;
using System.Collections;
using System.Collections.Generic;
// https://unity3d.com/pt/learn/tutorials/modules/beginner/live-training-archive/creating-scroll-lists-at-run-time
[System.Serializable]
public class ItemData {
public string animationName;
public Button.ButtonClickedEvent thingToDo;
}
public class ListManager : MonoBehaviour {
public GameObject sampleItemObject;
public GameObject sampleLoadingItemObject;
public string[] itemList;
private int index = 0;
private const int OFFSET = 20;
private int size = 0;
public GameObject listBlock;
public GameObject bar;
public Transform contentPanel;
public ScrollRect scrollView;
public InputField input;
private bool isLoading = false;
private GameObject loadingItem;
TrieST<string> trie;
void Start()
{
this.scrollView.onValueChanged.AddListener(checkScrollPosition);
this.input.onValueChange.AddListener(inputChanged);
// Load TrieST
{
this.trie = new TrieST<string>();
StreamReader s = new StreamReader(Application.dataPath + "/sinais.txt");
if (!s.EndOfStream) s.ReadLine();
while (!s.EndOfStream)
{
string temp = s.ReadLine();
this.trie.put(temp, temp);
}
}
this.itemList = getNamesByPrefix("");
this.index = 0;
this.size = itemList.Length;
this.loadingItem = Instantiate (sampleLoadingItemObject) as GameObject;
StartCoroutine("populateList");
}
public void checkScrollPosition(Vector2 scrollPosition)
{
if (scrollPosition.y <= 0F && ! this.isLoading)
StartCoroutine("populateList");
}
public void inputChanged(string text)
{
this.itemList = getNamesByPrefix(text.ToUpper());
this.index = 0;
this.size = itemList.Length;
this.contentPanel.DetachChildren();
foreach(GameObject go in GameObject.FindGameObjectsWithTag("clone"))
Destroy(go);
StartCoroutine("populateList");
}
private IEnumerator populateList()
{
changeLoadingState(true);
yield return new WaitForSeconds(0);
int last = this.index + OFFSET;
if (last > size) last = this.size;
for (int i = index; i < last; i++)
{
string item = itemList[i];
GameObject newButton = Instantiate (sampleItemObject) as GameObject;
SampleItem sampleItem = newButton.GetComponent<SampleItem>();
sampleItem.title.text = item;
sampleItem.GetComponent<Button>().onClick.AddListener(
delegate {
listBlock.SetActive(false);
bar.SetActive(true);
}
);
newButton.transform.SetParent(contentPanel);
//newButton.transform.SetAsFirstSibling();
}
this.index = last;
changeLoadingState(false);
}
private string[] getNamesByPrefix(string prefix)
{
Queue<string> names = this.trie.keysWithPrefix(prefix);
return names.ToArray();
}
private void changeLoadingState(bool active)
{
Debug.Log(active ? "Loading..." : "Done");
if (isLoading != active)
{
this.loadingItem.transform.SetParent(active ? contentPanel : null);
this.isLoading = active;
}
}
}