unity 加载streamingAsset目录下的图片
时间: 2024-05-16 17:11:56 浏览: 4
可以通过以下代码实现:
```csharp
using UnityEngine;
using UnityEngine.UI;
public class LoadImageFromStreamingAssets : MonoBehaviour
{
public string imageName;
void Start()
{
StartCoroutine(LoadImage());
}
IEnumerator LoadImage()
{
string imagePath = Application.streamingAssetsPath + "/" + imageName;
Texture2D tex;
#if UNITY_ANDROID && !UNITY_EDITOR
WWW www = new WWW(imagePath);
yield return www;
tex = www.texture;
#else
byte[] fileData = System.IO.File.ReadAllBytes(imagePath);
tex = new Texture2D(2, 2);
tex.LoadImage(fileData);
#endif
GetComponent<Image>().sprite = Sprite.Create(tex, new Rect(0, 0, tex.width, tex.height), new Vector2(0.5f, 0.5f));
}
}
```