- Home /
Rewriting a bullet count GUI to run more efficiently
ive written this code to create individual bullet icons on the screen space. Each time the weapon is fired via an Input the bullets are removed one at a time. My question is what is a better way to write this code. Im obviously new to scripting and dont quite understand how to smash code down to make it efficient.
#pragma strict
var shell : Texture;
var grayShell : Texture;
var numBullets : int = bulletTotal;
var bulletTotal : int = 7;
var screenPosX : int = 565;
var screenPosY : int = 1040;
function OnGUI () {
/*
for (var i = 0; i < numBullets; i++)
GUI.DrawTexture ( Rect (800, 550 + i * shell.height, shell.width, shell.height) , shell, ScaleMode.ScaleAndCrop, true);
*/
GUI.Box (Rect (869, 500, 200, 100), "ammoCounter");
if (bulletTotal > 0){ //Gray Shells
GUI.DrawTexture (Rect ( screenPosY, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 1){
GUI.DrawTexture (Rect ( screenPosY -20, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 2){
GUI.DrawTexture (Rect ( screenPosY -40, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 3){
GUI.DrawTexture (Rect ( screenPosY -60, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 4){
GUI.DrawTexture (Rect ( screenPosY -80, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 5){
GUI.DrawTexture (Rect ( screenPosY -100, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 6){
GUI.DrawTexture (Rect ( screenPosY -120, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
if (bulletTotal > 7){
GUI.DrawTexture (Rect ( screenPosY -140, screenPosX, grayShell.width, grayShell.height), grayShell, ScaleMode.ScaleToFit, true);
}
}
}
}
}
}
}
}
if (numBullets > 0){
GUI.DrawTexture (Rect ( screenPosY, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 1){
GUI.DrawTexture (Rect ( screenPosY -20, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 2){
GUI.DrawTexture (Rect ( screenPosY -40, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 3){
GUI.DrawTexture (Rect ( screenPosY -60, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 4){
GUI.DrawTexture (Rect ( screenPosY -80, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 5){
GUI.DrawTexture (Rect ( screenPosY -100, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 6){
GUI.DrawTexture (Rect ( screenPosY -120, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
if (numBullets > 7){
GUI.DrawTexture (Rect ( screenPosY -140, screenPosX, shell.width, shell.height), shell, ScaleMode.ScaleToFit, true);
}
}
}
}
}
}
}
}
}
the gray shells part is only to show an empty.
Thank you in advance for the help
Answer by ScroodgeM · Aug 21, 2012 at 10:41 PM
#pragma strict
var shell : Texture; var grayShell : Texture; var numBullets : int = bulletTotal; var bulletTotal : int = 7; var screenPosX : int = 565; var screenPosY : int = 1040;
function OnGUI () { GUI.Box (Rect (869, 500, 200, 100), "ammoCounter"); for (var i = 0; i < bulletTotal; i++) { DrawTexture(i, numBullets > i ? shell : grayShell); } }
function DrawTexture(i : int, t : Texture2D) { GUI.DrawTexture (Rect ( screenPosY - 20 * i, screenPosX, t.width, t.height), t, ScaleMode.ScaleToFit, true); }
Your answer
Follow this Question
Related Questions
gui list problem 2 Answers
GUI texture touch input problem 1 Answer
Updating GUI text with GetButtonDown? 1 Answer
how to input words from array to GUI? 0 Answers