Unity 异步加载网络头像
程序员文章站
2022-03-02 11:48:24
...
public void LoadByWWW(string str, Image imgHead)
{
StartCoroutine(Load(str, imgHead));
}
// ImageFormat
IEnumerator Load(string str, Image imgHead)
{
double startTime = (double)Time.time;
//请求WWW
WWW www = new WWW(str);
yield return www;
if (www != null && string.IsNullOrEmpty(www.error))
{
//获取Texture
Texture2D texture = www.texture;
//创建Sprite
Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0.5f, 0.5f));
imgHead.sprite = sprite;
startTime = (double)Time.time - startTime;
Debug.Log("WWW加载用时:" + startTime);
}
}
上一篇: Unity中序列化问题
下一篇: unity AssetBundle打包