Exemplo n.º 1
0
 /// <summary>
 /// 删除加载缓存图片
 /// </summary>
 /// <param name="info"></param>
 private void listRemoveImg(Image_info info)
 {
     iList.Remove(info);
     if (iList.Count >= 1)
     {
         StartCoroutine(LoadLocalImage(iList[0]));
     }
     else if (iList.Count <= 0)
     {
         startLoadImg = false;
     }
 }
Exemplo n.º 2
0
    IEnumerator DownloadImage(Image_info info)
    {
        string filePath = getImgUrl(info);

        string url   = idToUrl(info.key) + "@" + getSizeString(info.state);
        bool   isOk  = false;
        int    times = 0;

        while (!isOk)
        {
            WWW www = new WWW(url);
            yield return(www);

            if (www.error == null)
            {
                info.tex.LoadImage(www.bytes);
                info.isError = "OK";
                info.site    = "网络";
                info.fun(info.tex);
                listRemoveImg(info);
                isOk = true;
                try
                {
                    File.WriteAllBytes(filePath, info.tex.EncodeToJPG());
                }
                catch (System.Exception e)
                {
                    string s = e.ToString();
                }
            }
            else
            {
                Debug.LogError("ImageManager2 网络请求图片出错:" + url + www.error.ToString());
                info.isError = "Error";
                if (times == 0)
                {
                    listRemoveImg(info);
                }
                times++;
                if (times >= 10)
                {
                    isOk = true;
                    info.fun(null);
                }
            }
            if (!isOk)
            {
                yield return(new WaitForSeconds(3f));
            }
        }
    }
Exemplo n.º 3
0
 /// <summary>
 /// 添加一个元素到缓存中
 /// </summary>
 /// <param name="info">加载元素</param>
 /// <param name="isFistLoad">是否优先加载</param>
 private void listAddImg(Image_info info, bool isFistLoad)
 {
     if (iList.Contains(info))
     {
         iList.Remove(info);
     }
     if (isFistLoad)
     {
         iList.Insert(0, info);
     }
     else
     {
         iList.Add(info);
     }
 }
    /**
     * 构建信息对象,返回包含每个数据信息的结构
     */
    private List <Image_info> Build_info_object(string descrition)
    {
        List <Image_info> ret = new List <Image_info>();

        // 切割数据单元格
        string pattern = @"(\s*?DicingTextureData data\s*(?:.|\r|\n)*?int transparentIndex = -?\d+?)";

        // 目录名
        Match  dir_name_match = Regex.Match(descrition, "string m_Name = \"(\\w*)\"");
        string dir_name       = dir_name_match.Groups[1].ToString();

        foreach (Match match in Regex.Matches(descrition, pattern))
        {
            // 构建数据
            Image_info info = new Image_info();

            Match name_match = Regex.Match(match.Value, "string name = \"(\\w*)\"");
            info.name = name_match.Groups[1].ToString();

            Match atlasName_match = Regex.Match(match.Value, "string atlasName = \"(\\w*)\"");
            info.atlas_name = atlasName_match.Groups[1].ToString();

            Match width_match = Regex.Match(match.Value, "int width = (\\d+)");
            info.w = Convert.ToInt32(width_match.Groups[1].ToString());

            Match height_match = Regex.Match(match.Value, "int height = (\\d+)");
            info.h = Convert.ToInt32(height_match.Groups[1].ToString());

            info.dir_name = dir_name;

            info.cell_indexs = new List <int>();

            foreach (Match cell_index in Regex.Matches(match.Groups[1].ToString(), "data = (\\d+)"))
            {
                info.cell_indexs.Add(Convert.ToInt32(cell_index.Groups[1].ToString()));
            }

            ret.Add(info);
        }

        return(ret);
    }
Exemplo n.º 5
0
    /// <summary>
    /// 删除图片(非强制删除)
    /// </summary>
    /// <param name="id"></param>
    /// <param name="size"></param>
    public void deleteImage(string id, TextureSize_state size)
    {
        //Debug.Log ("deleteImage   "+id);
        if (!imgList.ContainsKey(id))
        {
            Debug.LogError(imgList.Count + "====deleteImage ERROR" + id);
            return;
        }
        Image_info info = imgList[id];

        if (info == null)
        {
            Debug.LogError("deleteImage ERROR");
            return;
        }

        //如果缓存中有图片,则删除该图片不继续加载
        if (iList.Contains(info))
        {
            iList.Remove(info);
        }

        info.stateIndex[(int)size]--;
        if (info.stateIndex[(int)size] < 0)
        {
            Debug.LogError("deleteImage ERROR 引用计数溢出");
        }

        //如果所有计数均为0,则删除该图片
        int max = 0;

        for (int i = 0; i < info.stateIndex.Length; i++)
        {
            max += info.stateIndex[i];
        }
        if (max <= 0)
        {
            imgList.Remove(id);
            Resources.UnloadUnusedAssets();
        }
    }
Exemplo n.º 6
0
    IEnumerator LoadLocalImage(Image_info info)
    {
        string filePath = getWWWImgUrl(info);
        WWW    www      = new WWW(filePath);

        yield return(www);

        if (www.error == null)
        {
            info.tex.LoadImage(www.bytes);
            listRemoveImg(info);
            info.isError = "OK";
            info.site    = "本地";
            info.fun(info.tex);
        }
        else
        {
            //TODO 加载网络数据
            StartCoroutine(DownloadImage(info));
        }
    }
