Skip to content

Instantly share code, notes, and snippets.

@boj
Created November 10, 2011 22:46
Show Gist options
  • Save boj/1356512 to your computer and use it in GitHub Desktop.
Save boj/1356512 to your computer and use it in GitHub Desktop.
Unity3d Number Updater
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
public class NumberSystem : MonoBehaviour {
public GameObject numbersPrefab;
public int startPositionLeft;
public int startPositionTop;
public int numberOfDigits;
public int digitWidth;
public int digitHeight;
public int digitPadding = 2;
public Color digitColor = Color.white;
private Dictionary<int, GameObject> digitTracker = new Dictionary<int, GameObject>();
void Start() {
Vector3 position = new Vector3(startPositionLeft, startPositionTop, -50);
for (int i = 0; i < numberOfDigits; i++) {
GameObject numRef = Instantiate(numbersPrefab, position, Quaternion.identity) as GameObject;
numRef.transform.parent = transform;
// set size
numRef.GetComponent<PackedSprite>().width = digitWidth;
numRef.GetComponent<PackedSprite>().height = digitHeight;
// set color
numRef.renderer.material.color = digitColor;
// set to first animation frame
numRef.GetComponent<PackedSprite>().PlayAnim(0, 0);
numRef.GetComponent<PackedSprite>().PauseAnim();
digitTracker.Add(i, numRef);
// set padding for next number
position.x += numRef.GetComponent<PackedSprite>().width + digitPadding;
}
}
public void UpdateNumbers(int number) {
for (int i = numberOfDigits - 1; i >= 0; i--) {
int num = number % 10;
digitTracker[i].GetComponent<PackedSprite>().PlayAnim(0, num);
digitTracker[i].GetComponent<PackedSprite>().PauseAnim();
number = number / 10;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment