Author Topic: Trying to clear and repopulate a UIGrid as a response to a UIButtonMessage  (Read 5803 times)

jzaun

  • Guest
I have my gui all setup. I have a number of buttons that when clicked shows a dialog with a scrolled UIPanel containing a UIGrid. I need to clear the UIGrid and reload it depending on the UIButton that was clicked. Each button has a UIButtonMessage on it that calls a function on my "GUI Controller" object. This object has a MonoBehaviour that is listed below.

Adding and removing items is wonky. Sometimes the items are removed, sometimes they aren't. When adding items I sometimes get everything added twice.

What do I need to do to be able to dynamically change the contents of a UIGrid?

Thanks - Justin

  1. public class PlayerGuiController : MonoBehaviour {
  2.        
  3.         public UIPanel panelStoreCategories;
  4.         public UIPanel panelStoreItems;
  5.         public UIGrid  gridStoreItems;
  6.         public GameObject prefabSaleItem;
  7.        
  8.         // Use this for initialization
  9.         void Start () {
  10.                 CloseAll ();   
  11.         }
  12.  
  13.  
  14.         public void CloseAll() {
  15.                 panelStoreCategories.gameObject.SetActive(false);
  16.                 panelStoreItems.gameObject.SetActive(false);
  17.                 StartCoroutine(RemoveAllSaleItems());
  18.         }
  19.  
  20.        
  21.         public void ShowTreasure() {
  22.                 CloseAll();
  23.                 UILabel label = panelStoreItems.transform.FindChild("Title").GetComponent<UILabel>();
  24.                 label.text = "Shop: Treasure";
  25.                 panelStoreItems.gameObject.SetActive(true);
  26.  
  27.                 List<Dictionary<string,string>> items = ConfigManager.Instance.treasureItems;
  28.                 foreach(Dictionary<string,string> item in items) {
  29.                         NewSaleItem(item["title"]);
  30.                 }
  31.                 gridStoreItems.Reposition();
  32.                 UIScrollBar sb = panelStoreItems.transform.FindChild("Scroll Bar").GetComponent<UIScrollBar>();
  33.                 sb.scrollValue = 0;
  34.         }
  35.        
  36.        
  37.         public void ShowResources() {
  38.                 CloseAll();
  39.                 UILabel label = panelStoreItems.transform.FindChild("Title").GetComponent<UILabel>();
  40.                 label.text = "Shop: Resources";
  41.                 panelStoreItems.gameObject.SetActive(true);
  42.  
  43.                 List<Dictionary<string,string>> items = ConfigManager.Instance.resourceItems;
  44.                 foreach(Dictionary<string,string> item in items) {
  45.                         NewSaleItem(item["title"]);
  46.                 }
  47.                 gridStoreItems.Reposition();
  48.                 UIScrollBar sb = panelStoreItems.transform.FindChild("Scroll Bar").GetComponent<UIScrollBar>();
  49.                 sb.scrollValue = 0;
  50.         }
  51.  
  52.        
  53.         private IEnumerator RemoveAllSaleItems() {
  54.                 foreach(Transform child in gridStoreItems.transform) {
  55.                     child.parent = null;
  56.                         DestroyImmediate(child.gameObject);
  57.                 }
  58.                 yield return new WaitForEndOfFrame();
  59.         }
  60.        
  61.        
  62.         private void NewSaleItem(string name) {
  63.                 GameObject item = NGUITools.AddChild(gridStoreItems.gameObject, prefabSaleItem);
  64.                 gridStoreItems.Reposition();
  65.                 UILabel label = item.transform.FindChild("Label").GetComponent<UILabel>();
  66.                 label.text = name;
  67.         }
  68. }
  69.  

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trying to clear and repopulate a UIGrid as a response to a UIButtonMessage
« Reply #1 on: February 06, 2013, 11:55:21 AM »
Use NGUITools.Destroy instead. It unparents the transform properly. Destroy call does not.

jzaun

  • Guest
Re: Trying to clear and repopulate a UIGrid as a response to a UIButtonMessage
« Reply #2 on: February 06, 2013, 12:13:41 PM »
I've figured out the duplicate entries on add, I had an error in the datafile loader.

The removal of items is till problematic though. I've switched to NGUITools.Destroy() and I've tried with and without de-parenting the child before the destroy. The odd thing is the items that are left aren't de-parented when I leave the line it. Is the foreach loop effected by the removal of the children? Should I just use a for loop and access the children in reverse order? It just seems odd as some, but not all, items are removed.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Trying to clear and repopulate a UIGrid as a response to a UIButtonMessage
« Reply #3 on: February 06, 2013, 10:05:59 PM »
Yes, you need to remove them in a reverse order. Removing items while iterating through the list is... bad.

jzaun

  • Guest
Re: Trying to clear and repopulate a UIGrid as a response to a UIButtonMessage
« Reply #4 on: February 06, 2013, 10:59:07 PM »
Yup that did it. Thanks for the speedy help!  :)