diff --git a/Assets/MindPowerSdk/EditorWindow/MapDataEditorWindow.cs b/Assets/MindPowerSdk/EditorWindow/MapDataEditorWindow.cs
index 5fc0f3a..f156546 100644
--- a/Assets/MindPowerSdk/EditorWindow/MapDataEditorWindow.cs
+++ b/Assets/MindPowerSdk/EditorWindow/MapDataEditorWindow.cs
@@ -197,68 +197,142 @@ namespace MindPowerSdk.Editor
Debug.Log($"map size:({map.Width},{map.Height}) | section size:({map.SectionWidth},{map.SectionHeight})");
// 调用生成 Terrain 的方法,传入 mapName 作为标识
- TerrainGenerator.GenTerrain(map, chunkSize, terrainMaterial, parent.transform, entry.mapName,
- entry.isSmoothing);
+ var textureList = TerrainGenerator.GenTerrain(map, chunkSize, terrainMaterial, parent.transform,
+ entry.mapName,
+ entry.isSmoothing);
Debug.Log($"生成地图:{entry.mapName}\n数据路径:{mapDataPath}\n资源路径:{resourceDataPath}");
- CreateMapObject(resourceDataPath, map, parent.transform);
+ // 创建场景对象并关联到地形
+ CreateMapObjectsWithTerrain(resourceDataPath, map, parent.transform, entry.mapName, chunkSize);
}
}
-
- void CreateMapObject(string objPath, MPMap map, Transform parent)
+ ///
+ /// 创建场景对象并智能地关联到地形系统
+ ///
+ /// 场景对象文件路径
+ /// 地图数据
+ /// 父物体
+ /// 地图名称
+ /// 区块大小
+ void CreateMapObjectsWithTerrain(string objPath, MPMap map, Transform parent, string mapName, int chunkSize)
{
+ // 加载场景对象数据
CSceneObjSet sceneObjSet = new CSceneObjSet();
sceneObjSet.LoadBin("Assets/Resources/sceneobjinfo.bin");
SceneObjFile sceneObjFile = new SceneObjFile();
sceneObjFile.Load(objPath);
- string objName = Path.GetFileNameWithoutExtension(objPath);
- GameObject root = new GameObject($"{objName}_SceneAssetsRoot");
- root.transform.SetParent(parent);
+
+ // 找到地形根节点
+ Transform terrainRoot = parent.Find(mapName);
+ if (terrainRoot == null)
+ {
+ Debug.LogError($"找不到地形根节点: {mapName}");
+ return;
+ }
+
+ // 创建场景对象根节点
+ string objName = Path.GetFileNameWithoutExtension(objPath);
+ GameObject sceneRoot = new GameObject($"{objName}_SceneObjects");
+ sceneRoot.transform.SetParent(parent);
+ // 收集所有地形组件,用于场景对象关联
+ Dictionary terrainMap = new Dictionary();
+ CollectTerrainComponents(terrainRoot, terrainMap, chunkSize);
+
+ // 遍历所有场景对象
for (int y = 0; y < sceneObjFile.FileHeader.SectionCntY; y++)
{
for (int x = 0; x < sceneObjFile.FileHeader.SectionCntX; x++)
{
- var list = sceneObjFile.objInfos[x, y];
- if (list is null) continue;
+ var objList = sceneObjFile.objInfos[x, y];
+ if (objList == null) continue;
- foreach (var sceneObjInfo in list)
+ foreach (var sceneObjInfo in objList)
{
- if (sceneObjInfo.GetTypeId() == 1)
- continue;
-
- int id = sceneObjInfo.GetID();
+ if (sceneObjInfo.GetTypeId() == 1) continue; // 跳过特效物件
+ int id = sceneObjInfo.GetID();
CSceneObjInfo modeInfo = sceneObjSet.Get(id);
- Vector3 pos = new Vector3(sceneObjInfo.X / 100f, sceneObjInfo.HeightOff / 100f,
- sceneObjInfo.Y / 100f * -1f);
- float mapHeight = map.GetHeight(pos.x, (sceneObjInfo.Y / 100f));
- pos.y += mapHeight;
+ // 计算世界坐标,确保与地形坐标系一致
+ // 地图坐标系转换:X不变,Z轴翻转以匹配地形位置计算
+ float objX = sceneObjInfo.X / 100f;
+ float objY = sceneObjInfo.HeightOff / 100f;
+ float objZ = map.Height - (sceneObjInfo.Y / 100f); // 与地形位置计算保持一致
- //Quaternion rot = Quaternion.AngleAxis(sceneObjInfo.YawAngle, Vector3.up);
- string modePath = "Assets/Resources/Model/Scene/" +
- Path.GetFileNameWithoutExtension(modeInfo.szDataName) + ".lmo.obj";
- Object mode = AssetDatabase.LoadAssetAtPath