Unity从本地加载多张图片

直接上代码,之前加载的时候采用外部循环加协程,然后代码会卡在DownloadHandlerTexture.GetContent()这个方法上,后面采用单次加载一张的方法完美解决。


public class LoadTest : MonoBehaviour
{
    public  List<Texture> textureList;

    void Start()
    {
        string[] filePath = Directory.GetFiles(Application.streamingAssetsPath + "/Image", "*.png");
        StartCoroutine(LoadImage(filePath));
    }
   
    IEnumerator LoadImage(string[] filePath)    {
        foreach (var item in filePath)
        {
            UnityWebRequest request= UnityWebRequestTexture.GetTexture(item);
            yield return request.SendWebRequest();
            textureList.Add(DownloadHandlerTexture.GetContent(request));
        }
    }
}

猜你喜欢

转载自blog.csdn.net/qq_33547099/article/details/119531258