public void SetSceneGameObjectInCell(int cellID) { SceneCellInfoInEditor cellInfo = new SceneCellInfoInEditor(); foreach (var cell in CurrentSceneInfo.cells) { if (cell.cellId == cellID) { cellInfo = cell; } } if (cellInfo == null) { return; } var itemIds = cellInfo.itemsNum; var allItemIds = CurrentSceneInfo.items; List <GameObject> gameObjectIncell = new List <GameObject>(); foreach (var targetItemId in itemIds) { foreach (var item in allItemIds) { if (targetItemId == item.objNum && !gameObjectIncell.Contains(item.obj)) { gameObjectIncell.Add(item.obj); } } } if (gameObjectIncell.Count > 0) { Selection.objects = gameObjectIncell.ToArray(); EditorGUIUtility.PingObject(gameObjectIncell[0]); } }
private void SetSceneCellObjectInfo(Transform sceneRoot, SceneInfoInEditor sceneInfo) { //初始化节点、预制物容器 List <SceneLayerInfo> layerList = new List <SceneLayerInfo>(); List <SceneItemInfoInEditor> prefabList = new List <SceneItemInfoInEditor>(); SceneCellInfoInEditor[] cellsInfo = new SceneCellInfoInEditor[CurrentSceneInfo.xCellCount * CurrentSceneInfo.zCellCount]; for (int i = 0; i < cellsInfo.Length; i++) { cellsInfo[i] = new SceneCellInfoInEditor(); cellsInfo[i].cellId = i; cellsInfo[i].itemsNum = new List <int>(); cellsInfo[i].OnlyInThisCellItemIds = new List <int>(); } //prefab总数 int prefabNum = 0; TraverseHierarchy(sceneRoot.transform, (Transform trans) => { UnityEngine.Object objPrefab = PrefabUtility.GetCorrespondingObjectFromSource(trans.gameObject); if (objPrefab != null) //如果对象是有预制物的对象(objPrefab为预制物对象,谨慎使用) { prefabNum++; return(true); } return(false); }); int itemNum = 0; //前序遍历所有节点,视prefab为叶 TraverseHierarchy(sceneRoot.transform, (Transform trans) => { EditorUtility.DisplayProgressBar("Title", string.Format("正在构建地图分块加载数据:{0}/{1}", itemNum, prefabNum), (float)itemNum / prefabNum); bool isPrefab = false; UnityEngine.Object objPrefab = PrefabUtility.GetCorrespondingObjectFromSource(trans.gameObject); if (objPrefab != null) //如果对象是有预制物的对象(objPrefab为预制物对象,谨慎使用) { isPrefab = true; string prefabPath = AssetDatabase.GetAssetPath(objPrefab); //Todo 可能需要添加获取资源其他信息 //prefabPath = CopyFileToResources(prefabPath); SceneItemInfoInEditor itemInfo = new SceneItemInfoInEditor(); itemInfo.parentLayer = MyMapTool.GetELaterByName(trans.parent.name, sceneRoot.name); List <int> numList; if (itemInfo.parentLayer == EMapLayer.Animation) { //如果是animation类型,则每个格子均加载 numList = new List <int>(sceneInfo.CellCount); for (int cellNum = 0; cellNum < sceneInfo.CellCount; cellNum++) { numList.Add(cellNum); } } else { numList = GetCellIdByGameObjectMesh(trans.gameObject, sceneInfo); //算出此游戏对象所在格子集合 } if (numList != null && numList.Count > 0 && itemInfo.parentLayer != EMapLayer.None) { //初始化物体info prefabList.Add(itemInfo); itemInfo.obj = trans.gameObject; itemInfo.pos = trans.position; itemInfo.scale = trans.lossyScale; itemInfo.rot = trans.eulerAngles; itemInfo.objNum = itemNum; itemInfo.objName = trans.name; itemInfo.prefabName = objPrefab.name; itemInfo.prefabAssetPath = prefabPath; //保存Item lightmap信息 Renderer[] renders = trans.gameObject.GetComponentsInChildren <Renderer>(); if (renders.Length > 0) { itemInfo.lightmapIndexes = new int[renders.Length]; itemInfo.lightmapScaleOffsets = new MyVector4[renders.Length]; itemInfo.realtimeLightmapIndexes = new int[renders.Length]; itemInfo.realtimeLightmapScaleOffsets = new MyVector4[renders.Length]; for (int i = 0; i < renders.Length; i++) { itemInfo.lightmapIndexes[i] = renders[i].lightmapIndex; itemInfo.lightmapScaleOffsets[i] = new MyVector4(renders[i].lightmapScaleOffset); itemInfo.realtimeLightmapIndexes[i] = renders[i].realtimeLightmapIndex; itemInfo.realtimeLightmapScaleOffsets[i] = new MyVector4(renders[i].realtimeLightmapScaleOffset); } } if (numList.Count == 1) { cellsInfo[numList[0]].OnlyInThisCellItemIds.Add(itemInfo.objNum); cellsInfo[numList[0]].itemsNum.Add(itemInfo.objNum); } else { foreach (int num in numList) //遍历对应编号的格子info,存入物体info { if (num >= 0 && num < cellsInfo.Length) { cellsInfo[num].itemsNum.Add(itemInfo.objNum); } } } itemNum++; } return(true); } else //如果对象不是预制物,则认为是层对象 { string layerName = trans.name; if (trans.gameObject == sceneRoot) { layerName = layerName.Split('#')[0]; } SceneLayerInfo layerInfo = new SceneLayerInfo(); layerInfo.layerName = layerName; layerInfo.pos = new MyVector3(trans.position); layerInfo.rot = new MyVector3(trans.eulerAngles); layerInfo.scal = new MyVector3(trans.lossyScale); if (trans.parent != null && trans != sceneRoot.transform) { layerInfo.parentLayer = MyMapTool.GetELaterByName(trans.parent.name, sceneRoot.name); } layerList.Add(layerInfo); } return(isPrefab); } ); EditorUtility.ClearProgressBar(); CurrentSceneInfo.layers = layerList.ToArray(); CurrentSceneInfo.items = prefabList.ToArray(); CurrentSceneInfo.cells = cellsInfo; }