Author Topic: NGUITools.AddChild<UITexture>(gameObject) scales wrongly  (Read 3693 times)

juhanay

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 1
    • View Profile
NGUITools.AddChild<UITexture>(gameObject) scales wrongly
« on: April 11, 2015, 05:48:46 AM »
Hi
I have a GUI that has been done with Unity GUI.DrawTexture() in scripts and it works fine, except the layer issue that GUI.DrawTexture draws on top of everything. Thus I am now transferring that GUI to NGUI, but I have run into problems.  It seems that NGUITools.AddChild() distorts the aspect ratio and the size of the original texture.  Size and scale are important to me, because my GUI consists of multiple different pieces and they need to fit together tightly. If one piece is off position or off size it shows.

Below is my code that does not work. It displays the texture but scale/position is wrong

  1. public Texture2D topleft; //This contains the original GUI.DrawTexture()  -texture. Texture type is Editor GUI or Legacy GUI
  2. public UIPanel p; //This is the panel to where I try to addChildren
  3. ...
  4. ...
  5. //The Draw Code
  6. UITexture textureToBeDrawnOnScreen = NGUITools.AddChild<UITexture>(p.gameObject);
  7. textureToBeDrawnOnScreen.mainTexture = topleft;
  8. textureToBeDrawnOnScreen.MakePixelPerfect ();
  9.  
  10. // This does not help either
  11. // textureToBeDrawnOnScreen.transform.localScale = (new Vector3 (1f, 1f, 1f));
  12. // What I am attempting  to do is  port GUI.DrawTexture(new Rect(0, 0, topleft.width, topleft.height), topleft); to NGUI.
  13.  

Any Ideas how to make this work? Or just Draw my GUI.DrawTexture() -GUI into single UITexture and display that as a single image?
Thanks in advance



 
« Last Edit: April 11, 2015, 06:04:39 AM by juhanay »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: NGUITools.AddChild<UITexture>(gameObject) scales wrongly
« Reply #1 on: April 12, 2015, 04:36:06 PM »
NGUITools.AddChild doesn't distort anything, but it will reset the game object's scale to (1, 1, 1). NGUI doesn't use the scale component, since it has Width and Height properties instead.

Try this -- start a new scene, ALT+SHIFT+T, choose your texture and click the Snap button. Does it look as you expect? If no, check the texture import settings.