Author Topic: Searching for UILabels in an object by name?  (Read 3057 times)

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Searching for UILabels in an object by name?
« on: May 09, 2012, 01:05:32 AM »
Is there anyway to get an object in the Menu list by name? So far I have my default menu that I am spawning from a prefab. I can change the text ok but to get to the objects I am doing something like

window_name = theMenu.GetComponentsInChildren<UILabel>()[0];
main_body = theMenu.GetComponentsInChildren<UILabel>()[1];

Now this works only because of the current order of the menu and is not really that stable, if I add a button or want to change my menu, it will probably find the UILabel from somewhere else instead of what I am expecting. What I am really trying to do is something like this:-

window_name = theMenu.GetComponent("Window Name");
main_body = theMenu.GetComponent("Main Body");

Thanks for reading.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #1 on: May 09, 2012, 01:23:31 AM »
Er... what? Why don't you just set UIPopupList.items?

Edit: And if you weren't talking about the popup list... I just suggest you have a script that references the labels you need as public variables. Then you can just drag & drop your labels into those variables in inspector.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #2 on: May 09, 2012, 01:34:58 AM »
Hi ArenMook,

Sorry if my post is confusing. I have a basic UIRoot with a few thing in it that I created in the UnityGUI.
Now what I plan on doing is spawning this menu from a prefab but then adding new things into it, in code. Basically I want to make a menu navigation system where I can click on buttons, fold a menu away and then Destroy it, build a new one and fade it back in.

I am doing all the generating after the game has compiled so I won't be able to just drag and drop anything after that.
My "theMenu" is a GameObject and I am able to access all the parameters by using the GetComponentsInChildren function. Except of course when I use that, simply GetComponents in UILabel will return the order of the list that is stored at (generally not in my control) and I want to grab the items by name. This is basically like using the FindWithTag function or similiar

Clearly by your response though, I must be going about this the totally wrong way.

[edit]

Hmm, thinking about this some more, it is probably better I create all my menus in Unity using drag and drop and then just Insubstantiate and delete each menu as needed right? Not sure if this would be an efficient way of making a menu system though. I am used to making everything in code myself so this scripting in Unity is taking some getting used too.  :o
« Last Edit: May 09, 2012, 01:54:31 AM by ENAY »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #3 on: May 09, 2012, 01:45:42 PM »
Create your entire UI, all the windws, panels, etc, beforehand. Even if you plan on populating a list with something (for example populating a UITable with a list of players), you should still do that by creating a prefab for a single line first. You will be then instantiating the prefab using NGUITools.AddChild, then GetComponent() on it to modify your data as needed.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #4 on: May 09, 2012, 10:08:09 PM »
Well so far I have some menus created now. I guess I should look at UITable. What I have at the moment is a GameObject and then I simply load the menu doing the following:-

LoadedMenu = Resources.Load("Menu01") as GameObject;

and Instantiate from it by doing:-

TheMenu = GameObject.Instantiate(LoadedMenu) as GameObject;

and then when I need a new menu I simply Destroy the TheMenu Object and load a new one and Instantiate again.
Not sure if this is a good way of doing it but so far no problems.

If I had an issue, it seems painfully slow having to copy and paste a tag for every single button and text dialogue and for some unknown reason I can't edit a Menu in the Project Window, it only expands as far as 'Camera', I have to drag it into the Hierarchy window, make the modifications, delete my Project version and then drag it back in.
I decided to change my tag naming style and had to go through every single part of each menu again just to update all the fields.

Still, NGUI is still awesome and seems to working well so far.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #5 on: May 09, 2012, 10:12:28 PM »
I don't recommend using Instantiate at all. Use NGUITools.AddChild. It will parent the object and reset its scale properly for you.

ENAY

  • Full Member
  • ***
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 248
    • View Profile
Re: Searching for UILabels in an object by name?
« Reply #6 on: May 09, 2012, 10:17:18 PM »
Thanks for the tip. I will have a poke around in NGUITools.