Skip to content

Instantly share code, notes, and snippets.

@nir1082
Last active January 23, 2018 03:11
Show Gist options
  • Save nir1082/b6ef65899105823ff72e68c2882711e0 to your computer and use it in GitHub Desktop.
Save nir1082/b6ef65899105823ff72e68c2882711e0 to your computer and use it in GitHub Desktop.
[Unity] Terrainのアルファマップをpngファイルに出力する
using System.IO;
using UnityEngine;
using UnityEditor;
public class TerrainAlphamapToPNG : Editor
{
[MenuItem("Assets/Create/TerrainPNG")]
static void Create()
{
TerrainData terrainData = (TerrainData)Selection.activeObject;
Texture2D[] textures = terrainData.alphamapTextures;
string directory = "TerrainAlphaMaps/" + terrainData.name;
if (!Directory.Exists(directory)) {
Directory.CreateDirectory(directory);
}
for (int i = 0; i < textures.Length; i++) {
Texture2D texture = textures[i];
byte[] texData = texture.EncodeToPNG();
string filePath = directory + "/" + texture.name + ".png";
File.WriteAllBytes(filePath, texData);
}
EditorUtility.OpenWithDefaultApp(directory);
}
[MenuItem("Assets/Create/TerrainPNG", true)]
static bool IsTerrainData()
{
return Selection.activeObject != null && Selection.activeObject is TerrainData;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment