Author Topic: How to Refresh UIGrid via code?  (Read 4828 times)

Paul1

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
How to Refresh UIGrid via code?
« on: August 17, 2014, 09:07:12 PM »
I have a function in a script that loops over a List, based on the contents decides whether to .SetActive (true)  or   .SetActive (false) icons in my UIGrid hiding the ones that should not be displayed and revealing others.

When the loop finishes I'd like to trigger the alphabetical sorting of the Grid to remove the blank spaces left behind by the inactive icons

Is there a function I can call in code to trigger this  (like right clicking on the UIGrid Script component in the inspector and selecting execute)

Thanks in advance!  :)

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: How to Refresh UIGrid via code?
« Reply #1 on: August 18, 2014, 11:41:05 AM »
UIGrid.Reposition

Paul1

  • Newbie
  • *
  • Thank You
  • -Given: 2
  • -Receive: 0
  • Posts: 5
    • View Profile
Re: How to Refresh UIGrid via code?
« Reply #2 on: August 19, 2014, 08:34:16 AM »
I love you!   ;D

hansthered

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Re: How to Refresh UIGrid via code?
« Reply #3 on: September 18, 2014, 10:02:01 PM »
I am having a similar issue. I am calling:

  1. theGrid = GetComponent<UIGrid>();
  2. theGrid.Reposition();

However, the grid does not seem to change.

I add labels like this:

  1.                 GameObject clone = (GameObject) Instantiate(playerFoodStuffsLabelPrefab, new Vector3 (0, 0, 0), Quaternion.identity);
  2.                 theGrid.AddChild(clone.transform);
  3.                 clone.transform.localScale = new Vector3 (1, 1, 1);
  4.                 newLabel = clone.gameObject.GetComponent<UILabel>();

I remove labels like this:

  1.                 kidCount = theGrid.GetChildList ().Count;
  2.  
  3.                 for(int i=1; i< kidCount; i++){
  4.                         Destroy (theGrid.GetChild(i).gameObject);
  5.                 }

The new label does seem to spawn, the foodstuffs do update, but there is a gap in the grid where it looks like another label should be.

The reason I set i=1 instead of 0 is because the first child of the grid is the original prefab object. If i set that to 0 the prefab gets deleted (which I don't want).

Please advise,

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile