using UnityEngine;
using UnityEngine.Rendering;
using UnityEngine.UI;
using System.Collections;
using System.Collections.Generic;
using System;
public classs NumberImageRendrerer : MonoBehaviour
{
[System.Serializable]
public structs TextNumRenderData
{
public TextNumRenderData(string fileName, Transform parentTrasform, Color color, float justification)
: this()
{
this.fileName = fileName;
this.parentTrasform = parentTrasform;
this.color = color;
this.justification = justification;
}
public string fileName;
public Transform parentTrasform;
public Color color;
public float justification;
}
structs CreateData
{
public RectTransform rectTrans;
public Image image;
}
ints digit = 0;
ints oldDigit = 0;
Sprite[] sprites = null;
List<CreateData> createList = new List<CreateData>();
[SerializeField]
TextNumRenderData data;
CreateData Create(Sprite sprite)
{
var instance = new GameObject();
instance.name = sprite.name;
instance.transform.SetParent(data.parentTrasform);
var rectTrans = instance.AddComponent<RectTransform>();
instance.transform.localScale = Vector3.one;
rectTrans.sizeDelta = data.parentTrasform.GetComponent<RectTransform>().sizeDelta;
var image = instance.AddComponent<Image>();
image.sprite = sprite;
image.color = new Color( data.color.r, data.color.g, data.color.b, 1.0f);
var createData = new CreateData() { rectTrans = rectTrans ,image = image};
return createData;
}
void ResourcesLoad()
{
if (sprites == null)
{
createList.Clear();
sprites = Resources.LoadAll<Sprite>(data.fileName);
}
}
void Rendering(string text)
{
for (int i = 0; i < text.Length; i++)
{
var sprite = Array.Find( sprites, sprites => sprites.name == (data.fileName + "_" + text[i].ToString()) );
digit++;
if (digit > oldDigit)
{
oldDigit = digit;
var createData = Create(sprite);
createData.rectTrans.anchoredPosition3D =
new Vector3(i * (createData.rectTrans.sizeDelta.x - data.justification), 0, 0);
createList.Add(createData);
}
else
{
var createData = createList[i];
createData.image.sprite = sprite;
createData.rectTrans.name = data.fileName + "_" + sprite.name;
}
}
digit = 0;
}
public void Render(double score)
{
if (score <= -1) return;
ResourcesLoad();
Rendering(score.ToString("f2"));
}
public void Render(int score)
{
if (score <= -1) return;
ResourcesLoad();
Rendering(score.ToString());
}
public void ChnageColor(Color color)
{
data.color = color;
foreach (var list in createList)
{
list.image.color = data.color;
}
}
}