Created
April 3, 2019 02:58
-
-
Save dshook/335cb5fe35d62368d50033a3b1181e09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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