- Home /
Unity3D - Ammo Texture repeat
hello everyone,
i have simple question
is there way to repeat Ammo depend on INT?
i mean for example i have
Public Int Ammo = 20;
i want to draw GUI Texture [of Small Bullets] 20 Time
and Thanks
Answer by robertbu · Aug 08, 2013 at 01:08 AM
Here is a bit of code that does what you asked for. Attach it to a game object, drag your bullet texture to the 'texture' variable, and adjust any of the other settings.
using UnityEngine;
using System.Collections;
public class RowOfIcons : MonoBehaviour {
public Texture texture;
public Vector2 startPosition = new Vector2(20,20); // Start position to display;
public int spaceBetween = 10;
public int number = 10; // Number to display;
private Rect rect;
void Start() {
rect.x = startPosition.x;
rect.y = startPosition.y;
rect.width = texture.width;
rect.height = texture.height;
}
void OnGUI() {
Rect rectT = rect;
for (int i = 0; i < number; i++) {
GUI.DrawTexture(rectT, texture);
rectT.x += texture.width + spaceBetween;
}
}
}
There are a couple of issues with this code that you should be aware of. First, since it uses GUI, you will have 1 drawcall per icon. Second, since it works in pixels, the amount of screen real estate the icons take will vary from device to device.
Answer by mactinite77 · Aug 08, 2013 at 01:28 AM
To draw a GUITexture multiple times you can use a for loop. For example :
for( int i = 0; i < Ammo; i++){
GUI.DrawTexture(new Rect(AmmoTexture.width * i,
AmmoTexture.height, AmmoTexture.width, AmmoTexture.height );
}
This will draw your bullet texture on the screen starting in the upper left corner, to change where it will draw add what you would normally put to place it to the first two fields.