/// <summary>
/// 设置头像
/// </summary>
/// <param name="url">URL.</param>
/// <param name="text">Text.</param>
public void SetHeadImg(string url,UITexture text)
{
StartCoroutine(DownloadHeadImg(url,text));
}
IEnumerator DownloadHeadImg(string url,UITexture text)
{
string path = Application.persistentDataPath + "/head_" + url.GetHashCode ()+".png";
Debuger.Log ("头像路径:"+path);
WWW www = new WWW (File.Exists (path) ? "file://" + path : url);
Debuger.Log (www.url);
yield return www;
if (!string.IsNullOrEmpty (www.error))
{
Debuger.Log("头像下载失败:"+www.error);
}
else
{
text.mainTexture = www.texture;
try
{
File.WriteAllBytes(path,www.texture.EncodeToPNG());
}
catch(Exception e)
{
Debuger.Log("头像数据写入错误:"+e.Message);
}
}
}
path= 2.在使用WWW进行加载资源的时候,路径是需要"file://"。( "file://" + path) |