unity 本地加载图片怎么写代码
时间: 2024-04-16 19:08:02 浏览: 9
你可以使用Unity中的Sprite和Texture2D类来加载本地图片。下面是一个示例代码:
```
using UnityEngine;
using UnityEngine.UI;
using System.IO;
public class LoadImageFromLocal : MonoBehaviour {
public Image image;
void Start () {
// 图片路径
string imagePath = Application.dataPath + "/Images/Image.png"; // 修改为你的本地图片路径
// 创建Texture2D对象
Texture2D texture = new Texture2D(2, 2);
// 加载本地图片
byte[] imageData = File.ReadAllBytes(imagePath);
texture.LoadImage(imageData);
// 创建Sprite对象
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
// 将Sprite对象显示在Image组件中
image.sprite = sprite;
image.SetNativeSize();
}
}
```
你只需要将代码中的图片路径修改为你的本地图片路径即可。