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 17:22:33 浏览: 149
open_file--java-application.rar_open_open ap4 file_open jar file
这是一段 Unity3D 中用于替换图片的代码。其中 ReplaceIMG 函数接受一个 RawImage 和一个图片路径参数,会通过 DownloadSync 函数下载该路径下的图片,然后将其设置为 RawImage 的 texture。DownloadSync 函数使用 UnityWebRequest 发送一个 GET 或 POST 请求,然后通过 YieldToStop 函数阻塞协程,直到请求完成后返回 UnityWebRequest 对象。DownloadEnumerator 函数则是用于实现 YieldToStop 函数的协程函数,它会在请求完成前不断循环,每次循环等待一帧。
阅读全文