using System.IO;
using UnityEngine;
public class DataReader : MonoBehaviour
{
private string filePath;
private string[] lines;
private Sprite sprite;
private string imagePath;
private Texture2D texture;
public void UpdateUIText(string inputCode)
{
filePath = Application.streamingAssetsPath + "/Excel/你的excel.csv"; // 替换为你的CSV文件路径
lines = File.ReadAllLines(filePath);
foreach (string line in lines)
{
string[] data = line.Split(',');
string code = data[0]; // 获取识别码
string value = data[1]; // 获取值
if (code == inputCode)
{
xx类.Instance.UItext.text = value; // 更新UI的Text值
break;
}
}
}
public void UpdateUIImage(string inputCode)
{
imagePath = Application.streamingAssetsPath + "/图片所在的streamingAssets子文件夹/" + inputCode+ ".png";
LoadLocalTexture(imagePath);
if (texture != null)
{
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), Vector2.zero);
}
if (sprite!=null)
{
// 将加载的图片赋值给UI Image组件的Source Image字段
xx类.Instance.UIImage.sprite = sprite;
xx类.Instance.UIImage.color = new Color(255, 255, 255, 255);
}
}
private Texture2D LoadLocalTexture(string path)
{
try
{
byte[] bytes = File.ReadAllBytes(path);
texture = new Texture2D(2, 2);
if (texture.LoadImage(bytes))
{
return texture;
}
else return null;
}
catch (System.Exception)
{
if(texture!=null) Destroy(texture);
if (sprite != null) Destroy(sprite);
return null;
}
}
}
unity通过StreamingAssets 从excel表获取text值和获取指定图片改变UI中的图片
猜你喜欢
转载自blog.csdn.net/weixin_42301988/article/details/131910750
今日推荐
周排行