Skip to content

Instantly share code, notes, and snippets.

@N-Carter
Created August 31, 2014 21:55
Show Gist options
  • Select an option

  • Save N-Carter/aa404305449f7cbd0e58 to your computer and use it in GitHub Desktop.

Select an option

Save N-Carter/aa404305449f7cbd0e58 to your computer and use it in GitHub Desktop.
UIScrollList
using UnityEngine;
using UnityEngine.Events;
using UnityEngine.UI;
using System.Collections;
public class UIScrollList : MonoBehaviour
{
[SerializeField] protected RectTransform m_RowPrefab;
[SerializeField] protected ScrollRect m_ScrollRect;
[SerializeField] protected UITest m_UITest;
[System.Serializable]
protected class Row
{
public string text;
public int quantity;
}
[SerializeField] protected Row[] m_Rows;
protected void Start()
{
// Remove placeholder(s):
var content = m_ScrollRect.content;
foreach(Transform child in content)
Destroy(child.gameObject);
var rowTransform = m_RowPrefab.GetComponent<RectTransform>();
float rowHeight = rowTransform.sizeDelta.y;
Vector2 contentSize = content.sizeDelta;
for(int i = 0; i < m_Rows.Length; ++i)
{
var row = m_Rows[i];
var rowInstance = (RectTransform)Instantiate(m_RowPrefab, content.TransformPoint(new Vector3(0.0f, i * -rowHeight, 0.0f)), content.rotation);
rowInstance.parent = content;
rowInstance.localScale = Vector3.one;
rowInstance.sizeDelta = new Vector2(contentSize.x, rowHeight);
var button = rowInstance.Find("Tool Kind Button/Text");
button.GetComponent<Text>().text = row.text;
var buttonClickedEvent = new Button.ButtonClickedEvent();
int j = i;
buttonClickedEvent.AddListener(() => Debug.Log("Button " + j));
button.transform.parent.GetComponent<Button>().onClick = buttonClickedEvent;
rowInstance.Find("Panel/Quantity Text").GetComponent<Text>().text = row.quantity.ToString();
}
content.sizeDelta = new Vector2(contentSize.x, m_Rows.Length * rowHeight);
content.localPosition = Vector3.zero;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment