我当前使用的是旧版Unity,用www来加载。www加载方式可以加载本地资源和服务器上的资源。
string resUrl = "http://pic1.win4000.com/mobile/2020-02-06/5e3ba18b062a8.jpg"; //服务器上的图片地址
Texture2D pic; //下载的图片资源
Image showImg; //场景中展示的图片
//下载图片
IEnumerator LoadTextureByWeb()
{
showImg = transform.Find("webImg").GetComponent<Image>();
WWW data = new WWW(resUrl);
yield return data;
pic = data.texture;
showImg.sprite = GetSpriteFromTexture(pic);
showImg.transform.localPosition = Vector3.zero;
showImg.SetNativeSize();
SetPictureMac(showImg);
}
//加载本地资源
IEnumerator LoadTexture()
{
textures = new Texture2D[datas.Length];
sprites = new Dictionary<string, Sprite>();
for (int i = 0; i < datas.Length; ++i)
{
string path = "file://" + Application.dataPath + datas[i].url.Replace("\\", "/");
WWW www = new WWW(path);
yield return www;
if (!string.IsNullOrEmpty(www.error))
{
Debug.LogError("load png is error: " + www.error);
yield break;
}
textures[i] = www.texture;
sprites.Add(datas[i].name, GetSpriteByTexture(textures[i]));
}
本地与网页上的差别就在于地址不同,没了。