Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: marcg11 on February 20, 2013, 04:53:45 AM

Title: Programmatically use a UITexture/UISprite
Post by: marcg11 on February 20, 2013, 04:53:45 AM
Hello, I have a Texture2D variable with the texture to add in an empty object. The gameObject is positioned right and now I just want to create an UISprite or UITexture (really don't know which one is the best for my case) and assign somehow my texture to the UISprite/UITexture. How can I acheive that?


  1. public GameObject icon;
  2.        
  3. void Start()
  4. {
  5.         icon = transform.FindChild("icon").gameObject;
  6.         icon.AddComponent<UITexture>(); // ????
  7. }
  8. public void setSlot(Item _item)
  9. {
  10.         Texture2D tex = _item.icon;
  11.         print("filling slot");
  12.         // ...
  13. }
Title: Re: Programmatically use a UITexture/UISprite
Post by: ArenMook on February 20, 2013, 07:06:47 AM
uiTexture.mainTexture = ...
Title: Re: Programmatically use a UITexture/UISprite
Post by: Nicki on February 20, 2013, 07:16:30 AM
A UISprite gets its texture data from an atlas, while a UITexture gets it from an individual texture.

See the Widget Tool in the unity menus on how to make it. Just make a "Simple Texture" and it will set the basic one up for you.

From code, the mainTexture property is the one you want to mess with.
Title: Re: Programmatically use a UITexture/UISprite
Post by: marcg11 on February 20, 2013, 08:05:55 AM
Thanks!  ;D