欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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