Version: 2017.2

WWW.LoadImageIntoTexture

切换到手册
public void LoadImageIntoTexture (Texture2D texture);

参数

tex 要用图像数据重写的现有纹理对象。

描述

将一个现有的 Texture2D 的内容替换为来自下载数据的图像。

数据必须为 JPG 或 PNG 格式的图像。如果数据不是有效的图像, 则生成的纹理将是一个小的问号图像。 建议为图像的每个维度使用“2 的幂”大小; 可以使用任意大小,但使用其他大小时,加载可能会稍慢一些, 占用的内存也要稍多一些。

对于 PNG 文件,如果文件中包含伽玛信息, 则向纹理应用伽玛校正。假设用于校正的伽马值为 2.0。如果文件不包含伽玛信息, 则不执行颜色校正。

该函数将纹理内容替换为下载的图像数据, 因此,纹理的大小和格式可能会更改。JPG 文件加载为 RGB24 格式, PNG 文件加载为 ARGB32 格式。如果调用 LoadImage 前的纹理格式为 DXT1DXT5, 则对加载的图像进行 DXT 压缩(对于 JPG 图像,使用 DXT1;对于 PNG 图像,使用 DXT5)。

如果尚未下载完数据,纹理将保持不变。 使用 isDoneyield 查看数据是否可用。

// Add this script to a GameObject. The Start() function fetches an
// image from the documentation site.  It is then applied as the
// texture on the GameObject.

using UnityEngine; using System.Collections;

public class ExampleClass : MonoBehaviour { public string url = "https://docs.unity3d.com/uploads/Main/ShadowIntro.png";

IEnumerator Start() { Texture2D tex; tex = new Texture2D(4, 4, TextureFormat.DXT1, false); WWW www = new WWW(url); yield return www; www.LoadImageIntoTexture(tex); GetComponent<Renderer>().material.mainTexture = tex; } }