// Draw the property inside the given rect public override void OnGUI(Rect position, SerializedProperty property, GUIContent label) { GUILayout.BeginHorizontal(); GUILayout.Label(label); RawFileData data = null; if (property.objectReferenceValue is RawFileData) { data = property.objectReferenceValue as RawFileData; } if (data != null && data.previewTexture != null) { GUILayout.Box(data.previewTexture, GUILayout.Width(64), GUILayout.Height(64)); } GUILayout.BeginVertical(); if (GUILayout.Button("Import...")) { string text = EditorUtility.OpenFilePanel("Import Raw Heightmap", string.Empty, "raw"); if (text != string.Empty) { ImportRawHeightmap importRawHeightmap = TerrainWizard.DisplayTerrainWizard <ImportRawHeightmap>("Import Heightmap", "Import"); importRawHeightmap.InitializeImportRaw(text, (float[,] values) => { UpdateTexture(data, values); }); } } if (data != null && data.previewTexture != null) { if (GUILayout.Button("Clear")) { Clear(data); } } GUILayout.EndVertical(); GUILayout.EndHorizontal(); }
private void UpdateTexture(RawFileData data, float[,] values) { Undo.RecordObject(data, "select raw file"); int w = values.GetLength(0); int h = values.GetLength(1); Texture2D tex = new Texture2D(64, 64); Color[] colors = new Color[tex.width * tex.height]; for (int i = 0; i < tex.width; i++) { for (int j = 0; j < tex.height; j++) { float value = values[i * w / tex.width, j *h / tex.height]; Color color = new Color(); color.a = 1; color.r = color.g = color.b = value; colors[j * tex.width + i] = color; } } tex.SetPixels(colors); tex.Apply(); data.previewTexture = tex; data.width = w; data.height = h; data.values = new float[w * h]; for (int i = 0; i < data.width; i++) { for (int j = 0; j < data.height; j++) { data.values[j * data.width + i] = values[i, j]; } } }
private void Clear(RawFileData data) { Undo.RecordObject(data, "clear raw data"); data.width = 0; data.height = 0; data.previewTexture = null; data.values = null; }
public override void Init() { rawFile = new RawFileData(); }