Author Topic: Change / Add Texture on runtime in a prefab  (Read 4230 times)

ashwanikumar04

  • Guest
Change / Add Texture on runtime in a prefab
« on: May 03, 2013, 08:49:43 AM »
Hello All:

I have a UIGrid which I am populating on run time using prefabs. In the prefab I have picture (UITexture) which I populate with byte array received from Web. The Picture is not shown on the prefabs but other details are shown which I populate.

I am using following code
  1.  WWW t_load = BundledAssets.GetBundledAsset ("sample.png");
  2.  Texture2D img = new Texture2D (2, 2);
  3.  t_load.LoadImageIntoTexture (img);
  4.  var localGuiTexture =item.gameObject.GetComponent<UITexture>();       
  5.  localGuiTexture.material = new Material (Shader.Find("Unlit/Transparent Colored"));
  6.  localGuiTexture.material.mainTexture = img;

I even tried removing the UITexture from the prefab and adding UITexture on runtime as mentioned http://www.tasharen.com/forum/index.php?topic=86.msg424#msg424, but no luck

Can any body tell me what is wrong here or any other approach to show image in the prefab.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Change / Add Texture on runtime in a prefab
« Reply #1 on: May 03, 2013, 09:34:57 AM »
Don't create a material. UITexture has a "mainTexture" property. Set that instead.

ashwanikumar04

  • Guest
Re: Change / Add Texture on runtime in a prefab
« Reply #2 on: May 03, 2013, 11:31:34 AM »
I have tried that also, but the image is coming only for some prefabs. Is there any other approach of showing images in a prefab?

Nicki

  • Global Moderator
  • Hero Member
  • *****
  • Thank You
  • -Given: 33
  • -Receive: 141
  • Posts: 1,768
    • View Profile
Re: Change / Add Texture on runtime in a prefab
« Reply #3 on: May 04, 2013, 08:19:51 PM »
Are you on the newest version of NGUI?

If not, try enabling depth pass and disabling again immediately after populating the UITexture to force it to update.

Use .mainTexture to put the texture directly in, and have the UITexture on the prefab be entirely empty so there's no material attached already as this tends to make it act a little weirdly, at least on old versions.