前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >unity3d:url下载头像保存在本地(微信头像)

unity3d:url下载头像保存在本地(微信头像)

作者头像
立羽
发布2023-08-24 14:46:27
发布2023-08-24 14:46:27
50300
代码可运行
举报
文章被收录于专栏:Unity3d程序开发Unity3d程序开发
运行总次数:0
代码可运行
代码语言:javascript
代码运行次数:0
复制
using UnityEngine;
using UnityEngine.UI;
using System.Collections;
using System.IO;
using System.Collections.Generic;

public class AsyncImageDownload : MonoBehaviour
{
    Dictionary<int, Sprite> m_dicHeadSpr = new Dictionary<int, Sprite>();

    private static AsyncImageDownload _instance = null;
    public static AsyncImageDownload GetInstance() { return Instance; }
    public static AsyncImageDownload Instance
    {
        get
        {
            if (_instance == null)
            {
                GameObject obj = new GameObject("AsyncImageDownload");
                _instance = obj.AddComponent<AsyncImageDownload>();
                DontDestroyOnLoad(obj);
                _instance.Init();
            }
            return _instance;
        }
    }

    public bool Init()
    {
        if (!Directory.Exists(Application.persistentDataPath + "/ImageCache/"))
        {
            Directory.CreateDirectory(Application.persistentDataPath + "/ImageCache/");
        }
        
        return true;
    }

    public void StartUp()
    {

    }

    public void SetAsyncImage(string url, Image image)
    {
        //开始下载图片前,将UITexture的主图片设置为占位图
        int code = url.GetHashCode();
        if (m_dicHeadSpr.ContainsKey(code))
        {
            image.sprite = m_dicHeadSpr[code];
        }
        else       //如果之前不存在缓存中  就用WWW类下载
        {
            //判断是否是第一次加载这张图片
            if (!File.Exists(path + code.ToString() + ".txt"))
            {
                //如果之前不存在缓存文件
                StartCoroutine(DownloadImage(url, image,code));
            }
            else
            {
                StartCoroutine(LoadLocalImage(url, image,code));
            }
        }
    }

    IEnumerator DownloadImage(string url, Image image, int code)
    {
        WWW www = new WWW(url);
        yield return www;

        Texture2D tex2d = www.texture;



        Sprite sprite = Sprite.Create(tex2d, new Rect(0, 0, tex2d.width, tex2d.height), new Vector2(0, 0));
        image.sprite = sprite;

        byte[] pngData = tex2d.EncodeToPNG();
        File.WriteAllBytes(path + code.ToString() + ".txt", pngData);

        m_dicHeadSpr[code] = sprite;
    }

    IEnumerator LoadLocalImage(string url, Image image,int code)
    {
        string filePath = "file:///" + path + code.ToString() + ".txt";
        //     Debug.Log("getting local image:" + filePath);
        WWW www = new WWW(filePath);
        yield return www;

        Texture2D texture = www.texture;
        Sprite sprite = Sprite.Create(texture, new Rect(0, 0, texture.width, texture.height), new Vector2(0, 0));
        image.sprite = sprite;
        m_dicHeadSpr[code] = sprite;

    }

    public string path
    {
        get
        {
            //pc,ios //android :jar:file//
            return Application.persistentDataPath + "/ImageCache/";

        }
    }
}
本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2018-12-27,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档