Recorded Video Training: Shop UI with Runtime Scroll Lists
Tutorial
Intermediate
1 Hour15 Mins
(19)
Summary
This recorded live training demonstrates a workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop interface where items can be bought and sold between two lists.
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 1/9: Intro and Setup.
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 2/9: Scroll View
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 3/9: Adding Buttons
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 4/9: Button Prefab
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 5/9: ShopScrollList Script
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from the collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 6/9: Adding Buttons Via Script
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 7/9: Creating Items and Testing
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 8/9: Adding Interactivity
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
In this live training we will learn the updated workflow for creating and populating scroll-lists at run time using the Scroll Rect interaction component. We will use this to create a simple two column shop where items can be bought and sold between two lists. A Scroll Rect can be used when content that takes up a lot of space needs to be displayed in a small area. The Scroll Rect provides functionality to scroll over this content.
This content is hosted by a third party provider that does not allow video views without acceptance of Targeting Cookies. Please set your cookie preferences for Targeting Cookies to yes if you wish to view videos from these providers.
Type caption for embed (optional)
Part 4/9: Button Prefab
SampleButton
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
publicclassSampleButton : MonoBehaviour {
public Button buttonComponent;
public Text nameLabel;
public Image iconImage;
public Text priceText;
private Item item;
private ShopScrollList scrollList;
// Use this for initializationvoidStart(){
buttonComponent.onClick.AddListener (HandleClick);
}
publicvoidSetup(Item currentItem, ShopScrollList currentScrollList){
item = currentItem;
nameLabel.text = item.itemName;
iconImage.sprite = item.icon;
priceText.text = item.price.ToString ();
scrollList = currentScrollList;
}
publicvoidHandleClick(){
scrollList.TryTransferItemToOtherShop (item);
}
}
ShopScrollList
using UnityEngine;
using System.Collections;
using UnityEngine.UI;
using System.Collections.Generic;
[System.Serializable]
publicclassItem
{
publicstring itemName;
public Sprite icon;
publicfloat price = 1;
}
publicclassShopScrollList : MonoBehaviour {
public List<Item> itemList;
public Transform contentPanel;
public ShopScrollList otherShop;
public Text myGoldDisplay;
public SimpleObjectPool buttonObjectPool;
publicfloat gold = 20f;
// Use this for initializationvoidStart ()
{
RefreshDisplay ();
}
voidRefreshDisplay()
{
myGoldDisplay.text = "Gold: " + gold.ToString ();
RemoveButtons ();
AddButtons ();
}
privatevoidRemoveButtons()
{
while (contentPanel.childCount > 0)
{
GameObject toRemove = transform.GetChild(0).gameObject;
buttonObjectPool.ReturnObject(toRemove);
}
}
privatevoidAddButtons()
{
for (int i = 0; i < itemList.Count; i++)
{
Item item = itemList[i];
GameObject newButton = buttonObjectPool.GetObject();
newButton.transform.SetParent(contentPanel);
SampleButton sampleButton = newButton.GetComponent<SampleButton>();
sampleButton.Setup(item, this);
}
}
publicvoidTryTransferItemToOtherShop(Item item)
{
if (otherShop.gold >= item.price)
{
gold += item.price;
otherShop.gold -= item.price;
AddItem(item, otherShop);
RemoveItem(item, this);
RefreshDisplay();
otherShop.RefreshDisplay();
Debug.Log ("enough gold");
}
Debug.Log ("attempted");
}
voidAddItem(Item itemToAdd, ShopScrollList shopList)
{
shopList.itemList.Add (itemToAdd);
}
privatevoidRemoveItem(Item itemToRemove, ShopScrollList shopList)
{
for (int i = shopList.itemList.Count - 1; i >= 0; i--)
{
if (shopList.itemList[i] == itemToRemove)
{
shopList.itemList.RemoveAt(i);
}
}
}
}
SimpleObjectPool
using UnityEngine;
using System.Collections.Generic;
// A very simple object pooling classpublicclassSimpleObjectPool : MonoBehaviour
{
// the prefab that this object pool returns instances ofpublic GameObject prefab;
// collection of currently inactive instances of the prefabprivate Stack<GameObject> inactiveInstances = new Stack<GameObject>();
// Returns an instance of the prefabpublic GameObject GetObject()
{
GameObject spawnedGameObject;
// if there is an inactive instance of the prefab ready to return, return thatif (inactiveInstances.Count > 0)
{
// remove the instance from teh collection of inactive instances
spawnedGameObject = inactiveInstances.Pop();
}
// otherwise, create a new instanceelse
{
spawnedGameObject = (GameObject)GameObject.Instantiate(prefab);
// add the PooledObject component to the prefab so we know it came from this pool
PooledObject pooledObject = spawnedGameObject.AddComponent<PooledObject>();
pooledObject.pool = this;
}
// put the instance in the root of the scene and enable it
spawnedGameObject.transform.SetParent(null);
spawnedGameObject.SetActive(true);
// return a reference to the instancereturn spawnedGameObject;
}
// Return an instance of the prefab to the poolpublicvoidReturnObject(GameObject toReturn)
{
PooledObject pooledObject = toReturn.GetComponent<PooledObject>();
// if the instance came from this pool, return it to the poolif(pooledObject != null && pooledObject.pool == this)
{
// make the instance a child of this and disable it
toReturn.transform.SetParent(transform);
toReturn.SetActive(false);
// add the instance to the collection of inactive instances
inactiveInstances.Push(toReturn);
}
// otherwise, just destroy itelse
{
Debug.LogWarning(toReturn.name + " was returned to a pool it wasn't spawned from! Destroying.");
Destroy(toReturn);
}
}
}
// a component that simply identifies the pool that a GameObject came frompublicclassPooledObject : MonoBehaviour
{
public SimpleObjectPool pool;
}
Recorded Video Training: Shop UI with Runtime Scroll Lists
Recorded Video Training: Shop UI with Runtime Scroll Lists
General Tutorial Discussion
0
0
1. Intro and Setup
0
0
2. Scroll View
0
0
3. Adding Buttons
0
0
4. Button Prefab
0
0
5. ShopScrollList Script
0
0
6. Adding Buttons Via Script
0
0
7. Creating Items and Testing
0
0
8. Adding Interactivity
0
0
9. Creating Second Shop, Q&A
0
0
Sorry, this learning content is no longer available.
This content has been removed, but don’t worry – you can still continue your learning journey with the content below, which covers many of the same topics and skills:
Sorry, this learning content is no longer available.
This content has been removed, but don’t worry – you can still continue your learning journey with the content below, which covers many of the same topics and skills: