首页 > 其他 > 详细

解决NGUI不显示gif图片的问题

时间:2020-05-09 11:13:45      阅读:68      评论:0      收藏:0      [点我收藏+]

问题:游戏内使用玩家的关系链头像(微信、qq)作为游戏头像,但是下载下来的gif格式资源无法跟png\jpg一样正确的显示,而是显示出红色的问号?

解决:下载后检查如果是.gif格式的资源,使用UniGif插件进行显示。由于UniGif插件使用unity的协成下载资源、播放动画,这边要注意unity对非活跃对象调用StartCoroutine

代码:

//避免重复加载,缓存下载结果
    private static Dictionary<string, DownCache> m_cacheDownload = new Dictionary<string, DownCache>();
    
    private class DownCache {
        public byte[] data;
        public string text;
        public Texture tex;
        public string url;
    }

/// <summary>
    /// 下载图片
    /// </summary>
    /// <param name="url"></param>
    /// <param name="callBack"></param>
    public static void DownLoadTextureByUrl(string url, Action<Texture, string> callBack) 
    {
        if(callBack == null) return;
        DownCache cache;
        
        if (m_cacheDownload.TryGetValue(url, out cache)) 
        {
            if (cache.text.StartsWith("GIF")) //gif
            {
                callBack(null, url);
            } else {
                callBack(cache.tex, url);
            }
            return;
        }
        
        TaskManager.Instance.Create(DownLoadPicture(url, callBack));
    }

    private static IEnumerator DownLoadPicture(string url, Action<Texture, string> callBack)
    {
        UnityWebRequest req = UnityWebRequest.Get(url);
        DownloadHandlerTexture handle = new DownloadHandlerTexture(true);
        req.downloadHandler = handle;
        req.timeout = 30;
        yield return req.SendWebRequest();
        if (req.isNetworkError || req.isHttpError)
        {
            yield break; 
        }

        Texture tex = handle.texture;

        if (handle.text.StartsWith("GIF")) //gif
        {
            if (callBack != null) {
                callBack(null, url);
            }
        } else {// png/jpg
            tex.name = url;
            if (callBack != null) {
                callBack(tex, url);
            }
        }
        
        DownCache cacheHandle = new DownCache();//缓存,req.Dispose会销毁handle,所以这边单独缓存
        cacheHandle.data = handle.data;
        cacheHandle.text = handle.text;
        cacheHandle.tex = tex;
        cacheHandle.url = url;
        
        if(!m_cacheDownload.ContainsKey(url))
            m_cacheDownload.AddValueEx(url,cacheHandle);
        
        Debug.Log("download end : " + tex.name + "_" + tex.width);
        req.Dispose();
    }

    public static void DownloadAndPlayGif(Component com, string url) {
        UniGifTexture uniGifTexture = com.GetComponent<UniGifTexture>();
        if (uniGifTexture != null) 
        {
            if (com.gameObject.activeInHierarchy) 
            {
                uniGifTexture.PlayGifWithUrl(url);
            } else {//协成必须在对象可见的情况下才能调用
                uniGifTexture.loadOnStartUrl = url;
                GGDebug.Debug("DownloadAndPlayGif with inactive obj : " + com.name);
            }
        }
    }

    public static void ClearGif(Component com) {
        UniGifTexture uniGifTexture = com.GetComponent<UniGifTexture>();
        if (uniGifTexture != null) {
            uniGifTexture.Clear();
            GameObject.DestroyImmediate(uniGifTexture.texture.mainTexture, true);
        }
    }

  对UniGifTexture做的一个小优化:

public void PlayGifWithUrl(string url) {
        if(url.Equals(loadOnStartUrl) && state == STATE.PLAYING) return;//减少重复加载
        
        Clear();
        DestroyImmediate(texture.mainTexture, true);
        
        loadOnStartUrl = url;
        PlayGif();
    }

  

解决NGUI不显示gif图片的问题

原文:https://www.cnblogs.com/wang-jin-fu/p/12856007.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!