Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: harrisonpink on February 05, 2015, 01:06:27 AM

Title: Create Labels in script
Post by: harrisonpink 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?
Title: Re: Create Labels in script
Post by: devomage 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.
Title: Re: Create Labels in script
Post by: harrisonpink 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?
Title: Re: Create Labels in script
Post by: devomage 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.
Title: Re: Create Labels in script
Post by: harrisonpink on February 11, 2015, 07:59:22 PM
Thank you for this detailed reply! I will give this a try and report back.
Title: Re: Create Labels in script
Post by: harrisonpink 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?
Title: Re: Create Labels in script
Post by: devomage 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.
Title: Re: Create Labels in script
Post by: harrisonpink 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.
Title: Re: Create Labels in script
Post by: devomage 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"?
Title: Re: Create Labels in script
Post by: harrisonpink 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.
Title: Re: Create Labels in script
Post by: devomage 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".
Title: Re: Create Labels in script
Post by: harrisonpink 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?
Title: Re: Create Labels in script
Post by: devomage 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.
Title: Re: Create Labels in script
Post by: harrisonpink 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!
Title: Re: Create Labels in script
Post by: harrisonpink 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.                 }
Title: Re: Create Labels in script
Post by: astanton81 on March 19, 2015, 09:17:07 PM
I might be missing something, but why can't you sort the list prior to adding them into the game, so that when you are going through the for loop adding the items, they are already sorted?
Title: Re: Create Labels in script
Post by: harrisonpink on March 20, 2015, 12:14:00 AM
Well, I'm going to want to expand this list by allowing the user to sort by other criteria such as card cost, so id like to be able to sort the list live.

(also the solution was super simple. it's just adding the following line in the foreach loop:

  1. go.name = pickedCard.name;

I told you it had been a long time since I coded!)
Title: Re: Create Labels in script
Post by: harrisonpink on March 20, 2015, 12:59:10 AM
I've successfully got the library creating the list and alphabetizing it. I've encountered some new problems now.

If I add a UIScrollView to the parent object (temp_library) and a UIDragScrollView to the prefab that makes up the list items, I get the following error, after which nothing spawns:

"You can't place widgets on a layer different than the UIPanel that manages them.
If you want to move widgets to a different layer, parent them to a new panel instead."

I assumed this meant the depth of the objects, but that can't be the case because I have plenty of other objects all spawning on different layers, so I don't understand this error.
Title: Re: Create Labels in script
Post by: ArenMook on March 21, 2015, 09:31:08 PM
Game object layer must match. Widgets must be on the same layer as panels, as the error message tells you.

NGUI forces this behaviour.
Title: Re: Create Labels in script
Post by: harrisonpink on March 24, 2015, 12:01:28 PM
Thank you for the reply! Just to clarify, are you talking about Unity Layers? http://docs.unity3d.com/Manual/Layers.html
Title: Re: Create Labels in script
Post by: ArenMook on March 25, 2015, 12:06:36 PM
Yup.