using UnityEngine;
using System;
using MindPowerSdk;
using UnityEngine;
public static class AtlasBuilder
{
///
/// 将 textures 中的贴图每 countPerAtlas 张打包成一个图集,默认图集尺寸为 2048。
///
/// 所有原始贴图,数量应为 countPerAtlas 的倍数
/// 每个图集中包含的贴图数量(例如 16)
/// 图集尺寸
/// 打包好的图集数组
public static Texture2D[] BuildAtlases(Texture2D[] textures, int countPerAtlas, int atlasSize = 2048)
{
int atlasCount = textures.Length / countPerAtlas;
Texture2D[] atlases = new Texture2D[atlasCount];
for (int i = 0; i < atlasCount; i++)
{
Texture2D atlas = new Texture2D(atlasSize, atlasSize, TextureFormat.RGBA32, false);
Texture2D[] group = new Texture2D[countPerAtlas];
for (int j = 0; j < countPerAtlas; j++)
{
group[j] = textures[i * countPerAtlas + j];
}
// 自动打包,第二个参数表示边距(像素),atlasSize 限制最大尺寸
atlas.PackTextures(group, 2, atlasSize);
atlases[i] = atlas;
}
return atlases;
}
}
public static class TerrainLayerBuilder
{
///
/// 从 4 个图集创建 TerrainLayer,每个图集对应一个层
///
/// 必须包含 4 个图集
/// TerrainLayer 数组
public static TerrainLayer[] BuildTerrainLayers(Texture2D[] atlases)
{
if (atlases.Length != 4)
{
Debug.LogError("期望传入 4 个图集贴图");
return null;
}
TerrainLayer[] layers = new TerrainLayer[4];
for (int i = 0; i < 4; i++)
{
TerrainLayer tl = new TerrainLayer();
tl.diffuseTexture = atlases[i];
// 假设图集内为 4×4 布局,每个子贴图尺寸为 atlas 尺寸的 1/4
tl.tileSize = new Vector2(tl.diffuseTexture.width / 4f, tl.diffuseTexture.height / 4f);
layers[i] = tl;
}
return layers;
}
}