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; }
时间: 2024-02-10 10:22:33 浏览: 24
这是一段 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;
}
```
再次感谢您的耐心和理解。
public void ReplaceIMG(RawImage _rawImg, string path) { try { //var tex = DownloadSync("file://" + path).texture; var tex= DownloadHandlerTexture.GetContent(DownloadSync("file://" + path)); 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; }
这段代码是用来替换一个 RawImage 的 texture,使其显示一张图片。具体实现是通过下载图片,转换成 Texture2D,再将其赋值给 RawImage 的 texture 属性。其中 DownloadSync 方法使用 UnityWebRequest 发起同步的网络请求, DownloadEnumerator 方法使用 yield 实现了异步等待网络请求完成。这样做的好处是可以保证图片下载完成后再进行后续操作,避免了图片还没有下载完成就已经被赋值给 RawImage 的 texture 属性的情况。