// 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(); }
/// <summary> /// 创建地图 /// </summary> /// <param name="mapName">地图名称</param> /// <param name="mapWidth">小地图行数</param> /// <param name="mapLength">小地图列数</param> /// <param name="mapHight">地图高度</param> /// <param name="highttextureAcc">高度纹理精度</param> /// <param name="sevepath">保存路径</param> /// <param name="rawpath">.raw文件路径</param> /// <param name="texture2DArr">贴图数组</param> public static void CreateTerrain(string mapName, int mapWidth, int mapLength, int mapHight, float highttextureAcc, string sevepath, string rawpath, Texture2D[] texture2DArr) { string[] rawpaths = Directory.GetFiles(rawpath); if (rawpaths.Length != mapLength * mapWidth) { Debug.LogError(".raw资源不足或行列数配置错误!"); return; } string foderPath = sevepath + "/" + mapName; if (Directory.Exists(foderPath)) { DeleteDirectory(foderPath); } DirectoryInfo directoryinfo = Directory.CreateDirectory(foderPath); AssetDatabase.Refresh(); GameObject rootGo = new GameObject(mapName); string localpath = foderPath.Substring(foderPath.IndexOf("Assets")) + "/"; TerrainLayer[] tls = new TerrainLayer[texture2DArr.Length]; for (int k = 0; k < texture2DArr.Length; k++) { TerrainLayer tl = new TerrainLayer(); AssetDatabase.CreateAsset(tl, localpath + "TerrainLayer_" + k + ".asset"); tl.diffuseTexture = texture2DArr[k]; tls[k] = tl; } for (int i = 0; i < mapLength; i++) { for (int j = 0; j < mapWidth; j++) { TerrainData terrainData = new TerrainData(); AssetDatabase.CreateAsset(terrainData, localpath + "TD_" + (i * mapWidth + j) + ".asset"); terrainData.heightmapResolution = 513; terrainData.SetDetailResolution(512, 32); terrainData.terrainLayers = tls; terrainData.size = new Vector3(50, mapHight, 50); GameObject go = Terrain.CreateTerrainGameObject(terrainData); go.name = "T_" + (i * mapWidth + j); go.transform.parent = rootGo.transform; go.transform.localPosition = new Vector3(j * 50, 0, i * 50); ImportRawHeightmap importRawHeightmap = new ImportRawHeightmap(); Terrain ter = go.GetComponent <Terrain>(); ter.heightmapPixelError = highttextureAcc; importRawHeightmap.InitializeImportRaw(ter, rawpaths[i * mapWidth + j]); PrefabUtility.SaveAsPrefabAssetAndConnect(go, localpath + mapName + "_" + (i * mapWidth + j) + ".prefab", InteractionMode.AutomatedAction); } } }