static void createDestroy2DObject() { GameObject newObj = new GameObject("Destroy2D Object"); Destroy2D terrain = newObj.AddComponent <Destroy2D>(); terrain.rebuild(50, 50, 5, 5, 0, 0); Vector3 planePos = Vector3.zero; if (GameObject.Find("SceneCamera") != null) { Vector2 mousePos = new Vector2(GameObject.Find("SceneCamera").camera.pixelWidth / 2, GameObject.Find("SceneCamera").camera.pixelHeight / 2); Ray mouseRay = SceneView.lastActiveSceneView.camera.ScreenPointToRay(mousePos); Plane p = new Plane(Vector3.back, 0); float distance = 0; p.Raycast(mouseRay, out distance); mouseRay.direction = new Vector3(mouseRay.direction.x, mouseRay.direction.y, mouseRay.direction.z); planePos = mouseRay.origin + mouseRay.direction * distance - new Vector3(0.5f, -0.5f, 0) * terrain.terrainSize; } newObj.transform.position = planePos; }
public override void OnInspectorGUI() { Destroy2D terrain = (target as Destroy2D); drawBox(); EditorGUILayout.LabelField("Commands"); GUILayout.BeginHorizontal(); if (GUILayout.Button("Clear All")) { terrain.clearAll(); } GUILayout.EndHorizontal(); drawBox(); EditorGUILayout.LabelField("Options"); EditorGUI.indentLevel++; EditorGUIUtility.LookLikeInspector(); materialEditor = EditorGUILayout.ObjectField("Material", materialEditor, typeof(Material), false) as Material; terrainSizeEditor = EditorGUILayout.FloatField("Size", terrainSizeEditor); resolutionEditor = EditorGUILayout.IntField("Resolution", resolutionEditor); nodesPerMeshEditor = EditorGUILayout.IntField("Nodes Per Mesh", nodesPerMeshEditor); //colliderDepthEditor = EditorGUILayout.FloatField("Collider Depth", colliderDepthEditor); //sortingLayerEditor = EditorGUILayout.IntField("Sorting Layer", sortingLayerEditor); //sortingOrderEditor = EditorGUILayout.IntField("Sorting Order", sortingOrderEditor); if (terrain.nodesPerMesh < 1) { terrain.nodesPerMesh = 1; } if (terrain.nodesPerMesh > terrain.resolution) { terrain.nodesPerMesh = terrain.resolution; } EditorGUI.indentLevel--; if (materialEditor != terrain.material) { terrain.material = materialEditor; terrain.updateMaterial(); } if ((terrainSizeEditor != terrain.terrainSize) || (resolutionEditor != terrain.resolution) || (nodesPerMeshEditor != terrain.nodesPerMesh) || (colliderDepthEditor != terrain.colliderDepth) || (sortingLayerEditor != terrain.sortingLayer) || (sortingOrderEditor != terrain.sortingOrder)) { if (GUILayout.Button("Commit")) { terrain.rebuild(terrainSizeEditor, resolutionEditor, nodesPerMeshEditor, colliderDepthEditor, sortingLayerEditor, sortingOrderEditor); } } drawBox(); EditorGUILayout.LabelField("Paint"); EditorGUI.indentLevel++; terrain.brushIntensity = EditorGUILayout.Slider("Brush Intensity", terrain.brushIntensity, 0.1f, 1.0f); terrain.brushSize = EditorGUILayout.Slider("Brush Size", terrain.brushSize, terrain.getNodeSize(), terrain.terrainSize); string[] toolBarOptions = { "Add Terrain", "Remove Terrain", "Paint" }; toolBarSelection = GUILayout.Toolbar(toolBarSelection, toolBarOptions); drawBrush = (toolBarSelection == 0); eraseBrush = (toolBarSelection == 1); paint1 = paint2 = paint3 = paint4 = false; if (toolBarSelection == 2) { GUILayout.Label("Paint Options"); string[] toolBarOptionsPaint = { "Mask 1", "Mask 2", "Mask 3", "Mask 4", "Erase" }; //toolBarSelectionPaint = GUILayout.SelectionGrid(toolBarSelectionPaint, toolBarOptionsPaint, 5, new GUILayoutOption[]{GUILayout.Width(buttonWidth), GUILayout.Height(buttonWidth/5)}); toolBarSelectionPaint = GUILayout.Toolbar(toolBarSelectionPaint, toolBarOptionsPaint); paint1 = (toolBarSelectionPaint == 0); paint2 = (toolBarSelectionPaint == 1); paint3 = (toolBarSelectionPaint == 2); paint4 = (toolBarSelectionPaint == 3); paintErase = (toolBarSelectionPaint == 4); } }