先 public RawImage test;
在Unity内创建rawimage(所选取的图片会赋到其材质上)
void LoadTexture()
{
OpenFileDialog openFileDialog = new OpenFileDialog();
//过滤器
openFileDialog.Filter = "JPG|*.jpg|PNG图片|*.png";
//当显示了Dialogue窗口之后 点击确认后执行if函数
if(openFileDialog.ShowDialog() == DialogResult.OK)
{
string texPath = openFileDialog.FileName;
Debug.Log(texPath);
Texture2D tex2d = new Texture2D(1, 1);
//将图片转为二进制存储
byte[] texByte=File.ReadAllBytes(texPath);
//从二进制形式加载出图片
tex2d.LoadImage(texByte);
//将加载出的图片赋给test的材质
test.texture = tex2d;
}
}

本文介绍了一种在Unity中通过代码方式加载本地图片文件,并将其显示在一个Raw Image组件上的方法。具体步骤包括使用C#代码打开文件对话框选择图片、读取图片的二进制数据、将二进制数据转换为Texture2D对象,最后将该纹理赋给Raw Image组件。
4832

被折叠的 条评论
为什么被折叠?



