- Home /
Replacing textures
Hi, so I am busy with my UI and I got stuck on this one problem.
I have 10 cars(just for testing) and there are 2 textures for each car. I have created a rectview to scroll thru the cars with the arrow keys. So I got it all working but, I need the current car that is selected to have a different texture(which is carSmallBox1 in the code). Can someone guide me to possible solution or an a idea that I can try. The link has the images if you want to test out the code(note, the textures with a 2 in front of the name like "2TestCube" is the textures for carSmallBox1[]), just set garageIsActive to true if you want to test it out. If you need me to explain anything, please ask.
Here is what it looks like.
The code: using UnityEngine; using System.Collections;
public class Garage : MonoBehaviour {
/*Create Textures*/
public Texture[] carSmallBox1 = new Texture[10];
public Texture[] carSmallBox2 = new Texture[10];
//private Texture carSmallBoxCurrent;
/*Create Rect for gui elements*/
private Rect[] carSmallBoxRect = new Rect[10];
private bool garageIsActive;
private int amountCars = 10;
/*Create variables to determine the locations of the GUI elements*/
[SerializeField] private float bottomBarCarsx; // This value will dtermine the legnth of the amount of cars in the x axis(returned as pixels)
[SerializeField] private float bottomBarCarsy; // This value will dtermine the legnth of the amount of cars in the y axis(returned as pixels)
[SerializeField] private float bottomBarCurrentx; //This value will determine which car is currently selected and to help with scrolling
private float maxBottomBarCurrentx;
private int currentCar = 0;
/*Create the needed variables to determine the size of the scroll view rect*/
private Vector2 scrollPos;
private Rect scrollRectPos;
private Rect scrollViewRect;
/*Create the need variables for screen locations and screen resolutions*/
private const float iconWidth = 70;
private const float iconHeight = 100;
private float screenWidth;
private float screenHeight;
private float nativeWidth = 1024;
private float nativeHeight = 597;
private float nativeWidthCentre = 512;
private float nativeHeightCentre = 298.5f;
private float rx;
private float ry;
void Start ()
{
screenWidth = Screen.width;
screenHeight = Screen.height;
bottomBarCarsx = nativeWidth/2;
bottomBarCarsy = nativeHeight - 150;
bottomBarCurrentx = 0;
scrollRectPos = new Rect(0, nativeHeight - 150, nativeWidth, 150);
scrollViewRect = new Rect(0, nativeHeight -150, nativeWidth +300,100);
/*Determine the rx and ry(i dont know what they stand for) for the matrix*/
rx = screenWidth/nativeWidth;
ry = screenHeight/nativeHeight;
for(int i = 0; i < amountCars; i++)
{
carSmallBoxRect[i] = new Rect(bottomBarCarsx, bottomBarCarsy, iconWidth,iconHeight);
bottomBarCarsx += 100;
}
maxBottomBarCurrentx = (amountCars*200) - 200;
scrollViewRect = new Rect(0, nativeHeight -150, bottomBarCarsx + 410,100);
}
void Update ()
{
garageIsActive = HomeScreen.test1Bool;
}
void OnGUI()
{
/*Create a new gui matrix to automatically scale all gui elemnts for any screen resolution*/
GUI.matrix = Matrix4x4.TRS (new Vector3(0, 0, 0), Quaternion.identity, new Vector3 (rx, ry, 1));
if(garageIsActive)
{
scrollPos = GUI.BeginScrollView( scrollRectPos, scrollPos, scrollViewRect);
for(int i = 0; i < amountCars; i++)
{
GUI.DrawTexture(carSmallBoxRect[i], carSmallBox2[i], ScaleMode.StretchToFill, true, 0);
}
GUI.ScrollTo(new Rect(0, nativeHeight - 150,1025 + bottomBarCurrentx, 150));
if(Input.GetKeyDown(KeyCode.RightArrow))
{
bottomBarCurrentx += 100;
if(bottomBarCurrentx > maxBottomBarCurrentx)
bottomBarCurrentx = maxBottomBarCurrentx;
currentCar ++;
}
if(Input.GetKeyDown(KeyCode.LeftArrow))
{
bottomBarCurrentx -= 100;
if(bottomBarCurrentx < 0)
bottomBarCurrentx = 0;
currentCar++;
}
GUI.EndScrollView ();
}
}
}
The link for the pictures: https://www.dropbox.com/s/1zypu1g1ofimiig/Assembly%20Bay%20Pics.zip?dl=0