public static WWW DownloadSync(string path, WWWForm form = null) { WWW www; if (form != null) www = new WWW(path, form); else www = new WWW(path); YieldToStop(www); return www; } private static void YieldToStop(WWW www) { var @enum = DownloadEnumerator(www); while (@enum.MoveNext()) ; } private static IEnumerator DownloadEnumerator(WWW www) { while (!www.isDone) { yield return null; } // Texture2D texture = www.texture; //// texture.filterMode = FilterMode.Point; // texture.Apply(); yield return www; }
时间: 2024-02-14 14:24:19 浏览: 27
这段代码是用来下载文件的,其中DownloadSync函数用来同步下载文件,参数path是要下载的文件路径,form是表单数据,返回值是一个WWW对象。YieldToStop函数用来等待下载完成,参数是WWW对象。DownloadEnumerator函数是一个协程函数,用来异步下载文件,参数是WWW对象。在协程中,通过while循环等待下载完成,然后返回WWW对象。在下载完成后,可以通过www.texture获取下载的图片。
相关问题
public void ReplaceIMG(RawImage _rawImg, string path) { print("ReplaceIMGwww"); try { var tex = DownloadSync("file://" + path).texture; if (tex == null) return; tex.wrapMode = TextureWrapMode.Clamp; float width = tex.width; float height = tex.height; _rawImg.texture = tex; _rawImg.SetNativeSize(); tex = null; } catch (Exception e) { Debug.Log(e.Message); } } public static UnityWebRequest DownloadSync(string path, WWWForm form = null) { UnityWebRequest www; if (form != null) www = UnityWebRequest.Post(path, form); else www = UnityWebRequest.Get(path); www.SendWebRequest(); YieldToStop(www); return www; } private static void YieldToStop(UnityWebRequest www) { var @enum = DownloadEnumerator(www); while (@enum.MoveNext()) ; } private static IEnumerator DownloadEnumerator(UnityWebRequest www) { while (!www.isDone) { yield return null; } yield return www; }
这是一段 Unity3D 中用于替换图片的代码。其中 ReplaceIMG 函数接受一个 RawImage 和一个图片路径参数,会通过 DownloadSync 函数下载该路径下的图片,然后将其设置为 RawImage 的 texture。DownloadSync 函数使用 UnityWebRequest 发送一个 GET 或 POST 请求,然后通过 YieldToStop 函数阻塞协程,直到请求完成后返回 UnityWebRequest 对象。DownloadEnumerator 函数则是用于实现 YieldToStop 函数的协程函数,它会在请求完成前不断循环,每次循环等待一帧。
var tex = DownloadSync("file://" + path).texture; 这句话报错请更正
抱歉,我再次犯了错误。在 IEnumerator 函数中,不能直接访问 UnityWebRequest 中的 texture 属性,因为 UnityWebRequest 并没有这个属性。可以使用 DownloadHandlerTexture 类来处理下载的纹理,并将其作为 UnityWebRequest 的结果返回。因此,需要将代码中的这一行:
var tex = DownloadSync("file://" + path).texture;
改为:
var tex = DownloadSync("file://" + path);
yield return tex;
var texture = DownloadHandlerTexture.GetContent(www);
然后,将 DownloadSync 函数中的这一行:
yield return www;
改为:
yield return new DownloadHandlerTexture();
以下是更正后的代码:
```
public void ReplaceIMG(RawImage _rawImg, string path)
{
print("ReplaceIMGwww");
try
{
var tex = DownloadSync("file://" + path);
yield return tex;
var texture = DownloadHandlerTexture.GetContent(tex);
if (texture == null)
return;
texture.wrapMode = TextureWrapMode.Clamp;
float width = texture.width;
float height = texture.height;
_rawImg.texture = texture;
_rawImg.SetNativeSize();
texture = null;
}
catch (Exception e)
{
Debug.Log(e.Message);
}
}
public static IEnumerator DownloadSync(string path, WWWForm form = null)
{
UnityWebRequest www;
if (form != null)
www = UnityWebRequest.Post(path, form);
else
www = UnityWebRequest.Get(path);
www.downloadHandler = new DownloadHandlerTexture();
www.SendWebRequest();
yield return YieldToStop(www);
}
private static object YieldToStop(UnityWebRequest www)
{
var @enum = DownloadEnumerator(www);
while (@enum.MoveNext()) ;
return @enum.Current;
}
private static IEnumerator DownloadEnumerator(UnityWebRequest www)
{
while (!www.isDone)
{
yield return null;
}
yield return www;
}
```
再次感谢您的耐心和理解。