Author Topic: Script conversion to use with NGUI.  (Read 5591 times)

Bilko

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Script conversion to use with NGUI.
« on: December 09, 2012, 07:13:35 AM »
Hey Guys;

I am not a coder by any means, as I use 3rd party visual scripting tools, so I am reaching out for help. Can someone please help me convert these two small scripts into two usable NGUI scripts?

The first one is for the players score display:

  1. //varible for points
  2. public var scorePoints :float = 0;
  3.  
  4. function OnGUI ()
  5. {      
  6.  
  7. // Score
  8.                 GUILayout.BeginArea(Rect(Screen.width  - 730 ,712,220,80));
  9.                 GUILayout.Label("" + scorePoints.ToString());
  10.         GUILayout.EndArea();
  11. }

The second one is for the Player lives display:

  1. //variable for lives
  2. public var lives :float = 3;
  3.  
  4. function OnGUI ()
  5. {      
  6. // Balls       
  7.         GUILayout.BeginArea(Rect(Screen.width  - 915 ,280,220,80));
  8.                 GUILayout.Label("" + lives.ToString());
  9.         GUILayout.EndArea();
  10. }
  11.  

I have 2 UIWidget's (UILabel) attached to the Camera in the 2D Root. The score one is called: ScorePoints The Lives one is called: BallsRemain which I would like to display the required information.

This is the last thing holding me up from releasing the game, so any and all help is greatly appreciated.

Thanks in advance.

sachaM

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: Script conversion to use with NGUI.
« Reply #1 on: December 09, 2012, 07:41:12 AM »
 ScorePoints.text=scorePoints.ToString();
BallsRemain .text=lives.ToString();

might do the trick

Bilko

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 12
    • View Profile
Re: Script conversion to use with NGUI.
« Reply #2 on: December 09, 2012, 08:32:02 AM »
Thanks for the reply.

In what context would I use that? How would that script look? These are written in JS.

I get an error: PlayerScore.js(7,1): BCE0005: Unknown identifier: 'ScorePoints'.

The script is attached here:

UIRoot (2D), Camera

The Widget is here:

UIRoot (2D), Camera, Anchor, Panel, ScorePoints or BallsRemain

Thanks again for the help.


Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Script conversion to use with NGUI.
« Reply #3 on: December 09, 2012, 09:05:13 AM »
This is pretty basic stuff.

I'm assuming you know how to setup a 2D gui with NGUI and if you don't watch some of the video tutorials.

Once this is done, you need to make two UILabels that you have referenced in your script. In your script, you can update these labels in Update() the way that sachaM says.

You really should be using C#, otherwise you have some hoops to jump through before it will work.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Script conversion to use with NGUI.
« Reply #4 on: December 09, 2012, 11:27:55 AM »
In Immediate mode GUI (OnGUI) you write your widgets in-code (via GUI or GUILayout). With a retained mode GUI (NGUI), you create your widget layout beforehand, and then you reference the components when you want to change the values. For example if you created a label, you can change its text by attaching a script to it that has code like this inside:
  1. void Start ()
  2. {
  3.     GetComponent<UILabel>().text = "Hello world!";
  4. }
Of course this function is in C#. I don't know javascript so I can't assist with that.