使用unitywebrequest读取文件夹下的图片

using UnityEngine;
using UnityEngine.UI;
 public class ImageManager : MonoBehaviour{    
 [SerializeField]    
 private GameObject imagePrefab;    
 void Start()    {        
 LoadManager.Ins.LoadOneFolderPicture(CreateImage, "\\狸熊");    
 }     
 public void CreateImage(Texture2D texture2D)    
 {        
 Sprite sprite = Sprite.Create(texture2D, new Rect(0, 0, texture2D.width, texture2D.height), new Vector2(0.5f, 0.5f));        
 var obj = GameObject.Instantiate(imagePrefab);        
 obj.transform.SetParent(transform);        
 obj.transform.localScale = Vector3.one;        
 Image image = obj.GetComponent<Image>();        
 image.sprite = sprite;        
 image.SetNativeSize();        
 obj.GetComponent<RectTransform>().sizeDelta = new Vector2(image.sprite.rect.width, image.sprite.rect.height) * 0.2f;        ;    } }
using System.Collections;
using System.Collections.Generic;using UnityEngine;
using System;
using UnityEngine.UI;
using UnityEngine.Networking;
using System.IO;
using System;
public class LoadManager : MonoBehaviour{    
public static LoadManager Ins;    
private string path;    
private void Awake()    
{        
Ins = this;        
path = Directory.GetCurrentDirectory()+"\\Source";    
}    
public void  LoadOnePicture(Action<Texture2D> action, string url)    
{        
StartCoroutine(LoadImage(action, path+ url));    
}    
public void LoadOneFolderPicture(Action<Texture2D> action,string url)    
{         
List<string> files = new List<string>();     
string[]  vs = Directory.GetFiles(path+url);        
foreach(string i in vs)        
{            
string tmp = Path.GetExtension(i);            
if (tmp == ".png"||tmp==".jpg")            
{                
files.Add(i);            
}        
}        
foreach(string i in files)        
{            
StartCoroutine(LoadImage(action, i));        
}    
}    
private IEnumerator LoadImage(Action<Texture2D> action,string url)    
{        
UnityWebRequest request = UnityWebRequest.Get(url);        
DownloadHandlerTexture texture = new DownloadHandlerTexture(true);        
request.downloadHandler = texture;        
yield return request.SendWebRequest();        
if (!request.isNetworkError)        
{            
action(texture.texture);        
}     
} 
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Unity中,您可以使用UnityWebRequest读取文件夹。首先,您需要将文件夹的路径传递给UnityWebRequest,然后使用UnityWebRequest读取文件夹中的内容。在此过程中,您需要使用System.IO命名空间中的类来处理文件和文件夹的读取操作。 以下是一个示例代码,展示了如何使用UnityWebRequest读取文件夹中的内容: ```csharp using System.IO; using UnityEngine; using UnityEngine.Networking; public class FolderReader : MonoBehaviour { private string folderPath = "/storage/emulated/0/"; // 文件夹路径 private void Start() { StartCoroutine(ReadFolderContents()); } private IEnumerator ReadFolderContents() { UnityWebRequest www = UnityWebRequest.Get(folderPath); yield return www.SendWebRequest(); if (www.result == UnityWebRequest.Result.Success) { string folderContents = www.downloadHandler.text; // 处理文件夹内容... } else { Debug.LogError("Failed to read folder contents: " + www.error); } } } ``` 在上述代码中,我们使用UnityWebRequest.Get()方法并传递文件夹路径作为参数。然后,我们使用www.downloadHandler.text属性来获取文件夹的内容。您可以根据需要进一步处理文件夹内容。 请注意,您需要确保文件夹路径是正确的,并且您的Unity项目有适当的权限来访问该文件夹。另外,为了使用UnityWebRequest,您需要导入命名空间using UnityEngine.Networking;<span class="em">1</span><span class="em">2</span> #### 引用[.reference_title] - *1* *2* [Unity 读取Json常用的几种方式](https://blog.csdn.net/U3DCoder/article/details/115464140)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 100%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值