WWW.LoadImageIntoTexture 加载图像到纹理

function LoadImageIntoTexture (tex : Texture2D) : void



Replaces the contents of an existing Texture2D with an image from the downloaded data.


The data must be an image in JPG or PNG format. If the data is not a valid image, the generated texture will be a small image of a question mark. It is recommended to use power-of-two size for each dimension of the image; arbitrary sizes will also work but can load slightly slower and take up a bit more memory.


For PNG files, gamma correction is applied to the texture if PNG file contains gamma information. Display gamma for correction is assumed to be 2.0. If file does not contain gamma information, no color correction will be performed.


This function replaces texture contents with downloaded image data, so texture size and format might change. JPG files are loaded into RGB24 format, PNG files are loaded into ARGB32 format. If texture format before calling LoadImage is DXT1 or DXT5, then the loaded image will be DXT-compressed (into DXT1 for JPG images and DXT5 for PNG images).

这个函数用下载图像数据替换纹理内容,所以纹理的大小和格式可能会改变结构的内容。 JPG文件加载为RGB24格式,PNG文件加载为ARGB32格式。如果在调用之前纹理格式的加载图像是DXT1或DXT5,那么加载的图像将被DXT压缩(DXT1为JPG图像和DXT5为PNG图像)。

If the data has not finished downloading the texture will be left untouched. Use isDone or yield to see if the data is available.


// Continuously get the latest webcam shot from outside "Friday's" in Times Square
// and DXT compress them at runtime
var url = "http://images.earthcam.com/ec_metros/ourcams/fridays.jpg";

function Start () {
	// Create a texture in DXT1 format
	renderer.material.mainTexture = new Texture2D(4, 4, TextureFormat.DXT1, false);

	while(true) {
		// Start a download of the given URL
		var www = new WWW(url); 

		// wait until the download is done
		yield www; 

		// assign the downloaded image to the main texture of the object
  if (t_load == null)   
	  Debug.Log("Application data path: " + Application.dataPath);        
	  string targetFile = "file://" + Application.dataPath + "/Resources/Textures/Icons/Chairs_65x65.png";       
	  Debug.Log("Beginning load at time: " + Time.time);       
	  t_load = new WWW(targetFile);    
  else if (t_load.isDone && t_dynamic_tx == null)   
	  Debug.Log("File has finished being loaded at " + Time.time);        
	  t_dynamic_tx = new Texture2D(65, 65);        
	  Debug.Log("Preparing to load PNG into Texture");        
	  Debug.Log("Loaded image into texture");   
Page last updated: 2011-4-25