public async Task <Sprite> getAvatar(string url) { var hashCode = url.GetHashCode(); Sprite sprite = null; if (!Images.TryGetValue(hashCode, out sprite)) { Texture2D texture = new Texture2D(100, 100); string prefsURL = PlayerPrefs.GetString(url); if (string.IsNullOrEmpty(url)) { return(null); } UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); await www.SendWebRequest(); if (www.isNetworkError || www.isHttpError) { Debug.LogWarning(www.error); return(null); } var avatarTexture = DownloadHandlerTexture.GetContent(www); texture = ImagesManager.RoundCrop(avatarTexture); PlayerPrefs.SetString(url, System.Convert.ToBase64String(texture.EncodeToPNG())); sprite = Sprite.Create(texture, new Rect(0f, 0f, texture.width, texture.height), Vector2.zero); Images.Add(hashCode, sprite); return(sprite); } else { return(sprite); } }
public static Sprite getSpriteFromBytes(byte[] bytes) { Texture2D texture = new Texture2D(1, 1); Sprite newSprite = null; texture.LoadImage(bytes); Texture2D roundTxt = ImagesManager.RoundCrop(texture); newSprite = Sprite.Create(roundTxt as Texture2D, new Rect(0f, 0f, roundTxt.width, roundTxt.height), Vector2.zero); return(newSprite); }
public IEnumerator initPlayerAvatar(string url, Image avatar) { UnityWebRequest www = UnityWebRequestTexture.GetTexture(url); yield return(www.SendWebRequest()); var texture = DownloadHandlerTexture.GetContent(www); Texture2D RoundTxt = ImagesManager.RoundCrop(texture); avatar.sprite = Sprite.Create(RoundTxt, new Rect(0, 0, RoundTxt.width, RoundTxt.height), new Vector2(0, 0)); }