public static void DrawMeshTerrainDropZone(DropZoneType dropZoneType, MeshTerrain meshTerrain, ref Boolean addedItem) { Event evt = Event.current; Texture2D iconTexture = GetDropZoneIconTexture(dropZoneType); Rect dropArea = GUILayoutUtility.GetRect(iconTexture.width, iconTexture.height, GUILayout.ExpandWidth(false)); GUILayoutUtility.GetRect(5, iconTexture.height, GUILayout.ExpandWidth(false)); EditorGUI.DrawPreviewTexture(dropArea, iconTexture); switch (evt.type) { case EventType.DragUpdated: case EventType.DragPerform: if (!dropArea.Contains(evt.mousePosition)) { return; } bool hasType = HasDropComponentType(DragAndDrop.objectReferences, dropZoneType); if (!hasType) return; DragAndDrop.visualMode = DragAndDropVisualMode.Copy; if (evt.type == EventType.DragPerform) { DragAndDrop.AcceptDrag(); foreach (Object draggedObject in DragAndDrop.objectReferences) { GameObject droppedgo; switch (dropZoneType) { case DropZoneType.Terrain: droppedgo = draggedObject as GameObject; if (!droppedgo) break; addedItem = true; meshTerrain.AddTerrain(droppedgo, TerrainSourceID.TerrainSourceID1); break; case DropZoneType.MeshRenderer: droppedgo = draggedObject as GameObject; if (!droppedgo) break; addedItem = true; meshTerrain.AddMeshRenderer(droppedgo, TerrainSourceID.TerrainSourceID1); break; } } } break; } }
void CreateMeshTerrains(bool addToVegetationSystem) { for (int i = 0; i <= _meshRendererList.Count - 1; i++) { MeshRenderer meshRenderer = _meshRendererList[i]; if (meshRenderer == null) { continue; } GameObject go = new GameObject("MeshTerrain_" + meshRenderer.name); if (_addAsChild) { go.transform.SetParent(meshRenderer.transform); } MeshTerrain meshTerrain = go.AddComponent <MeshTerrain>(); MeshTerrainData meshTerrainData = CreateInstance <MeshTerrainData>(); if (!AssetDatabase.IsValidFolder("Assets/MeshTerrainData")) { AssetDatabase.CreateFolder("Assets", "MeshTerrainData"); } string filename = "MeshTerrainData_" + Guid.NewGuid() + ".asset"; AssetDatabase.CreateAsset(meshTerrainData, "Assets/MeshTerrainData/" + filename); MeshTerrainData loadedMeshTerrainData = AssetDatabase.LoadAssetAtPath <MeshTerrainData>("Assets/MeshTerrainData/" + filename); meshTerrain.MeshTerrainData = loadedMeshTerrainData; meshTerrain.AddMeshRenderer(meshRenderer.gameObject, TerrainSourceID.TerrainSourceID1); meshTerrain.GenerateMeshTerrain(); if (addToVegetationSystem) { VegetationStudioManager.AddTerrain(go, true); } } }