using System.Collections;
using System.IO;
using UnityEngine;
using UnityEngine.UI;
public class ImageLoader : MonoBehaviour
{
private Image targetImage; // 需要赋值图片的Image组件
void Start()
{
targetImage = GetComponent<Image>();
// 加载图片协程
StartCoroutine(LoadImage());
}
IEnumerator LoadImage()
{
string imagePath = Path.Combine(Application.streamingAssetsPath, "待机图片/待机图片.jpg");
// 使用WWW类加载图片
var www = new WWW("file://" + imagePath);
yield return www;
if (string.IsNullOrEmpty(www.error))
{
// 加载成功,将图片赋值给Image组件
targetImage.sprite = Sprite.Create(www.texture, new Rect(0, 0, www.texture.width, www.texture.height), Vector2.zero);
}
else
{
Debug.LogError("Failed to load image: " + www.error);
}
}
}
02-29
271
![](https://csdnimg.cn/release/blogv2/dist/pc/img/readCountWhite.png)
01-31