/// <summary> /// 设置图集中散图的位置和缩放 /// </summary> /// <returns>The texture scale and offset.</returns> /// <param name="renderTextureSize">Render texture size.</param> /// <param name="width">Width.</param> /// <param name="height">Height.</param> // private Rect GetTextureScaleAndOffset (int renderTextureSize, float width, float height) { // //Debug.LogError(renderTextureSize + "---" + width + "---" + height); // //合并算法 // if (currentTextureList.Count == 0) { // return new Rect (0f, 0f, (float) renderTextureSize / width, (float) renderTextureSize / height); // } // endIndex = currentTextureList.Count - 1; // currentAllWidth = GetAllWidth (beginIndex, endIndex); // if (currentAllWidth + width > renderTextureSize) { // currentAllHeight = GetMaxHeight (beginIndex, endIndex); // beginIndex = endIndex + 1; // currentAllWidth = GetAllWidth (beginIndex, endIndex); // } // return new Rect ((-1) * (currentAllWidth / width), (-1) * currentAllHeight / height, renderTextureSize / width, renderTextureSize / height); // } /// <summary> /// 计算当前行中散图的宽度之和 /// </summary> /// <returns>The all width.</returns> /// <param name="bIndex">B index.</param> /// <param name="eIndex">E index.</param> // private float GetAllWidth (int bIndex, int eIndex) { // float allWidth = 0; // for (int i = bIndex; i <= eIndex; i++) { // allWidth += textureList[i].texture.width; // } // return allWidth; // } /// <summary> /// 计算当前所有行中最高的散图的高度之和 /// </summary> /// <returns>The max height.</returns> /// <param name="bIndex">B index.</param> /// <param name="eIndex">E index.</param> // private float GetMaxHeight (int bIndex, int eIndex) { // float maxHeight = int.MinValue * 1.0f; // for (int i = bIndex; i <= eIndex; i++) { // if (maxHeight < textureList[i].texture.height) { // maxHeight = textureList[i].texture.height; // } // } // lineHeightList.Add (maxHeight); // float allheight = 0; // for (int j = 0; j < lineHeightList.Count; j++) { // allheight += lineHeightList[j]; // } // return allheight; // } /// <summary> /// 给散图排序 /// </summary> /// <returns>The list.</returns> /// <param name="textures">Textures.</param> private List <TextureClass> SortList(List <TextureClass> textures) { List <TextureClass> tempList = textures; for (int i = 0; i < tempList.Count - 1; i++) { for (int j = 0; j < tempList.Count - 1 - i; j++) { if (GetAreaByTexture(tempList[j].texture) > GetAreaByTexture(tempList[j + 1].texture)) { TextureClass tempTexture = tempList[j]; tempList[j] = tempList[j + 1]; tempList[j + 1] = tempTexture; } } } return(tempList); }
public IEnumerator GetRenderTextureByTexturesOptimize(CombineTextureData combineTextureData, List <Spine.AtlasRegion> regions, int renderTextureSize, Action <PackagedTextureResult> finishCallBack) { ResetValue(combineTextureData); currentAtlasSize = renderTextureSize; List <TextureClass> textureList = new List <TextureClass>(); //GetTextureClassList (textures); List <TextureClass> currentTextureList = new List <TextureClass> (); Material material = InitMaterial(); MaxRectsBinPack maxRectsBinPack = new MaxRectsBinPack(currentAtlasSize, currentAtlasSize, false); RenderTexture render = RenderTexture.GetTemporary(currentAtlasSize, currentAtlasSize, 0, RenderTextureFormat.ARGB4444); Dictionary <RenderTexture, TextureClass[]> dic = new Dictionary <RenderTexture, TextureClass[]> (); Graphics.Blit(null, null, material, 0); for (int i = 0; i < regions.Count; i++) { UnityEngine.Profiling.Profiler.BeginSample("SpineChange_ToTexture"); RenderTexture regionTex = Spine.Unity.Modules.AttachmentTools.AtlasUtilities.ToTexture(regions[i]); UnityEngine.Profiling.Profiler.EndSample(); textureList.Add(new TextureClass { index = i, texture = regionTex, textureSize = new TextureSize { x = regionTex.width, y = regionTex.height } }); if (!IsSuitableTexture(textureList[i].texture)) { throw new System.NotSupportedException("[--散图 " + textureList[i].texture.name + " 的大小超出单位图集的大小,请修改散图或单位图集的大小(SpineConstValue.smallAtlasSize)--]"); } Rect posRect = maxRectsBinPack.Insert(textureList[i].texture.width, textureList[i].texture.height, MaxRectsBinPack.FreeRectChoiceHeuristic.RectBestAreaFit); if (posRect.height <= 0) { //保存本次的图集和散图的位置信息 TextureClass[] array1 = new TextureClass[currentTextureList.Count]; currentTextureList.CopyTo(array1); dic.Add(render, array1); currentTextureList.Clear(); Graphics.Blit(null, null, material, 0); //初始化参数进行下一次的合图 material = InitMaterial(); maxRectsBinPack = new MaxRectsBinPack(currentAtlasSize, currentAtlasSize, false); render = RenderTexture.GetTemporary(currentAtlasSize, currentAtlasSize, 0, RenderTextureFormat.ARGB4444); Graphics.Blit(null, null, material, 0); textureList.RemoveAt(i); --i; combineTextureData.smallAtlasNumber++; continue; } Rect scaleRect = GetScaleRect(posRect, renderTextureSize, renderTextureSize); material.SetTexture("_MainTex", textureList[i].texture); material.SetFloat("_ScaleX", scaleRect.width); material.SetFloat("_ScaleY", scaleRect.height); material.SetFloat("_OffsetX", scaleRect.x); material.SetFloat("_OffsetY", scaleRect.y); Graphics.Blit(null, render, material, 1); textureList[i].rect = posRect; currentTextureList.Add(textureList[i]); //卸载散图资源 // textureList[i].texture = null; // UnityEngine.Resources.UnloadUnusedAssets(); RenderTexture.ReleaseTemporary(textureList[i].texture); textureList[i].texture.Release(); textureList[i].texture = null; yield return(null); } Graphics.Blit(null, null, material, 0); TextureClass[] array2 = new TextureClass[currentTextureList.Count]; currentTextureList.CopyTo(array2); dic.Add(render, array2); if (null != combineTextureData.combineCoroutineBig) { GameManager.Instance.StopCoroutine(combineTextureData.combineCoroutineBig); } combineTextureData.combineCoroutineBig = GameManager.Instance.StartCoroutine(FinallyPackage(combineTextureData, dic, finishCallBack)); }