Skip to content

Instantly share code, notes, and snippets.

@haramakoto
Last active January 25, 2023 02:29
Show Gist options
  • Save haramakoto/8436c105a72a28f98c6a6bc6dcec555f to your computer and use it in GitHub Desktop.
Save haramakoto/8436c105a72a28f98c6a6bc6dcec555f to your computer and use it in GitHub Desktop.
画像で数字を表示する(3桁)
namespace com.etc.utility
{
using UnityEngine;
public class ImageNumViewer : MonoBehaviour
{
[SerializeField]
private ImageNumViewUnit[] numViewUnits;
private int currentNum;
public void ShowNum(int num)
{
this.numViewUnits[1].GetComponent<CanvasGroup>().alpha = num < 10 ? 0 : 1;
this.numViewUnits[2].GetComponent<CanvasGroup>().alpha = num < 100 ? 0 : 1;
for (int i = 0; i < num.ToString().Length; i++)
{
int currentDigits_ = this.GetNthDigitNum(this.currentNum, i + 1);
int newDigits = this.GetNthDigitNum(num, i + 1);
if (newDigits != currentDigits_)
{
this.numViewUnits[i].ShowNum(newDigits);
}
}
this.currentNum = num;
}
private void Start()
{
this.ShowNum(0);
}
private int GetNthDigitNum(int num, int digit)
{
int currentDigitNum = 1;
num = System.Math.Abs(num);
while (num > 0)
{
if (currentDigitNum == digit)
{
return num % 10;
}
num /= 10;
currentDigitNum++;
}
return 0;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment