tex | 要用图像数据重写的现有纹理对象。 |
将一个现有的 Texture2D 的内容替换为来自下载数据的图像。
数据必须为 JPG 或 PNG 格式的图像。如果数据不是有效的图像,
则生成的纹理将是一个小的问号图像。
建议为图像的每个维度使用“2 的幂”大小;
可以使用任意大小,但使用其他大小时,加载可能会稍慢一些,
占用的内存也要稍多一些。
对于 PNG 文件,如果文件中包含伽玛信息,
则向纹理应用伽玛校正。假设用于校正的伽马值为 2.0。如果文件不包含伽玛信息,
则不执行颜色校正。
该函数将纹理内容替换为下载的图像数据,
因此,纹理的大小和格式可能会更改。JPG 文件加载为 RGB24 格式,
PNG 文件加载为 ARGB32 格式。如果调用 LoadImage 前的纹理格式为
DXT1 或 DXT5,
则对加载的图像进行 DXT 压缩(对于 JPG 图像,使用 DXT1;对于 PNG 图像,使用 DXT5)。
如果尚未下载完数据,纹理将保持不变。
使用 isDone 或 yield
查看数据是否可用。
// 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); using (WWW www = new WWW(url)) { yield return www; www.LoadImageIntoTexture(tex); GetComponent<Renderer>().material.mainTexture = tex; } } }
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.