/// <summary> /// 创建方块贴图 /// </summary> public static void CreateBlockTexture(int blockTextureSize, string savePath, string saveName, int frameIndex, List <BlockModelCreateBean> listCreateData) { //生成图片tex Texture2D outTexture = new Texture2D(blockTextureSize, blockTextureSize, TextureFormat.RGBA32, false); outTexture.SetPixels(new Color[blockTextureSize * blockTextureSize]); for (int i = 0; i < listCreateData.Count; i++) { BlockModelCreateBean itemCreateData = listCreateData[i]; //Graphics.CopyTexture(itemCreateData.texureBlock, 0,0,0,0, itemCreateData.texureBlock.width, itemCreateData.texureBlock.width, outTexture,0, 0, itemCreateData.startPixel.x, itemCreateData.startPixel.y); //获取当前帧的贴图 int numberTex = itemCreateData.listTexureBlock.Count; int startIndex = frameIndex % numberTex; Color32[] colorPixel = itemCreateData.listTexureBlock[startIndex].GetPixels32(); outTexture.SetPixels32(itemCreateData.startPixel.x, itemCreateData.startPixel.y, itemCreateData.texureSize, itemCreateData.texureSize, colorPixel); } //保存图片 string saveCompletePath = $"{savePath}/{saveName}_{frameIndex}.png"; File.WriteAllBytes(saveCompletePath, outTexture.EncodeToPNG()); EditorUtil.RefreshAsset(); EditorUtil.SetTextureData(saveCompletePath, format: TextureImporterFormat.DXT5, textureImporterCompression: TextureImporterCompression.Uncompressed); }
/// <summary> /// 创建方块模型 /// </summary> public static void CreateBlockModel(int blockTextureSize, string pathRes, string pathSaveTexure, string saveName, string pathMatBlock, int textureArrayNumber = 1) { try { FileInfo[] files = FileUtil.GetFilesByPath($"{pathRes}"); if (files.IsNull()) { LogUtil.Log("CreateBlockModel Fail No Block"); return; } //所有的创建数据 List <BlockModelCreateBean> listCreateData = new List <BlockModelCreateBean>(); //所有像素的最后一个UV坐标位置 Dictionary <int, Vector2Int> dicUVPosition = new Dictionary <int, Vector2Int>(); //当前到第几排像素 int currentPixel = 0; for (int i = 0; i < files.Length; i++) { EditorUI.GUIShowProgressBar("资源刷新", $"刷新方块模型中({i}/{files.Length})", (float)i / files.Length); FileInfo itemFile = files[i]; if (itemFile.Name.Contains(".meta")) { continue; } if (itemFile.Name.Contains(".dae")) { //生成新的方块 string nameNew = $"{itemFile.Name.Replace(".dae", "")}"; //获取对应的材质贴图 string texPath = $"{pathRes}/{nameNew}_texture"; List <Texture2D> listTexItem = new List <Texture2D>(); for (int t = 0; t < textureArrayNumber; t++) { Texture2D texItem = EditorUtil.GetAssetByPath <Texture2D>($"{texPath}{t}.png"); if (texItem != null) { listTexItem.Add(texItem); } } //如果有贴图 则开始生成数据 if (!listTexItem.IsNull()) { //首先设置图片的一些属性 for (int t = 0; t < listTexItem.Count; t++) { EditorUtil.SetTextureData($"{texPath}{t}.png"); } int sizeTexture = listTexItem[0].width > listTexItem[0].height ? listTexItem[0].height : listTexItem[0].width; BlockModelCreateBean blockModelCreateData = new BlockModelCreateBean(); blockModelCreateData.nameBlock = nameNew; blockModelCreateData.uvScaleSize = blockTextureSize / sizeTexture; blockModelCreateData.listTexureBlock = listTexItem; blockModelCreateData.texureSize = sizeTexture; //获取该像素到达的位置 if (dicUVPosition.TryGetValue(sizeTexture, out Vector2Int uvPosition)) { //首先检测是否到达最后一个 if (blockTextureSize - uvPosition.x >= sizeTexture) { dicUVPosition[sizeTexture] = new Vector2Int(uvPosition.x + sizeTexture, uvPosition.y); blockModelCreateData.startPixel = new Vector2Int(uvPosition.x + sizeTexture, uvPosition.y); } else { //如果已经是最后一个了 则下一排 currentPixel += sizeTexture; dicUVPosition[sizeTexture] = new Vector2Int(0, currentPixel); blockModelCreateData.startPixel = new Vector2Int(0, currentPixel); } } else { //如果是第一次添加 dicUVPosition.Add(sizeTexture, new Vector2Int(0, currentPixel)); blockModelCreateData.startPixel = new Vector2Int(0, currentPixel); currentPixel += sizeTexture; } listCreateData.Add(blockModelCreateData); } } } //是否需要创建TextureArray Material matUse = EditorUtil.GetAssetByPath <Material>(pathMatBlock);; //创建方块贴图 if (textureArrayNumber > 1) { CreateBlockAnimTexture(blockTextureSize, textureArrayNumber, pathSaveTexure, saveName, listCreateData); CreateBlockTextureArray(blockTextureSize, textureArrayNumber, new List <string>() { saveName }); EditorUtil.RefreshAsset(matUse); } else { CreateBlockTexture(blockTextureSize, pathSaveTexure, saveName, 0, listCreateData); Texture2D createTex = EditorUtil.GetAssetByPath <Texture2D>($"{pathSaveTexure}/{saveName}_0.png"); //设置材质球 matUse.mainTexture = createTex; EditorUtil.RefreshAsset(matUse); } //生成相关模型 for (int i = 0; i < listCreateData.Count; i++) { BlockModelCreateBean itemCreateData = listCreateData[i]; //获取老方块 GameObject obj = EditorUtil.GetAssetByPath <GameObject>($"{pathRes}/{itemCreateData.nameBlock}.dae"); MeshFilter objOldMeshFilter = obj.GetComponentInChildren <MeshFilter>(); GameObject objNew = new GameObject(itemCreateData.nameBlock); objNew.transform.localScale = Vector3.one * 0.03125f; objNew.transform.localPosition = new Vector3(0, -0.5f, 0); MeshRenderer objNewMeshRenderer = objNew.AddComponent <MeshRenderer>(); MeshFilter objNewMeshFilter = objNew.AddComponent <MeshFilter>(); //设置UV Vector2[] oldUVList = objOldMeshFilter.sharedMesh.uv; Vector2[] newUVList = new Vector2[oldUVList.Length]; for (int f = 0; f < oldUVList.Length; f++) { newUVList[f] = oldUVList[f] * (1f / itemCreateData.uvScaleSize); newUVList[f] += itemCreateData.GetStartUV(blockTextureSize); } //创建新的mesh Mesh newMesh = new Mesh(); newMesh.name = $"{itemCreateData.nameBlock}_Mesh"; newMesh.SetVertices(objOldMeshFilter.sharedMesh.vertices); newMesh.SetTriangles(objOldMeshFilter.sharedMesh.triangles, 0); newMesh.SetUVs(0, newUVList); newMesh.RecalculateBounds(); newMesh.RecalculateNormals(); //保存mesh string pathMesh = $"{Path_Block_Model_Save}/{newMesh.name}.asset"; EditorUtil.CreateAsset(newMesh, pathMesh); //设置mesh objNewMeshFilter.sharedMesh = newMesh; //设置材质 objNewMeshRenderer.material = matUse; EditorUtil.CreatePrefab(objNew, $"{Path_Block_Model_Save}/{itemCreateData.nameBlock}.prefab"); //EditorUtil.RefreshAsset(objNew); DestroyImmediate(objNew); } } finally { EditorUI.GUIHideProgressBar(); } }