void OnEnable() { lastTool = Tools.current; Tools.current = Tool.None; for (int i = 0; i < targets.Length; i++) { LowPolyTerrain terrain = (LowPolyTerrain)targets[i]; if (terrain.transform.childCount == 0) { terrain.GenerateDefaultMesh(); } else { for (int j = 0; j < terrain.transform.childCount; j++) { if (terrain.transform.GetChild(j).GetComponent <MeshFilter>().sharedMesh != null) { terrain.transform.GetChild(j).GetComponent <LowPolyTerrainChunk>().vertices = new List <Vector3>(terrain.transform.GetChild(j).GetComponent <MeshFilter>().sharedMesh.vertices); } } } } }
public override void OnInspectorGUI() { for (int i = 0; i < targets.Length; i++) { LowPolyTerrain terrain = (LowPolyTerrain)targets[i]; EditorGUILayout.BeginHorizontal(); if (GUILayout.Button("Modify")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.ModifyHeight; } else if (GUILayout.Button("Set")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.SetHeight; } else if (GUILayout.Button("Smooth")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.SmoothHeight; } else if (GUILayout.Button("Paint")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.Paint; } else if (GUILayout.Button("Randomize")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.Randomize; } else if (GUILayout.Button("Options")) { terrain.lowPolyTerrainMode = LowPolyTerrain.LowPolyTerrainMode.Option; } EditorGUILayout.EndHorizontal(); DrawLine(); if (terrain.lowPolyTerrainMode == LowPolyTerrain.LowPolyTerrainMode.ModifyHeight) { EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("brushSize").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Brush Size", serializedObject.FindProperty("brushSize").floatValue), 0, Mathf.Max(serializedObject.FindProperty("chunkWidth").intValue *serializedObject.FindProperty("chunksWidth").intValue, serializedObject.FindProperty("chunksHeight").intValue *serializedObject.FindProperty("chunkHeight").intValue) * 0.75f); serializedObject.FindProperty("brushStrength").floatValue = Mathf.Max(EditorGUILayout.FloatField("Brush Strength", serializedObject.FindProperty("brushStrength").floatValue), 0); serializedObject.FindProperty("brushSmoothness").floatValue = Mathf.Clamp01(EditorGUILayout.FloatField("Brush Smoothness", serializedObject.FindProperty("brushSmoothness").floatValue)); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); } } else if (terrain.lowPolyTerrainMode == LowPolyTerrain.LowPolyTerrainMode.SetHeight) { EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("brushSize").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Brush Size", serializedObject.FindProperty("brushSize").floatValue), 0, Mathf.Max(serializedObject.FindProperty("chunkWidth").intValue *serializedObject.FindProperty("chunksWidth").intValue, serializedObject.FindProperty("chunksHeight").intValue *serializedObject.FindProperty("chunkHeight").intValue) * 0.75f); serializedObject.FindProperty("terrainHeight").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Height", serializedObject.FindProperty("terrainHeight").floatValue), serializedObject.FindProperty("minHeight").floatValue, serializedObject.FindProperty("maxHeight").floatValue); GUILayout.Label("Pick height with shift left click"); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); } if (GUILayout.Button("Flatten Terrain")) { terrain.FlattenTerrain(); } } else if (terrain.lowPolyTerrainMode == LowPolyTerrain.LowPolyTerrainMode.SmoothHeight) { EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("brushSize").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Brush Size", serializedObject.FindProperty("brushSize").floatValue), 0, Mathf.Max(serializedObject.FindProperty("chunkWidth").intValue *serializedObject.FindProperty("chunksWidth").intValue, serializedObject.FindProperty("chunksHeight").intValue *serializedObject.FindProperty("chunkHeight").intValue) * 0.75f); serializedObject.FindProperty("smoothFactor").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Smooth Factor", serializedObject.FindProperty("smoothFactor").floatValue), 1, 10); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); } } else if (terrain.lowPolyTerrainMode == LowPolyTerrain.LowPolyTerrainMode.Paint) { EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("brushSize").floatValue = Mathf.Clamp(EditorGUILayout.FloatField("Brush Size", serializedObject.FindProperty("brushSize").floatValue), 0, Mathf.Max(serializedObject.FindProperty("chunkWidth").intValue *serializedObject.FindProperty("chunksWidth").intValue, serializedObject.FindProperty("chunksHeight").intValue *serializedObject.FindProperty("chunkHeight").intValue) * 0.75f); EditorGUILayout.PropertyField(serializedObject.FindProperty("materials"), true); serializedObject.FindProperty("materialIndex").intValue = Mathf.Clamp(EditorGUILayout.IntField("Material Index", serializedObject.FindProperty("materialIndex").intValue), 0, serializedObject.FindProperty("materials").arraySize - 1); serializedObject.FindProperty("materialMask").intValue = Mathf.Clamp(EditorGUILayout.IntField("Material Mask", serializedObject.FindProperty("materialMask").intValue), -1, serializedObject.FindProperty("materials").arraySize - 1); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); } if (GUILayout.Button("Fill Terrain")) { terrain.FillTerrain(); } } else if (terrain.lowPolyTerrainMode == LowPolyTerrain.LowPolyTerrainMode.Randomize) { EditorGUI.BeginChangeCheck(); GUILayout.Label("Randomize with perlin noise"); serializedObject.FindProperty("perlinScale").floatValue = Mathf.Min(8f, Mathf.Max(EditorGUILayout.FloatField("Perlin Noise Scale", serializedObject.FindProperty("perlinScale").floatValue), 0)); serializedObject.FindProperty("perlinStrength").floatValue = EditorGUILayout.FloatField("Perlin Strength", serializedObject.FindProperty("perlinStrength").floatValue); serializedObject.FindProperty("perlinPower2").boolValue = GUILayout.Toggle(serializedObject.FindProperty("perlinPower2").boolValue, "Exponential"); if (EditorGUI.EndChangeCheck()) { serializedObject.ApplyModifiedProperties(); } if (GUILayout.Button("Randomize with perlin noise")) { terrain.RandomizePerlinNoise(); } } else { GUILayout.Label("Warning: changing these properties will reset the terrain"); EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("chunksWidth").intValue = Mathf.Clamp(EditorGUILayout.IntField("Chunks X", serializedObject.FindProperty("chunksWidth").intValue), 1, 50); serializedObject.FindProperty("chunksHeight").intValue = Mathf.Clamp(EditorGUILayout.IntField("Chunks Z", serializedObject.FindProperty("chunksHeight").intValue), 1, 50); serializedObject.FindProperty("chunkWidth").intValue = Mathf.Clamp(EditorGUILayout.IntField("Chunk Width", serializedObject.FindProperty("chunkWidth").intValue), 1, 20); serializedObject.FindProperty("chunkHeight").intValue = Mathf.Clamp(EditorGUILayout.IntField("Chunk Height", serializedObject.FindProperty("chunkHeight").intValue), 1, 20); DrawLine(); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); terrain.GenerateDefaultMesh(); } EditorGUI.BeginChangeCheck(); EditorGUILayout.PropertyField(serializedObject.FindProperty("materials"), true); serializedObject.FindProperty("minHeight").floatValue = EditorGUILayout.FloatField("Minimum Height", serializedObject.FindProperty("minHeight").floatValue); serializedObject.FindProperty("maxHeight").floatValue = EditorGUILayout.FloatField("Maximum Height", serializedObject.FindProperty("maxHeight").floatValue); if (serializedObject.FindProperty("minHeight").floatValue >= serializedObject.FindProperty("maxHeight").floatValue) { serializedObject.FindProperty("minHeight").floatValue = serializedObject.FindProperty("maxHeight").floatValue; } terrain.ClampVertices(); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); terrain.GenerateMesh(); } float quadSize = serializedObject.FindProperty("quadSize").floatValue; EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("quadSize").floatValue = Mathf.Max(EditorGUILayout.FloatField("Quad Size", serializedObject.FindProperty("quadSize").floatValue), 0.0001f); if (EditorGUI.EndChangeCheck() == true) { if (quadSize != serializedObject.FindProperty("quadSize").floatValue) { serializedObject.ApplyModifiedPropertiesWithoutUndo(); terrain.ChangeQuadSize(quadSize); terrain.GenerateMesh(); } } EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("colliders").boolValue = GUILayout.Toggle(serializedObject.FindProperty("colliders").boolValue, "Generate Colliders"); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); for (int j = 0; j < terrain.transform.childCount; j++) { terrain.transform.GetChild(j).GetComponent <MeshCollider>().enabled = serializedObject.FindProperty("colliders").boolValue; } } EditorGUI.BeginChangeCheck(); serializedObject.FindProperty("flipNormals").boolValue = GUILayout.Toggle(serializedObject.FindProperty("flipNormals").boolValue, "Flip Normals"); if (EditorGUI.EndChangeCheck() == true) { serializedObject.ApplyModifiedProperties(); terrain.GenerateMesh(); } } GUILayout.Space(20); DrawLine(); if (GUILayout.Button("Generate Terrain")) { terrain.GenerateMesh(); } if (GUILayout.Button("Reset Terrain")) { terrain.GenerateDefaultMesh(); } } }