public void Init(TerrainAssets config, float size) { m_asset = config; m_visibleSize = size; m_uvDis = 1f / m_asset.terrain_Size; }
//创建TerrainAssets索引配置 static TerrainAssets ExportTerrainIndexAssets(Terrain[] terrains) { string pathTerrainAssets = string.Format("{0}/TerrainData.asset", ExportFolder); TerrainAssets terrainAssets = GetStriptableObject <TerrainAssets>(pathTerrainAssets); terrainAssets.terrain_Row_Count = TERRAIN_ROW_COUNT; terrainAssets.terrain_Column_Count = TERRAIN_COLUMN_COUNT; terrainAssets.terrain_Size = TERRAIN_SIZE; terrainAssets.terrainIndex = new string[TERRAIN_ROW_COUNT * TERRAIN_COLUMN_COUNT]; foreach (Terrain terrain in terrains) { TerrainData terrainData = terrain.terrainData; if (terrainData.size.x == TERRAIN_SIZE && terrainData.size.z == TERRAIN_SIZE) { int x = (int)terrain.transform.localPosition.x; int z = (int)terrain.transform.localPosition.z; if (x % TERRAIN_SIZE == 0 && z % TERRAIN_SIZE == 0) { int index_x = x / TERRAIN_SIZE; int index_y = z / TERRAIN_SIZE; if (index_x > TERRAIN_ROW_COUNT || index_y > TERRAIN_COLUMN_COUNT) { Debug.LogErrorFormat("地形{0}坐标超出地图行:{1}列:{2}:{3}", terrain.name, TERRAIN_ROW_COUNT, TERRAIN_COLUMN_COUNT, terrain.transform.localPosition); continue; } int index = index_y * TERRAIN_COLUMN_COUNT + index_x; terrainAssets.terrainIndex[index] = terrainData.name; } else { Debug.LogErrorFormat("地形{0}坐标不是Size:{1}的倍数:{2}", terrain.name, TERRAIN_SIZE, terrain.transform.localPosition); } } else { Debug.LogErrorFormat("地形{0}大小错误:{1}", terrain.name, terrainData.size); } } UnityEditor.EditorUtility.SetDirty(terrainAssets); UnityEditor.AssetDatabase.SaveAssets(); return(terrainAssets); }
static void CreateTerrainRoot(TerrainAssets terrainAssets) { GameObject root = GameObject.Find(EXPORT_ROOT_NAME); GameObject.DestroyImmediate(root); root = new GameObject(EXPORT_ROOT_NAME); TerrainRoot hierarchy = root.AddComponent <TerrainRoot>(); GameObject go = new GameObject("Terrain"); go.transform.SetParent(root.transform); go.transform.localPosition = Vector3.zero; go.transform.localRotation = Quaternion.identity; go.transform.localScale = Vector3.one; //MeshFilter MeshFilter filter = go.GetComponent <MeshFilter>(); if (filter == null) { filter = go.AddComponent <MeshFilter>(); } //MeshRenderer MeshRenderer renderer = go.GetComponent <MeshRenderer>(); if (renderer == null) { renderer = go.AddComponent <MeshRenderer>(); } renderer.shadowCastingMode = UnityEngine.Rendering.ShadowCastingMode.Off; //Material Dictionary <int, Material> matDic = new Dictionary <int, Material>(); for (int i = 0; i < terrainAssets.terrainIndex.Length; i++) { string terrainName = terrainAssets.terrainIndex[i]; if (string.IsNullOrEmpty(terrainName)) { continue; } string pathMeshData = string.Format("{0}/{1}.asset", MeshDataExportFolder, terrainAssets.terrainIndex[i]); TerrainMeshData meshData = GetStriptableObject <TerrainMeshData>(pathMeshData); if (!matDic.ContainsKey(meshData.matIndex)) { matDic.Add(meshData.matIndex, meshData.material); } } Material[] mats = new Material[matDic.Count]; foreach (var kv in matDic) { mats[kv.Key] = kv.Value; } renderer.sharedMaterials = mats; //BoxCollider BoxCollider collider = go.GetComponent <BoxCollider>(); if (collider == null) { collider = go.AddComponent <BoxCollider>(); } hierarchy.RootTerrain = go; string path = string.Format("{0}/TerrainRoot.prefab", ExportFolder); if (File.Exists(path)) { GameObject prefab = AssetDatabase.LoadAssetAtPath <GameObject>(path); PrefabUtility.ReplacePrefab(root, prefab); } else { PrefabUtility.CreatePrefab(path, root); } GameObject.DestroyImmediate(root); }