Search Unity

Textures on the fly

Discussion in 'Scripting' started by htwarrior, Jun 12, 2007.

  1. htwarrior

    htwarrior

    Joined:
    Feb 25, 2006
    Posts:
    40
    Hi, Is there a way of setting a texture to an object on the fly?. I have a set of textures in my project and I want to put them in a little ball according to a previous result.

    Thanks in advance
     
  2. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    Sure...

    Code (csharp):
    1. var myTexture1 : Texture2D;
    2. var myTexture2 : Texture2D;
    3.  
    4. function Update() {
    5.    if (Input.GetButton("Fire1")) {
    6.       renderer.material.mainTexture = myTexture1;
    7.    }
    8.    else {
    9.       renderer.material.mainTexture = myTexture2;
    10.    }
    11. }
    Something like that....

    --Eric
     
  3. htwarrior

    htwarrior

    Joined:
    Feb 25, 2006
    Posts:
    40
    The problem is that my input is an integer number from 1 to 90 and the textures have their names in the form "name01"..."name90". So I want to know if is possible to assign the textures without having to put them previously in the inspector.

    Thanks
     
  4. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    You could do

    Code (csharp):
    1. var myTexture : Texture2D[];
    2. var number = 42;
    3.  
    4. function Update() {
    5.    renderer.material.mainTexture = myTexture[number];
    6. }
    But with 90 textures, that's a lot of draggin' and droppin'. Have a look at Resources.Load.

    --Eric
     
  5. htwarrior

    htwarrior

    Joined:
    Feb 25, 2006
    Posts:
    40
    Thanks, I did a test with Resources.Load and it worked.

    Htwarrior