I need create hud on weapon (display ammo)
Can you help me how to do it ?
Example:
I need create hud on weapon (display ammo)
Can you help me how to do it ?
Example:
Option 1:
Use this UnityWiki Script you can create a sprite sheet attached to an object to loop through your sprite sheet.
The script was all messed up when I went to that link I reformatted it and cleaned it up for easier reading.
You could hook into this script with a trigger to update the index, set it manually or do anything else you wanted to do
public class AnimatedTextureExtendedUV : MonoBehaviour {
//vars for the whole sheet
public int colCount = 4;
public int rowCount = 4;//vars for animation
public int rowNumber = 0; //Zero Indexed
public int colNumber = 0; //Zero Indexed
public int totalCells = 4;
public int fps = 10;
//Maybe this should be a private var
private Vector2 offset;
//Update
void Update () { SetSpriteAnimation(colCount,rowCount,rowNumber,colNumber,totalCells,fps); }
//SetSpriteAnimation
void SetSpriteAnimation(int colCount ,int rowCount ,int rowNumber ,int colNumber,int totalCells,int fps ){
// Calculate index
int index = (int)(Time.time * fps);
// Repeat when exhausting all cells
index = index % totalCells;
// Size of every cell
float sizeX = 1.0f / colCount;
float sizeY = 1.0f / rowCount;
Vector2 size = new Vector2(sizeX,sizeY);
// split into horizontal and vertical index
var uIndex = index % colCount;
var vIndex = index / colCount;
// build offset
// v coordinate is the bottom of the image in opengl so we need to invert.
float offsetX = (uIndex+colNumber) * size.x;
float offsetY = (1.0f - size.y) - (vIndex + rowNumber) * size.y;
Vector2 offset = new Vector2(offsetX,offsetY);
renderer.material.SetTextureOffset ("_MainTex", offset);
renderer.material.SetTextureScale ("_MainTex", size);
}
}
Options 2:
Create an empty game object. Add a mesh. Texture the mesh an appropriate background texture (in your image above it would be the blue backer). Add a 3d text object for current bullet count, and a 3d text for out of bullet count. Create a prefab and attach this script to your ammo counter object.
Extended Node: You might need this shader from the unity wiki Shader script
public class AmmoCounterScript : MonoBehaviour {
public TextMesh AmmoLeftMesh;
public TextMesh AmmoInMagMesh; // Include any other text you want in the sam fashion
public int AmmoLeft = 0;
public int AmmoInMag = 0;
public bool DidChangeAmmoCount = false;
void Update () {
// Not sure if this is really an optimization but it doesn't hurt
if (DidChangeAmmoCount) {
AmmoLeftMesh.Text = "" + AmmoLeft;
AmmoInMagMesh.Text = "" + AmmoInMag;
DidChangeAmmoCount = false;
}
}
public void ChangeAmmoLeft(int newValue) {
AmmoLeft = newValue;
DidChangeAmmoCount = true;
}
public void ChangeAmmoInMag(int newValue) {
AmmoInMag = newValue;
DidChangeAmmoCount = true;
}
public void DecrementAmmLeft() {
AmmoLeft--;
DidChangeAmmoCount = true;
}
}