ListManager.cs 2.97 KB
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;
		}
	}
	
}