Author Topic: Create Labels in script  (Read 32374 times)

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Create Labels in script
« on: February 05, 2015, 01:06:27 AM »
Hi all, I've been away from NGUI and scripting as a whole for a couple of months, and I can't for the life of me figure out how to do a specific thing with NGUI.

I want to create a scroll-able list of strings from a List Array I have in code. The list needs to be able to change size based on how large the List Array is. I was thinking I could code something that said "for each item in the list array, create an NGUI label, move it X pixels downwards and fill it with the string in the List Array", but I can't remember at all how to create an NGUI Label without one already existing in the game world.

Does anyone have any advice for me here?

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #1 on: February 05, 2015, 01:20:25 AM »
use a grid and add items to the grid.  reposition when necessary.  similar results with table component...

  1. foreach (component c in list)
  2. {
  3.         GameObject go = NGUITools.AddChild(grid.gameObject, prefab);
  4. }
  5.  
  6. grid.Reposition();
  7.  
  8.  

encase the grid inside a UIScrollView and add a UIDragScrollView to each item of the grid.

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #2 on: February 05, 2015, 01:49:22 AM »
Thanks for the quick reply!

When adding a child, do I need to specify that the child is a Label? Or can I add text to a generic child object?

Also, I'd like to be able to sort alphabetically or even numerically. Is this possible with this method?
« Last Edit: February 05, 2015, 01:57:13 AM by harrisonpink »

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #3 on: February 05, 2015, 05:32:27 AM »
each child that you add is a prefab.  what is in the prefab is up to you.  generally you would have an empty game object with several components attached.

- gameobject
  - uilabel
  - uisprite
  - uilabel

- 01
  - Name
  - Image
  - Message

something like this to access the components within the loop:

  1. UILabel UILabel_Name = go.transform.FindChild("Name").GetComponent<UILabel>();
  2. UISprite UISprite_Image = go.transform.FindChild("Image").GetComponent<UISprite>();
  3.  
  4. //or
  5.  
  6. go.transform.FindChild("Name").GetComponent<UILabel>().text = "message";
  7.  
  8.  

in your case you could simply make the uilabel the prefab.

set sorting to "Alphabetic" and name each child you add accordingly.  when you reposition they will be in whatever sort scheme you design.  i tend to use object id's a lot - both for sorting and to find the row you need quickly.  alternately, you can have a "hidden label" that stores an object id and sort the grid in other ways.  not sure if this is "best practice", but it works for me.
« Last Edit: February 05, 2015, 05:49:21 AM by devomage »

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #4 on: February 11, 2015, 07:59:22 PM »
Thank you for this detailed reply! I will give this a try and report back.

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #5 on: March 18, 2015, 01:47:37 AM »
This is working well, but the grid of labels isn't sorting alphabetically. What can I do to sort the list once the foreach loop has completed?

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #6 on: March 18, 2015, 03:25:38 PM »
This is working well, but the grid of labels isn't sorting alphabetically. What can I do to sort the list once the foreach loop has completed?

generally you'd want name the root of your prefab "000", "001", "002" and then sort alphabetically.  alternately, your root object could be named by words (ie "apple", "orange", "grapes") and a hidden label inside your prefab could be an "indexid".  most the time i use database id's or the index id of a list.

  1. int index = 0;
  2.  
  3. foreach (component c in list)
  4. {
  5.     GameObject go = NGUITools.AddChild(grid.gameObject, prefab);
  6.     go.name = string.format("{0:000}", index)
  7.  
  8.     index += 1;
  9. }
  10.  
  11. grid.Reposition();
  12.  
  13.  

be sure to call grid.Reposition() once your grid is filled.

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #7 on: March 18, 2015, 04:14:34 PM »
Hmm, so there is no way to easily sort alphabetically without trickery? My list is hundreds of items long, and not in alphabetical order naturally (it's for a card game) so manually adding id numbers is a very very lengthy proposition which gets longer if new cards are released and numbers need to be shifted to accommodate them.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #8 on: March 18, 2015, 04:54:41 PM »
Hmm, so there is no way to easily sort alphabetically without trickery? My list is hundreds of items long, and not in alphabetical order naturally (it's for a card game) so manually adding id numbers is a very very lengthy proposition which gets longer if new cards are released and numbers need to be shifted to accommodate them.

i think this is common practice, rather than trickery.

give examples of your naming convention.  your labels are "8S", "2C", etc?   you are displaying "hundreds of items"?

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #9 on: March 18, 2015, 07:35:01 PM »
I'm making a list of Hearthstone cards http://www.hearthhead.com/cards

There are hundreds of them. I need to sort them alphabetically.

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #10 on: March 19, 2015, 02:33:39 AM »
I'm making a list of Hearthstone cards http://www.hearthhead.com/cards

There are hundreds of them. I need to sort them alphabetically.

it looks like you could name your root object by the name of the card?  ie "Angry Chicken", "Anima Golem".

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #11 on: March 19, 2015, 03:57:45 PM »
I will definitely give that a try. Will that allow the NGUI script to sort them properly?

devomage

  • Sr. Member
  • ****
  • Thank You
  • -Given: 7
  • -Receive: 67
  • Posts: 250
    • View Profile
Re: Create Labels in script
« Reply #12 on: March 19, 2015, 05:49:01 PM »
I will definitely give that a try. Will that allow the NGUI script to sort them properly?

if they are contained in a grid (or table) - the UIGrid has a sort alphabetically.  grid.Reposition() will re-sort the grid after adding new components.

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #13 on: March 19, 2015, 06:41:10 PM »
Ah! I didnt realize the sorting was done by prefab name instead of label string. Woops! Thank you!

harrisonpink

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 29
    • View Profile
Re: Create Labels in script
« Reply #14 on: March 19, 2015, 08:48:58 PM »
Is there a way during the AddChild code to name the child prefab? Currently I have:

  1. UIGrid tempGrid = tempLibrary.GetComponent<UIGrid>();
  2.                
  3.                 foreach (Card pickedCard in myCardCollection.cards)
  4.                 {
  5.                         GameObject go = NGUITools.AddChild(tempGrid.gameObject, tempEntry);
  6.                        
  7.                         UILabel tempEntryLabel = go.transform.FindChild("Card Label").GetComponent<UILabel>();
  8.                        
  9.                         go.transform.FindChild("Card Label").GetComponent<UILabel>().text = pickedCard.name;
  10.                        
  11.                         tempGrid.Reposition();
  12.                 }