Tasharen Entertainment Forum

Support => NGUI 3 Support => Topic started by: ashwanikumar04 on May 03, 2013, 08:49:43 AM

Title: Change / Add Texture on runtime in a prefab
Post by: ashwanikumar04 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 (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.
Title: Re: Change / Add Texture on runtime in a prefab
Post by: ArenMook on May 03, 2013, 09:34:57 AM
Don't create a material. UITexture has a "mainTexture" property. Set that instead.
Title: Re: Change / Add Texture on runtime in a prefab
Post by: ashwanikumar04 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?
Title: Re: Change / Add Texture on runtime in a prefab
Post by: Nicki 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.