how to create texture for each dynamically generating gui button

how to create texture for each dynamically generating gui button

how to give texture for each button

using UnityEngine;
using System.Collections;

public class fr : MonoBehaviour {
	public string []items;
	public string selecteditems="";
	private bool editng=false;
	
	
	
 
	public Rect box;
	 
	
	
	void OnGUI()
	{
		
		
		 
		
		
		
		if(GUI.Button(box,selecteditems))
		{
			
			Debug.Log("test");
		
			editng=true;
			
			
		}
		
		 
		if(editng)
		{
			
			
			
			
			for(int x=0;x<items.Length;x++)
			{
				
				
				
				if(GUI.Button(new Rect(box.x,(box.height*x)+box.y +box.height,box.width,box.height),items[x]))
				{
					
					
					selecteditems=items[x];
					editng=false;
					Debug.Log("answer:"+selecteditems);
					if(items[x]==items[0])
					{
						
						Debug.Log("u selected item[0]");
						
					}
					if(items[x]==items[1])
					{
						
						Debug.Log("u selected item[1]");
						
					}
					if(items[x]==items[2])
					{
						
						Debug.Log("u selected item[2]");
						
					}
						if(items[x]==items[3])
					{
						
						Debug.Log("u selected item[3]");
						
					}
						if(items[x]==items[4])
					{
						
						Debug.Log("u selected item[4]");
						
					}
				}
				
			}
			
		}

 
		
		
	
	}
}

check out the image for output of this code

add the GUIStyle in the GUI.Button

public GUIStyle Stylish;

if(GUI.Button(new Rect(box.x,(box.height*x)+box.y +box.height,box.width,box.height),items[x]), Stylish)

Or generate the style dynamically :wink:

  1. Generate a small texture (say 3x3, depending of your border)
  2. Assign it to a style (GUIStyle → normal → GUIStyleState → background)
  3. Set style border to (1, 1, 1, 1)