Search Unity

array list textures

Discussion in 'Immediate Mode GUI (IMGUI)' started by jsromero27, Feb 6, 2017.

  1. jsromero27

    jsromero27

    Joined:
    Jan 22, 2015
    Posts:
    4
    Hello,

    I have a problem with creating buttons from an arraylist.

    Here is some sample code to show my problem:

    //////////////////////////////////////////////////
    Texture2D[ ] tex ;
    .
    ( code to insert textures into tex)
    .
    voidOnGUI(){

    for(int i =0; i < tex.Length; i++)
    (GUILayout.Button(tex[ i ]));

    }

    ///////////////////////////////////////////////

    The problem is I get the correct number of buttons created,however they all share the texture name, so I end up with "n" number of buttons all with texture "n".

    For example if my array contained textures [A,B,C], I would see the buttons being created in the following order:
    A -> BB -> CCC

    How could I define each button to have its own texture from the arraylist?

    Thank you.
     
  2. PsyKaw

    PsyKaw

    Joined:
    Aug 16, 2012
    Posts:
    102
    Code (csharp):
    1.  
    2. void OnGUI()
    3. {
    4.     for(int i =0; i < tex.Length; i++)
    5.     {
    6.         string texStr = tex[i];
    7.         if(GUILayout.Button(texStr))
    8.         {
    9.             // your button code here
    10.         }
    11.     }
    12. }
    13.  
    Try something like this (not tested). You have to declare a local variable to avoid i incrementation during "for" loop.
     
  3. jsromero27

    jsromero27

    Joined:
    Jan 22, 2015
    Posts:
    4

    Thank you for this, I found out I was doing a for loop twice so I was getting more values entered into my array than needed.

    I managed to fix that and now get the correct number of buttons and each has its own texture, however the next texture deletes the previous texture, so I end up with for example 5 blank buttons and one with the texture of the last texture in my array.

    I did try to create a string and texture created from my text [ i ] array but no luck.