先 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;
}
}