Author Topic: Need help integrating GUI Script into NGUI  (Read 1316 times)

Mister iOS

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
Need help integrating GUI Script into NGUI
« on: October 06, 2014, 05:25:09 PM »
hey guys, I was wondering if anyone could help me with a script, it works fine with GUI, but im trying to integrate same script into NGUI.... need help please.

Here's the script:

if(loadedLeaderBoard != null) {
         for(int i = 1; i < 10; i++) {
            GCScore score = loadedLeaderBoard.GetScore(i, GCBoardTimeSpan.ALL_TIME, diplayCollection);
            if(score != null) {
               GUI.Label(new Rect(550,  90 + 70 * i, 100, 40), i.ToString(), boardStyle);
               GUI.Label(new Rect(650, 90 + 70 * i, 100, 40), score.GetLongScore().ToString() , boardStyle);
               GUI.Label(new Rect(750, 90 + 70 * i, 100, 40), score.playerId, boardStyle);
               
               
               GameCenterPlayerTemplate player = GameCenterManager.GetPlayerById(score.playerId);
               if(player != null) {
                  GUI.Label(new Rect(850, 90 + 70 * i , 100, 40), player.alias, boardStyle);
                  if(player.avatar != null) {
                     GUI.DrawTexture(new Rect(900, 75 + 70 * i, 50, 50), player.avatar);
                  } else  {
                     GUI.Label(new Rect(900, 90 + 70 * i, 100, 40), "no photo ", boardStyle);
                  }
               }


I know i have to declare UILabel(s) and then in OnGUI() i put myUILabel.txt = whatever; I just dont know what to do with the for(int i = 1; i < 10; i++) because that part puts everything in order (its a leaderboard so it HAS to be in order) doesn't have to be up to 10 can be up to 5, but still have no idea how to do it, any help would be greatly appreciated.

PS. help with NGUI texture for player.avatar would also be appreciated :) THANK YOU IN ADVANCE!!!

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Need help integrating GUI Script into NGUI
« Reply #1 on: October 07, 2014, 10:11:30 AM »
You will never use OnGUI when working with NGUI. Never.

When you need to update some text, you would do it when you need it updated. OnGUI runs several times per frame. There is absolutely no need to update text several times per frame.

For example, when your unit gets damaged, you can change its health by simply accessing its label component and setting its value right then and there:
  1. unitHUD.GetComponent<UILabel>().text = someFloatValue + "%";
In your case, create a prefab out of what a single row should look like in your leaderboard, then create a script that will reference all the important fields -- such as player name, player score, etc. This script will go on the prefab.

When you open your window, you need to populate its content by instantiating your score item prefab several times (as many times as you have entries). GetComponent<YourCustomClass>(), then set its values, such as
  1. YourCustomClass mine = instantiatedObject.GetComponent<YourCustomClass>();
  2. mine.playerName.text = "Some Player";
  3. mine.playerScore.text = "12345";