Author Topic: Programmatically use a UITexture/UISprite  (Read 3365 times)

marcg11

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Programmatically use a UITexture/UISprite
« 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. }
« Last Edit: February 20, 2013, 06:12:34 AM by marcg11 »

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Programmatically use a UITexture/UISprite
« Reply #1 on: February 20, 2013, 07:06:47 AM »
uiTexture.mainTexture = ...

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Programmatically use a UITexture/UISprite
« Reply #2 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.

marcg11

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 4
    • View Profile
Re: Programmatically use a UITexture/UISprite
« Reply #3 on: February 20, 2013, 08:05:55 AM »
Thanks!  ;D