Author Topic: Making buttons in-script  (Read 3803 times)

bionicnacho

  • Guest
Making buttons in-script
« on: January 28, 2013, 02:35:42 PM »
Hello!

I am doing game saving and i got stuck on something.

I can save the game just fine. So i have a list of all the game saves. I want a window to open game. It would list all of the saved games and let me click on them to open it.

With unity's default GUI, i would use
  1.     for(var sg in LevelSerializer.SavedGames[LevelSerializer.PlayerName]) {
  2.        if(GUILayout.Button(sg.Caption)) {
  3.          sg.Load();
  4.          Time.timeScale = 1;
  5.          }
  6.     }

How could i do something like this with NGUI?

What i thought i could do was make the "for" make new buttons (like the unity gui).

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Making buttons in-script
« Reply #1 on: January 28, 2013, 02:43:22 PM »
Create a button for how a single saved game entry would look like, save it as a prefab, and NGUITools.AddChild-instantiate it at run-time as many times as you want. For added ease-of-use, add it as a child of a game object that has a UIGrid on it. It will reposition the children automatically for you, acting as a GUILayout.

bionicnacho

  • Guest
Re: Making buttons in-script
« Reply #2 on: January 28, 2013, 02:49:59 PM »
Is there any document that shows how to use NGUITools.AddChild?

I have no idea on how to use that.
« Last Edit: January 29, 2013, 05:25:56 PM by bionicnacho »

bionicnacho

  • Guest
Re: Making buttons in-script
« Reply #3 on: January 29, 2013, 05:52:25 PM »
-bump-

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Making buttons in-script
« Reply #4 on: January 29, 2013, 05:53:26 PM »
Serious?

NGUITools.AddChild(parent, prefab).

bionicnacho

  • Guest
Re: Making buttons in-script
« Reply #5 on: January 29, 2013, 05:54:23 PM »
Well, i'm sorry for asking! I'm new to this NGUI stuff.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Making buttons in-script
« Reply #6 on: January 29, 2013, 05:58:20 PM »
Rule of thumb... try using something you're trying to figure out, then post a question if you don't understand it.

Especially for really simple stuff like this. You could have at least looked at the function by going to its definition.
  1. /// <summary>
  2. /// Instantiate an object and add it to the specified parent.
  3. /// </summary>
  4.  
  5. static public GameObject AddChild (GameObject parent, GameObject prefab)
...or checked the script documentation for the function:

http://www.tasharen.com/ngui/docs/class_n_g_u_i_tools.html#af58290856540b1f25b682f5f3ae00baa