您的位置:首页 > 移动开发 > Unity3D

Unity3D 动态批量加载本地图片

2017-11-06 15:00 288 查看
public Sprite[] PicObj = new Sprite[10];
private string url;
private WWW []www=new WWW[10];

private void Awake()
{

//url = Application.dataPath + "/StreamingAssets/";//本地图片
url = "D:\\Photo\\";//本地图片
//D:\Photo
for (int i = 0; i < 10; i++)
{
StartCoroutine(DownLoadPic(i));
}
}

/// <summary>
/// 加载图片
/// </summary>
/// <param name="filename"></param>
/// <returns></returns>
private IEnumerator DownLoadPic(int filename)
{
www[filename] = new WWW("file://" + url + filename.ToString()+".jpg");
Debug.Log("file://" + url + filename.ToString() + ".jpg");
while (!www[filename].isDone)
yield return www[filename];
//获取Texture
Texture2D texture = www[filename].texture;
//创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
PicObj[filename] = sprite;
//img.sprite = sprite;
//清理之前数据,释放内存
Resources.UnloadUnusedAssets();
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: