zoukankan      html  css  js  c++  java
  • 解决NGUI不显示gif图片的问题

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

    解决:下载后检查如果是.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();
        }
    

      

  • 相关阅读:
    (数据看成字符串)大端——高尾端,小端——低尾端链接
    ASP.NET 页生命周期概述
    asp.net web 开发登录相关操作的控件LoginName、LoginStatus和LoginView控件使用详解
    Server.MapPath()目录详解
    C# ASP.NET FILEUPLOAD详解
    HTML中id、name、class 区别
    注册表新建 -------DWORD(32-位)
    XSD标准架构-----<xsd:element> 元素详解
    html5学习链接
    关于HSL和HSV颜色空间的详细论述
  • 原文地址:https://www.cnblogs.com/wang-jin-fu/p/12856007.html
Copyright © 2011-2022 走看看