Author Topic: Randomly generate Texture array.  (Read 5219 times)

cybergoogle

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Randomly generate Texture array.
« 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.

here is my code so far.

#pragma strict

var itemTextures : Texture[];

function Start () {

renderer.material.mainTexture = itemTextures[Random.Range(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. =(

r.pedra

  • Full Member
  • ***
  • Thank You
  • -Given: 7
  • -Receive: 20
  • Posts: 131
    • View Profile
Re: Randomly generate Texture array.
« Reply #1 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

  1. GetComponent<UITexture>().mainTexture = itemTextures[Random.Range(0, 31)];

I recommend you to use a Texture2D array instead of a Texture array.

cybergoogle

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Randomly generate Texture array.
« Reply #2 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: Randomly generate Texture array.
« Reply #3 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:
  1. using UnityEngine;
  2.  
  3. [RequireComponent(typeof(UITexture))]
  4. public class Test : MonoBehaviour
  5. {
  6.     public Texture2D textures;
  7.  
  8.     void Start ()
  9.     {
  10.         GetComponent<UITexture>().mainTexture = textures[Random.Range(0, 31)];
  11.     }
  12. }
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.
  1. UITexture tex = GetComponent<UITexture>();
  2. tex.MakePixelPerfect();

cybergoogle

  • Newbie
  • *
  • Thank You
  • -Given: 0
  • -Receive: 0
  • Posts: 3
    • View Profile
Re: Randomly generate Texture array.
« Reply #4 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.