var tex = DownloadSync("file://" + path).texture; 这句话报错请更正
时间: 2024-03-16 08:46:10 浏览: 150
抱歉,我再次犯了错误。在 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;
}
```
再次感谢您的耐心和理解。
阅读全文