Author Topic: Instances of prefabs spawning in the center of screen (AddChild + localPosition)  (Read 4263 times)

Corestacks

  • Guest
- I have created a UIPanel prefab called "prefab_score" which contains a child UILabel.
- I have then called the function below to spawn an instance of the prefab at a specified location, but all the instances spawn at the same location centered in the screen, however in the inspector, if i click on each individual instance, the position seems to be different. I am aware I need to use localPosition and AddChild.. and i am moving the UIPanel, not the widget... What am i doing wrong?

public void ShowScoreAt(Vector3 pos)
{
    go_instance = NGUITools.AddChild(GameObject.Find("Anchor").gameObject, prefab_score);
    go_instance.transform.localPosition = new Vector3(pos.x, pos.y, -1);             
    go_instance.GetComponentInChildren<UILabel>().text = "x" + points.ToString

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
First of all, never use GameObject.Find. Ever. Instead, in your script set a public GameObject variable that you will drag & drop from inspector, or create a singleton attached to the object in question. GameObject.Find is SLOOOOOOOOOOOOOWWWWWWWW.

Second, what you are doing is fine. You use AddChild to position the object below the anchor, and then you adjust its local position, which is in screen coordinates (if the root is pixel perfect). I'd double check the "pos" you're passing in, and make sure the UI panel isn't marked as "static".

Corestacks

  • Guest
I am very aware of not using FIND, it was just for prototype + sample illustration purposes :)

I have checked the position passed to the method is the correct world coordinates, i get coordinates like:

(1.0, 3.4, 1.0)
(2.0, 3.4, 1.0)
(3.0, 3.4, 1.0)

The parent UIPanel is not set as static either.
My UIRoot was set to FixedSize, from what I read I need this to be FixedSize so it displays the correct size for different mobile resolutions. Off topic.. should I use FixedSize or FixedSize on Mobile? What is the difference?

I have tried setting the UIroot to PixelPerfect but still not working. The prefab appears centered in the screen and every instance stacks up on top of each other at the same position. However, when I click on the prefab in the scene, the position shown are (1.0, 3.4, 1.0), (2.0, 3.4, 1.0), (3.0, 3.4, 1.0) which seems to be correct but why isnt it spawning on those positions?

I have spent too much time on this and dont see what I have done wrong.. I really need help on this, thanks!

Corestacks

  • Guest
I have placed the prefab panel into the UIRoot Anchor, and observed the position displayed under the inspector when dragged horizontally goes from -170 to 170... what kind of coordinate is this using? The world coordinate should only be from 0 to 6.. I have the feeling that the coordinate system differs. Hence (1.0, 3.4, 1.0), (2.0, 3.4, 1.0), (3.0, 3.4, 1.0) is stacking on top of each other. Please enlighten me with what kind of transformation I need to use... worldtoviewport? worldtoscreen?

NOTE: I am using 2 cameras. The NGUI Camera just for the UI, and the main camera for the 3d object (this is where I am passing the object's 3d world position to the AddChild method).

Do I need to convert the position from world to screen relative to the main camera, and then from screen back to world relative to the NGUI camera?

SOLVED: Ok after much digging i found the answer, hope it helps y'all and avoid wasting time like i did:

Vector3 pos = mGameCamera.WorldToViewportPoint(target.position);
transform.position = mUICamera.ViewportToWorldPoint(pos);
« Last Edit: September 03, 2013, 08:05:50 PM by Corestacks »