Author Topic: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position  (Read 4328 times)

adrian.tut

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Hi,

I'm trying to create a healthbar at runtime. I made a healthbar prefab, saved with the correct scale and position, and at runtime I create it with NGUITools.AddChild on a ui panel parent. This doesn't keep the scale and position however, it's always reset to default values.

What am I doing wrong? How am I supposed to do this correctly?

Thanks.
« Last Edit: December 21, 2013, 06:18:16 PM by adrian.tut »

beermoney

  • Jr. Member
  • **
  • Thank You
  • -Given: 0
  • -Receive: 2
  • Posts: 80
    • View Profile
Re: [3.0.7] NGUITools.AddChild() losing prefab scale and position
« Reply #1 on: December 21, 2013, 01:24:42 PM »
NGUITools is just a utility to save you a little typing, NGUITools.AddChild looks like this so you can see the newly created objects transforms reset to zero (or technically the identity matrix) which is usually useful
  1.         static public GameObject AddChild (GameObject parent, GameObject prefab)
  2.         {
  3.                 GameObject go = GameObject.Instantiate(prefab) as GameObject;
  4.  
  5.                 if (go != null && parent != null)
  6.                 {
  7.                         Transform t = go.transform;
  8.                         t.parent = parent.transform;
  9.                         t.localPosition = Vector3.zero;
  10.                         t.localRotation = Quaternion.identity;
  11.                         t.localScale = Vector3.one;
  12.                         go.layer = parent.layer;
  13.                 }
  14.                 return go;
  15.         }
  16.  

you could write the same function yourself and not set the local positions/scale/rotation or assign them to to that of the prefab:
  1. static public class adrian_tools{
  2.         static public GameObject AddChild (GameObject parent, GameObject prefab)
  3.         {
  4.                 GameObject go = GameObject.Instantiate(prefab) as GameObject;
  5.  
  6.                 if (go != null && parent != null)
  7.                 {
  8.                         Transform t = go.transform;
  9.                         t.parent = parent.transform;
  10.                         t.localPosition = prefab.transform.localPosition;
  11.                         t.localRotation = prefab.transform.localRotation ;
  12.                         t.localScale = prefab.transform.localScale;
  13.                         go.layer = parent.layer;
  14.                 }
  15.                 return go;
  16.         }
  17. }
  18.  


adrian.tut

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 2
    • View Profile
Re: [3.0.7] NGUITools.AddChild() losing prefab scale and position
« Reply #2 on: December 21, 2013, 06:08:43 PM »
Hi,

It's working fine now.

Thank you!

« Last Edit: December 21, 2013, 06:19:43 PM by adrian.tut »

jtms

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position
« Reply #3 on: April 17, 2014, 11:16:52 PM »
wow the code above helped me so much... thank you!

kodagames

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 8
    • View Profile
Re: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position
« Reply #4 on: September 25, 2014, 12:29:27 AM »
Very Helpful! Greatly Appreciated!