请先在Assets目录下面新建StreamingAssets文件夹
在StreamingAssets文件夹建立一个图片目录,把需要加载的图片拖放进该目录
path = Application.streamingAssetsPath + “/图片目录/图片1.png”;
public IEnumerator TextureReader(string path, UnityAction<Texture2D> action)
{
UnityWebRequest unityWebRequest = UnityWebRequestTexture.GetTexture(path);
yield return unityWebRequest.SendWebRequest();
if (unityWebRequest.error != null)
Debug.Log(unityWebRequest.error);
else
{
byte[] bts = unityWebRequest.downloadHandler.data;
if (action != null)
{
action(DownloadHandlerTexture.GetContent(unityWebRequest));
}
}
}
使用方法
StartCoroutine(TextureReader(img_path,
(t2d) =>
{
myImage.overrideSprite = Sprite.Create(t2d, new Rect(0, 0, t2d.width, t2d.height), Vector2.zero);
}));