Created
June 30, 2014 14:52
-
-
Save sapphire-al2o3/1c51027845c48f3ad10a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using UnityEditor; | |
using System.Collections; | |
using System.IO; | |
public class BoxTerrain : EditorWindow | |
{ | |
Texture2D texture = null; | |
[MenuItem ("Editor/Box Terrain")] | |
static public void Init() | |
{ | |
BoxTerrain window = EditorWindow.GetWindow<BoxTerrain>(); | |
} | |
void OnGUI() | |
{ | |
EditorGUILayout.BeginHorizontal(); | |
if (GUILayout.Button("Load...", GUILayout.Width(100))) | |
{ | |
string path = EditorUtility.OpenFilePanel("Select png", "", "png"); | |
if (path.Length != 0) | |
{ | |
texture = new Texture2D(1, 1); | |
texture.LoadImage(File.ReadAllBytes(path)); | |
texture.filterMode = FilterMode.Point; | |
//texture = Resources.LoadAssetAtPath<Texture2D>(path); | |
//texture = EditorGUIUtility.Load(path) as Texture2D; | |
} | |
} | |
if (GUILayout.Button("Create Box", GUILayout.Width(100))) | |
{ | |
var pixels = texture.GetPixels32(); | |
int width = texture.width; | |
int height = texture.height; | |
float offsetX = width * 0.5f; | |
float offsetZ = height * 0.5f; | |
for (int i = 0; i < height; i++) | |
{ | |
for (int j = 0; j < width; j++) | |
{ | |
int k = i * width + j; | |
int y = pixels[k].r; | |
if (y > 0) | |
{ | |
int x = j; | |
int z = i; | |
var g = GameObject.CreatePrimitive(PrimitiveType.Cube); | |
g.transform.position = new Vector3(x - offsetX, y * 0.5f, z - offsetZ); | |
g.transform.localScale = new Vector3(1.0f, y, 1.0f); | |
g.name = "Cube" + k.ToString(); | |
Undo.RegisterCreatedObjectUndo(g, "Create Box"); | |
} | |
} | |
} | |
} | |
EditorGUILayout.EndHorizontal(); | |
if (texture != null) | |
{ | |
EditorGUI.DrawPreviewTexture(new Rect(25, 60, 100, 100), texture); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment