Version: 2020.1
Method group is Obsolete

WWW.texture

切换到手册
Obsolete public Texture2D texture ;

描述

返回从下载的数据生成的 Texture2D(只读)。

数据必须为 JPG 或 PNG 格式的图像。如果数据不是有效的图像, 则生成的纹理将是一个小的问号图像。 建议为图像的每个维度使用“2 的幂”大小; 可以使用任意大小,但使用其他大小时,加载可能会稍慢一些, 占用的内存也要稍多一些。每次调用纹理属性都会分配一个新的 Texture2D。如果您不断地下载纹理, 则必须为之前创建的纹理使用 LoadImageIntoTextureDestroy

对于 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 = "http://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。