// 对自己的头像做缓存
private Sprite selfIcon;
/// <summary>
/// 设置玩家的头像 微信或本地
/// </summary>
/// <param name="imgIcon">sprite</param>
/// <param name="url">头像url</param>
public void SetPlayerIcon(Image imgIcon, string url)
{
if (imgIcon == null) return;
if (string.IsNullOrEmpty(url))
{
return;
}
bool isSelf = url == GameManager.Instance.userInfo.FaceURL;
if (isSelf && selfIcon != null)
{
imgIcon.sprite = selfIcon;
return;
}
StartCoroutine(LoadIcon(imgIcon, url, isSelf));
}
private IEnumerator LoadIcon(Image imgIcon, string url, bool cache)
{
WWW www = new WWW(url);
yield return new WaitUntil(() => www.isDone);
if (www.error == null)
{
var tex2d = www.texture;
var spr = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), Vector2.zero);
imgIcon.sprite = spr;
if (cache) selfIcon = spr;
}
else
{
Debug.LogError(www.error);
}
}
}
private Sprite selfIcon;
/// <summary>
/// 设置玩家的头像 微信或本地
/// </summary>
/// <param name="imgIcon">sprite</param>
/// <param name="url">头像url</param>
public void SetPlayerIcon(Image imgIcon, string url)
{
if (imgIcon == null) return;
if (string.IsNullOrEmpty(url))
{
return;
}
bool isSelf = url == GameManager.Instance.userInfo.FaceURL;
if (isSelf && selfIcon != null)
{
imgIcon.sprite = selfIcon;
return;
}
StartCoroutine(LoadIcon(imgIcon, url, isSelf));
}
private IEnumerator LoadIcon(Image imgIcon, string url, bool cache)
{
WWW www = new WWW(url);
yield return new WaitUntil(() => www.isDone);
if (www.error == null)
{
var tex2d = www.texture;
var spr = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), Vector2.zero);
imgIcon.sprite = spr;
if (cache) selfIcon = spr;
}
else
{
Debug.LogError(www.error);
}
}
}