//根据贴图数组来创建合并好的贴图图集 private static Texture2D CreateAtlasMaxRect(TextureData[] textureDatas) { Array.Sort <TextureData>(textureDatas, new Texture2DComparison()); //读贴图数据进行排序 Texture2D atlas = new Texture2D(ATLAS_MAX_SIZE, ATLAS_MAX_SIZE); //创建Texture2D图集,按照最大尺寸 MaxRectsBinPack.PackTextures(atlas, GetPackTextures(textureDatas)); //将贴图数据(每个sprite)填充进atlas图集 return(atlas); }
/// 向高度增长空间 private static Rect[] CalcByGrowHeight(Texture2D[] textures, ref int maxWidth, ref int maxHeight) { bool successed = true; Rect[] aryRects = new Rect[textures.Length]; while (true) { MaxRectsBinPack packer = new MaxRectsBinPack(maxWidth, maxHeight, false); for (int i = 0; i < textures.Length; ++i) { Texture2D tex = textures[i]; Rect rect = packer.Insert(tex.width, tex.height, FreeRectChoiceHeuristic.RectBestAreaFit); aryRects[i] = rect; if (rect.width == 0 || rect.height == 0) { //不通过 successed = false; break; } if (i == textures.Length - 1) { successed = true; } } if (successed) { break; } //先往高度发展,如果发现高度大于1024,则向宽度发展 if (maxHeight >= ATLAS_MAX_SIZE && maxWidth < ATLAS_MAX_SIZE) { //不允许超过2048 maxWidth *= 2; } else if (maxHeight >= FAVOR_ATLAS_SIZE && maxWidth < FAVOR_ATLAS_SIZE) { //尽量不能超过1024 maxWidth *= 2; } else if (maxHeight >= maxWidth * 2) { //尽量使宽和高之间的比例不要相差太大,防止IOS上会扩展成更大的正方形 maxWidth *= 2; } else { maxHeight *= 2; } aryRects = new Rect[textures.Length]; } return(aryRects); }