using UnityEngine; public class TileTextureGenerator : MonoBehaviour { public Texture2D[] tileTextures; // 瓷砖贴图数组 public int tileRows = 3; // 平铺行数 public int tileCols = 3; // 平铺列数 public Material targetMaterial; // 目标材质 void Start() { if (tileTextures == null || tileTextures.Length == 0) { Debug.LogError("请设置瓷砖贴图数组!"); return; } if (targetMaterial == null) { Debug.LogError("请设置目标材质!"); return; } GenerateTiledTexture(); } void GenerateTiledTexture() { // 获取第一张瓷砖贴图的尺寸 int tileWidth = tileTextures[0].width; int tileHeight = tileTextures[0].height; // 计算目标贴图尺寸 int targetWidth = tileWidth * tileCols; int targetHeight = tileHeight * tileRows; // 创建目标贴图 Texture2D targetTexture = new Texture2D(targetWidth, targetHeight); // 循环平铺 for (int row = 0; row < tileRows; row++) { for (int col = 0; col < tileCols; col++) { // 计算当前瓷砖在目标贴图上的起始位置 int x = col * tileWidth; int y = row * tileHeight; // 选择要使用的瓷砖贴图(这里简单地循环使用数组中的贴图) Texture2D currentTile = tileTextures[(row * tileCols + col) % tileTextures.Length]; // 将当前瓷砖贴图复制到目标贴图 for (int i = 0; i < tileWidth; i++) { for (int j = 0; j < tileHeight; j++) { targetTexture.SetPixel(x + i, y + j, currentTile.GetPixel(i, j)); } } } } // 应用更改并生成 Mipmap targetTexture.Apply(); // 将生成的贴图应用到目标材质 targetMaterial.mainTexture = targetTexture; } }