当下载大型纹理时,www.texture
会导致打嗝。
你应该尝试以下几点:
1.使用 WWW
的 LoadImageIntoTexture
函数,它将已下载数据中的图像替换为现有 Texture2D
的内容。如果问题仍未解决,请继续阅读。
WWW www = new WWW("file://" + filePath);
yield return www;
www.LoadImageIntoTexture(m_myTexture);
2.使用www.textureNonReadable
变量
使用www.textureNonReadable
代替www.texture
也可以加快加载速度。我时常看到这种情况发生。
3.使用函数Graphics.CopyTexture
从一张纹理复制到另一张纹理。这应该很快。如果问题仍未解决,请继续阅读。
//Create new Empty texture with size that matches source info
m_myTexture = new Texture2D(www.texture.width, www.texture.height, www.texture.format, false);
Graphics.CopyTexture(www.texture, m_myTexture);
4.使用Unity的UnityWebRequest
API。这个API取代了WWW
类。你必须拥有Unity 5.2或更高版本才能使用它。它具有GetTexture
函数,专门用于下载纹理。
using (UnityWebRequest www = UnityWebRequest.GetTexture("http://www.my-server.com/image.png"))
{
yield return www.Send();
if (www.isError)
{
Debug.Log(www.error);
}
else
{
m_myTexture = DownloadHandlerTexture.GetContent(www);
}
}
如果上述三个选项无法解决冻结问题,另一个解决方案是使用协程函数逐个复制像素,使用
GetPixel
和
SetPixel
函数。您可以添加计数器并设置等待时间,这样就可以将纹理复制分散到一段时间内。
5. 逐个使用
GetPixel
和
SetPixel
函数复制
Texture2D
中的像素。示例代码包括来自 NASA 的 8K 纹理以进行测试。它不会在复制
Texture
时阻塞。如果出现阻塞,请减少
copyTextureAsync
函数中
LOOP_TO_WAIT
变量的值。您还可以选择提供一个函数,在完成复制
Texture
后将调用该函数。
public Texture2D m_myTexture;
void Start()
{
StartCoroutine(downloadTexture());
}
IEnumerator downloadTexture()
{
string url = "http://eoimages.gsfc.nasa.gov/images/imagerecords/79000/79793/city_lights_africa_8k.jpg";
WWW www = new WWW(url);
yield return www;
Debug.Log("Downloaded Texture. Now copying it");
StartCoroutine(copyTextureAsync(www.texture, false, finishedCopying));
}
IEnumerator copyTextureAsync(Texture2D source, bool useMipMap = false, System.Action callBack = null)
{
const int LOOP_TO_WAIT = 400000;
int loopCounter = 0;
int heightSize = source.height;
int widthSize = source.width;
m_myTexture = new Texture2D(widthSize, heightSize, source.format, useMipMap);
for (int y = 0; y < heightSize; y++)
{
for (int x = 0; x < widthSize; x++)
{
Color tempSourceColor = source.GetPixel(x, y);
m_myTexture.SetPixel(x, y, tempSourceColor);
loopCounter++;
if (loopCounter % LOOP_TO_WAIT == 0)
{
yield return null;
}
}
}
m_myTexture.Apply();
if (callBack != null)
{
callBack.Invoke();
}
}
void finishedCopying()
{
Debug.Log("Finished Copying Texture");
}