Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: adrian.tut on December 21, 2013, 11:44:38 AM

Title: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position
Post by: adrian.tut on December 21, 2013, 11:44:38 AM
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.
Title: Re: [3.0.7] NGUITools.AddChild() losing prefab scale and position
Post by: beermoney 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.  

Title: Re: [3.0.7] NGUITools.AddChild() losing prefab scale and position
Post by: adrian.tut on December 21, 2013, 06:08:43 PM
Hi,

It's working fine now.

Thank you!

Title: Re: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position
Post by: jtms on April 17, 2014, 11:16:52 PM
wow the code above helped me so much... thank you!
Title: Re: [3.0.7] [fixed] NGUITools.AddChild() losing prefab scale and position
Post by: kodagames on September 25, 2014, 12:29:27 AM
Very Helpful! Greatly Appreciated!