Support => NGUI 3 Support => Topic started by: cybergoogle on May 05, 2014, 01:55:23 AM
Title: Randomly generate Texture array.
Post by: cybergoogle on May 05, 2014, 01:55:23 AM
I would like to create a texture array using the UITexture prefab provided with NGUI however; it's not working out. I am unfamiliar with c# and created a script using javascript that creates an array in the inspector but nothing happens.
The idea is that upon the start of the scene I would like to generate a random texture 0-31.
The texture array appears in the inspector I assign textures to all the elements 0-31 but nothing happens when I click play. I attached Mesh Renderer as I was getting compiler errors asking for that. No compiler errors but nothing happens. No textures are showing up just blank screen.
Please help. Pardon for I am a noob at programming. =(
Title: Re: Randomly generate Texture array.
Post by: r.pedra on May 05, 2014, 05:03:51 AM
If you want to affect a texture to a NGUI UITexture, you need to create a UITexture on your object an assign it with
I recommend you to use a Texture2D array instead of a Texture array.
Title: Re: Randomly generate Texture array.
Post by: cybergoogle on May 05, 2014, 05:40:40 PM
Tried that code in both javascript and c# UITexture script but did not work and gave compiler errors.
I have a UITexture prefab in the scene and I want that texture to change randomly whenever the scene is started. I am making a mobile app and when a user touches a button that opens this scene I want a texture to randomly generate I have 31 images.
Do I edit the Texture script? (UITexture)?
I created a scene without using NGUI used a plane and placed a texture on top and used the code I had above and it worked however the sizing on the ipad mini and android is off and the ngui button is off as well since there are two cameras when I use the camera looking at the textured plane and the NGUI button prefab.
Any suggestions?
Title: Re: Randomly generate Texture array.
Post by: ArenMook on May 05, 2014, 11:52:19 PM
Widgets don't have a renderer, so your renderer.material won't work. If you have a UITexture, you need to get a component of type UITexture, then change its 'mainTexture' as r.pedra mentioned. If you aren't sure how to get components in Javascript, I can only suggest going back to the very basics of Unity. I don't use Javascript myself, so I can't help you here. C# equivalent of what you're doing is:
If the "sizing is off" that's because changing the texture won't make it resize. You need to call MakePixelPerfect() to make it change its dimensions after changing the texture.
UITexture tex = GetComponent<UITexture>();
tex.MakePixelPerfect();
Title: Re: Randomly generate Texture array.
Post by: cybergoogle on May 12, 2014, 04:15:19 PM
I solved the problem by just creating a plane and adding a texture. I then attached the random texture script and just made it fit an ipad screen i'll adjust according to screen size I suppose. I'm using a ngui button but I couldn't figure out how to use the NGUI UITexture with this script. Thanks for the help anyways.