Ciao. Mi servirebbe uno script in c# da usare su Unity. Lo script dovrebbe semplicemente…
- User
Ciao. Mi servirebbe uno script in c# da usare su Unity. Lo script dovrebbe semplicemente esportare l'Heightmap di un oggetto "Terrain" divisa in tiles, in base a quante ne vuole l'utente. Ad esempio: un terreno di grandezza di 1000x e 1000z, l'utente ha impostato 4 tiles X e 4 tiles Z, lo script dovrebbe suddividere il terreno in 16 tiles e creare una texture dell'heightmap di ogni singola tiles.
- Assistant
Ecco uno script di base in C# per Unity che fa ciò che hai richiesto:
using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEditor; public class ExportHeightmapTiles : MonoBehaviour { public Terrain terrain; public int tilesX = 4; public int tilesZ = 4; public void ExportTiles() { if (terrain == null) { Debug.LogError("Terrain non impostato. Si prega di assegnare un oggetto Terrain."); return; } TerrainData terrainData = terrain.terrainData; int heightmapWidth = terrainData.heightmapResolution; int heightmapHeight = terrainData.heightmapResolution; int tileWidth = heightmapWidth / tilesX; int tileHeight = heightmapHeight / tilesZ; float[,] heights = terrainData.GetHeights(0, 0, heightmapWidth, heightmapHeight); for (int z = 0; z < tilesZ; z++) { for (int x = 0; x < tilesX; x++) { Texture2D texture = new Texture2D(tileWidth, tileHeight, TextureFormat.RGB24, false); for (int j = 0; j < tileHeight; j++) { for (int i = 0; i < tileWidth; i++) { float h = heights[z * tileHeight + j, x * tileWidth + i]; Color color = new Color(h, h, h); texture.SetPixel(i, j, color); } } texture.Apply(); byte[] bytes = texture.EncodeToPNG(); System.IO.File.WriteAllBytes("Tile_" + x + "_" + z + ".png", bytes); } } Debug.Log("Esportazione delle tiles completata."); } }Per utilizzare questo script, segui questi passaggi:
- Crea un nuovo GameObject vuoto nella tua scena di Unity e rinominalo "ExportHeightmap".
- Copia e incolla il codice precedente in un nuovo file chiamato "ExportHeightmapTiles.cs" nella tua cartella Assets.
- Allega lo script "ExportHeightmapTiles" al tuo GameObject "ExportHeightmap" trascinandolo dalla tua cartella Assets.
- Imposta la proprietà "Terrain" del componente ExportHeightmapTiles su un oggetto Terrain nella tua scena.
- Imposta le proprietà "tilesX" e "tilesZ" come desideri.
- Seleziona il GameObject "ExportHeightmap" e vai nella finestra dell'Inspector. Aggiungi un nuovo componente "Editor Script" e incolla il seguente codice (assicurati di aver prima importato
UnityEditor):
[CustomEditor(typeof(ExportHeightmapTiles))] public class ExportHeightmapTilesEditor : Editor { public override void OnInspectorGUI() { DrawDefaultInspector(); ExportHeightmapTiles script = (ExportHeightmapTiles)target; if (GUILayout.Button("Esporta Tiles")) { script.ExportTiles(); } } }Ora vedrai un pulsante "Esporta Tiles" nell'Inspector del tuo GameObject "ExportHeightmap". Premendo quel pulsante, verranno esportate le tiles dell'heightmap in formato PNG nella cartella del progetto. Puoi modificare il percorso o il formato dell'immagine come desideri.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.