Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: Siddharth3322 on February 03, 2014, 11:02:09 PM

Title: Multiple UILabel Creation At Runtime
Post by: Siddharth3322 on February 03, 2014, 11:02:09 PM
For my 2d game, I want to create Score Scene which display 10 high scores with player names and points they have.
Basically for title purpose I have created sprite for "Player Name" and "Score" but for actual data displaying I want to use UILabel.

So how to achieve this? For this I have two concepts in mind.

But for second approach I have to give then particular position so this thing become frustrating me. For each widget I set anchors type unified and for all side set to current position so it always keep its position regarding aspect ratio change.

I want help in creation of score scene. What is the better way to create this?
Title: Re: Multiple UILabel Creation At Runtime
Post by: PoN on February 03, 2014, 11:22:33 PM
1. Create prefab with UILabels which you need.
2. In for* loop - Use NGUITools.AddChild() to add your prefab with labels to UIGrid or UITable;
Title: Re: Multiple UILabel Creation At Runtime
Post by: ArenMook on February 04, 2014, 02:51:47 AM
If you are positioning things yourself, why are you using anchors?
Title: Re: Multiple UILabel Creation At Runtime
Post by: Siddharth3322 on February 05, 2014, 04:11:48 AM
If you are positioning things yourself, why are you using anchors?

Because when click on play button with Maximize on Play, all widget don't maintain its position that I set at design time.

  1. GameObject panelObj = GameObject.Find ("Panel");
  2.                 GameObject playerLblInstance = NGUITools.AddChild (panelObj, playerNamePref);
  3.                 Vector3 playerLblPosition = playerLblInstance.transform.position;
  4.                 playerLblPosition.x = pX;
  5.                 playerLblPosition.y = pY;
  6.                 playerLblInstance.transform.position = playerLblPosition;

At present I used above code for creating labels run time. They don't maintain its position on actual resolution.
For that I want to set anchor type to unified and set all side to set to current position. Manually I can able to do all things but through scripting how to achieve this?
Title: Re: Multiple UILabel Creation At Runtime
Post by: ArenMook on February 05, 2014, 02:55:01 PM
UIRect.SetAnchor functions lets you set the anchor. You can also look inside them to see what they actually do inside.

If you want the anchors to use the widget's current position instead of setting it all up yourself, use UIRect.AnchorPoint's SetVertical or SetHorizontal functions. For example, if "leftAnchor.target" is the widget's parent:
  1. widget.leftAnchor.SetHorizontal(widget.leftAnchor.target, widget.localCorners[0].x);
The first value is the anchor target. The second value is the current difference in position between the widget and the target. Since the target is the parent, it's a simple local position difference. I use widget.localCorners[0] because that gives me the widget's bottom-left corner, regardless of the widget's pivot.
Title: Re: Multiple UILabel Creation At Runtime
Post by: Siddharth3322 on February 06, 2014, 12:51:44 AM
Thanks for all this detailed reply. I found difficulty in implementing that.

I write following code to set anchor in created label.
  1. playerLblInstance.GetComponent<UILabel> ().leftAnchor.SetHorizontal (panelObj.transform, panelObj.GetComponent<UIPanel> ().localCorners [0].x);

But I found following exception in that

Quote
NullReferenceException
  at (wrapper managed-to-native) UnityEngine.Transform:INTERNAL_get_position (UnityEngine.Vector3&)

  at UnityEngine.Transform.get_position () [0x00000] in C:\BuildAgent\work\d3d49558e4d408f4\artifacts\EditorGenerated\UnityEngineTransform.cs:27

  at UIRect+AnchorPoint.SetHorizontal (UnityEngine.Transform parent, Single localPos) [0x0005a] in D:\unity\AddictiveBirdHunting\Assets\NGUI\Scripts\Internal\UIRect.cs:76

  at ScoreSceneScript.AddLabel (Single pX, Single pY) [0x00040] in D:\unity\AddictiveBirdHunting\Assets\Scripts\ScoreSceneScript.cs:38

  at ScoreSceneScript.AddPlayerNames () [0x0001e] in D:\unity\AddictiveBirdHunting\Assets\Scripts\ScoreSceneScript.cs:21

  at ScoreSceneScript.Start () [0x00000] in D:\unity\AddictiveBirdHunting\Assets\Scripts\ScoreSceneScript.cs:10
 
(Filename: Assets/NGUI/Scripts/Internal/UIRect.cs Line: 76)

I need further help from your side.
Title: Re: Multiple UILabel Creation At Runtime
Post by: ArenMook on February 06, 2014, 10:12:33 PM
That error tells you that transform.position is where the problem occurs, suggesting that 'transform' is null. Add some null checks to your code.