Exemplo n.º 7
0
 public void loadImage(string id, TextureSize_state size, Callback <Texture2D> fun, bool isFistLoad = false)
 {
     if (imgList.ContainsKey(id))
     {
         Image_info info = imgList[id];
         info.isError = "null";
         info.site    = "null";
         info.fun     = fun;
         //如果有小图,则删除小图,加载大图
         if ((int)imgList[id].state > (int)size)
         {
             info.state = size;
             info.stateIndex[(int)size]++;
             listAddImg(info, isFistLoad);
             listLoadImg();
         }
         //否则记数加1,然后返回图片
         else
         {
             info.stateIndex[(int)size]++;
             fun(imgList[id].tex);
         }
     }
     else
     {
         //请求本地是否有该图片
         Image_info info = new Image_info();
         info.isError = "null";
         info.site    = "null";
         info.tex     = new Texture2D(0, 0);
         info.key     = id;
         info.state   = size;
         info.stateIndex[(int)size] = 1;
         info.fun = fun;
         imgList.Add(id, info);
         listAddImg(info, isFistLoad);
         listLoadImg();
     }
 }
Exemplo n.º 8
0
    private void loadLocalImage(Image_info info)
    {
        string filePath = Application.persistentDataPath + "/Resources/" + info.key;

        if (System.IO.File.Exists(filePath))
        {
            try
            {
                Texture2D test = (Texture2D)Resources.Load(info.key, typeof(Texture2D));
                info.tex = test;
                info.fun(info.tex);
            }
            catch (System.Exception e)
            {
                Debug.Log("loadLocalImage 读取本地文件出错:" + e.ToString());
            }
        }
        else
        {
            StartCoroutine(DownloadImage(info));
        }
    }
Exemplo n.º 9
0
    private static string getImgUrl(Image_info info)
    {
        switch (info.state)
        {
        case TextureSize_state.Texture_raw:
            return(FileConfig.img_file_raw + info.key + ".sky");

        case TextureSize_state.Texture_big:
            return(FileConfig.img_file_big + info.key + ".sky");

        case TextureSize_state.Texture_centre:
            return(FileConfig.img_file_centre + info.key + ".sky");

        case TextureSize_state.Texture_little:
            return(FileConfig.img_file_little + info.key + ".sky");

        case TextureSize_state.Texture_tiny:
            return(FileConfig.img_file_tiny + info.key + ".sky");

        default:
            return(null);
        }
    }
    /**
     * 转换图像
     */
    private void Convert_Image(Image_info info, Dictionary <string, Bitmap> atlas_Bitmaps)
    {
        var path = Path.Combine(source_path, info.atlas_name + ".png");

        if (!atlas_Bitmaps.ContainsKey(path))
        {
            System.Console.WriteLine("载入地图集:{0}", info.atlas_name);
            // 垂直翻转画布,因为是UV坐标
            Bitmap atlas_bitmap = Image.FromFile(path) as Bitmap;
            atlas_bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);
            atlas_Bitmaps.Add(path, atlas_bitmap);
        }

        Bitmap atlas_image = atlas_Bitmaps[path];

        int[] index = info.cell_indexs.ToArray();

        Bitmap dst_bitmap = new Bitmap(info.w, info.h, PixelFormat.Format24bppRgb);

        Graphics g;

        //要绘制到的位图
        g = Graphics.FromImage(dst_bitmap);

        int cellWidth  = info.w;
        int cellHeight = info.h;

        int cellSize        = 64;
        int padding         = 3;
        int paddingCellSize = cellSize - padding * 2;

        int cellCountX = (int)Math.Ceiling(1.0f * cellWidth / paddingCellSize);
        int cellCountY = (int)Math.Ceiling(1.0f * cellHeight / paddingCellSize);

        int atlasWidth      = atlas_image.Width;
        int atlasHeight     = atlas_image.Height;
        int atlasCellCountX = (int)Math.Ceiling(1.0f * atlasWidth / cellSize);

        int i = 0;

        for (int cellY = 0; cellY < cellCountY; ++cellY)
        {
            int y0 = cellY * paddingCellSize;
            for (int cellX = 0; cellX < cellCountX; ++cellX)
            {
                int x0        = cellX * paddingCellSize;
                int cellIndex = index[i];

                int ux = (cellIndex % atlasCellCountX) * cellSize;
                int uy = (cellIndex / atlasCellCountX) * cellSize;

                //复制图像
                g.DrawImage(atlas_image, new Rectangle(x0, y0, cellSize, cellSize), new Rectangle(ux, uy, cellSize, cellSize), GraphicsUnit.Pixel);
                i++;
            }
        }

        dst_bitmap.RotateFlip(RotateFlipType.RotateNoneFlipY);

        string save_file_path = Path.Combine(target_path, info.dir_name, info.name + ".png");

        dst_bitmap.Save(save_file_path, ImageFormat.Png);

        System.Console.WriteLine("写入文件:{0}", save_file_path);
    }
Exemplo n.º 11
0
 private static string getWWWImgUrl(Image_info info)
 {
     return("file:///" + getImgUrl(info));
 }