WebRequest加载streamingAssetsPath文件夹中的图片来动态修改Image的Sprite
程序员文章站
2022-04-03 22:21:17
...
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.UI;
public class WebRequest加载图片 : MonoBehaviour
{
public Image image;
public Sprite sprite;
private string path;
void Start()
{
}
IEnumerator GetText(int index)
{
switch (index)
{
case 0:
path = "file://" + Application.streamingAssetsPath + "/1.jpg";
break;
case 1:
path = "file://" + Application.streamingAssetsPath + "/2.jpg";
break;
}
//path = "file://" + Application.streamingAssetsPath + "/1.jpg";
using (UnityWebRequest uwr = UnityWebRequestTexture.GetTexture(path))
{
yield return uwr.SendWebRequest();
if (uwr.isNetworkError || uwr.isHttpError)
{
Debug.Log(uwr.error);
}
else
{
// Get downloaded asset bundle
var texture = DownloadHandlerTexture.GetContent(uwr);
sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
image.sprite = sprite;
}
}
}
public void Button0()
{
StartCoroutine(GetText(0));
}
public void Button1()
{
StartCoroutine(GetText(1));
}
上一篇: Js日期格式转换
下一篇: Unity运行平台判断