返回从下载的数据生成的 Texture2D(只读)。
数据必须为 JPG 或 PNG 格式的图像。如果数据不是有效的图像,
则生成的纹理将是一个小的问号图像。
建议为图像的每个维度使用“2 的幂”大小;
可以使用任意大小,但使用其他大小时,加载可能会稍慢一些,
占用的内存也要稍多一些。每次调用纹理属性都会分配一个新的 Texture2D。如果您不断地下载纹理,
则必须为之前创建的纹理使用 LoadImageIntoTexture
或 Destroy。
对于 PNG 文件,如果文件中包含伽玛信息,
则向纹理应用伽玛校正。假设用于校正的伽马值为 2.0。如果文件不包含伽玛信息,
则不执行颜色校正。
JPG 文件加载为 RGB24 格式,
PNG 文件加载为 ARGB32 格式。若要对下载的图像进行 DXT 压缩,
请改为使用 LoadImageIntoTexture。
如果对象尚未下载完数据,将返回一个虚拟图像。
使用 isDone 或 yield 查看数据是否可用。
using UnityEngine; using System.Collections;
// Get the latest webcam shot from outside "Friday's" in Times Square public class ExampleClass : MonoBehaviour { public string url = "https://images.earthcam.com/ec_metros/ourcams/fridays.jpg";
IEnumerator Start() { // Start a download of the given URL using (WWW www = new WWW(url)) { // Wait for download to complete yield return www;
// assign texture Renderer renderer = GetComponent<Renderer>(); renderer.material.mainTexture = www.texture; } } }
注意:每次调用时,WWW.texture 属性都会分配一个新的 Texture2D。
因此,请始终将结果分配给局部变量,
以便稍后使用 Destroy() 将其释放掉。
调用 www.texture 将分配一个新的纹理,
但由于没有对其的本地引用,该纹理永远不会被释放。
或者,也可以使用 WWW.LoadImageIntoTexture。
Did you find this page useful? Please give it a rating:
Thanks for rating this page!
What kind of problem would you like to report?
Thanks for letting us know! This page has been marked for review based on your feedback.
If you have time, you can provide more information to help us fix the problem faster.
Provide more information
You've told us this page needs code samples. If you'd like to help us further, you could provide a code sample, or tell us about what kind of code sample you'd like to see:
You've told us there are code samples on this page which don't work. If you know how to fix it, or have something better we could use instead, please let us know:
You've told us there is information missing from this page. Please tell us more about what's missing:
You've told us there is incorrect information on this page. If you know what we should change to make it correct, please tell us:
You've told us this page has unclear or confusing information. Please tell us more about what you found unclear or confusing, or let us know how we could make it clearer:
You've told us there is a spelling or grammar error on this page. Please tell us what's wrong:
You've told us this page has a problem. Please tell us more about what's wrong:
Thank you for helping to make the Unity documentation better!
Your feedback has been submitted as a ticket for our documentation team to review.
We are not able to reply to every ticket submitted.