Skip to content

Instantly share code, notes, and snippets.

@dshook
Created April 3, 2019 02:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dshook/335cb5fe35d62368d50033a3b1181e09 to your computer and use it in GitHub Desktop.
Save dshook/335cb5fe35d62368d50033a3b1181e09 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
public class ScoreChanger : MonoBehaviour {
TMP_Text tmp;
StringChanger stringChanger = new StringChanger();
float timer;
int score = 0;
void Awake(){
tmp = GetComponent<TMP_Text>();
}
void Update(){
timer += Time.deltaTime;
if((int)timer % 2 == 0){
score++;
}
stringChanger.UpdateString(tmp, nameof(score), score);
}
}
public class StringChanger {
Dictionary<string, int> prevIntValues = new Dictionary<string, int>();
Dictionary<string, float> prevFloatValues = new Dictionary<string, float>();
public void UpdateString(TMP_Text tmp, string name, int value, string format = null){
if(!prevIntValues.ContainsKey(name) || prevIntValues[name] != value){
prevIntValues[name] = value;
if(!string.IsNullOrEmpty(format)){
tmp.text = string.Format(format, value);
}else{
tmp.text = value.ToString();
}
}
}
public void UpdateString(TMP_Text tmp, string name, float value, string format = null){
if(!prevFloatValues.ContainsKey(name) || prevFloatValues[name] != value){
prevFloatValues[name] = value;
if(!string.IsNullOrEmpty(format)){
tmp.text = string.Format(format, value);
}else{
tmp.text = value.ToString();
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment