/// <summary> /// 卸载格子 /// </summary> private void DoUnloadGrid() { if (m_NeedUnLoadGrids != null && m_NeedUnLoadGrids.Count > 0) { for (int iGrid = 0; iGrid < m_NeedUnLoadGrids.Count; iGrid++) { Vector3Int v3Int = m_NeedUnLoadGrids[iGrid]; long gridHashId = AreaLayerInfo.GetHashCode(v3Int); if (m_GridIndexs.ContainsKey(gridHashId)) { int index = m_GridIndexs[gridHashId]; if (m_AreaLayerInfo.AreaVirtualGridInfos.Count > index) { AreaVirtualGridInfo gridInfo = m_AreaLayerInfo.AreaVirtualGridInfos[index]; List <int> unitIndexs = gridInfo.m_UnitIndexs; if (unitIndexs != null && unitIndexs.Count > 0) { for (int iUnit = 0; iUnit < unitIndexs.Count; iUnit++) { m_UnitController.RemoveUnit(unitIndexs[iUnit]); } } } } else { Debug.LogError("不存在gridHashId:" + gridHashId); } } m_NeedUnLoadGrids.Clear(); } }
/// <summary> /// 检查能加载的Grid和卸载的grid /// </summary> private void CheckLoadOrUnLoadGrid() { //检查可卸载的Grid if (m_LoadedGrids != null && m_LoadedGrids.Count > 0) { m_RemoveLoaded.Clear(); HashSet <Vector3Int> .Enumerator iter = m_LoadedGrids.GetEnumerator(); while (iter.MoveNext()) { Vector3Int gridIndex = iter.Current; int offestX = Mathf.Abs(gridIndex.x - m_CurrentPosX); int offestY = Mathf.Abs(gridIndex.y - m_CurrentPosY); int offestZ = Mathf.Abs(gridIndex.z - m_CurrentPosZ); if (offestX > m_UnloadRange || offestY > m_UnloadRange || offestZ > m_UnloadRange) { m_NeedUnLoadGrids.Add(gridIndex); m_RemoveLoaded.Add(gridIndex); if (m_NeedLoadGrids.Contains(gridIndex)) { m_NeedLoadGrids.Remove(gridIndex); } } } if (m_RemoveLoaded != null && m_RemoveLoaded.Count > 0) { for (int iLoad = 0; iLoad < m_RemoveLoaded.Count; iLoad++) { m_LoadedGrids.Remove(m_RemoveLoaded[iLoad]); } m_RemoveLoaded.Clear(); } } //检查可加载的Grid int minIndexX = -m_UnloadRange + m_CurrentPosX; int maxIndexX = m_UnloadRange + m_CurrentPosX; int minIndexY = -m_UnloadRange + m_CurrentPosY; int maxIndexY = m_UnloadRange + m_CurrentPosY; int minIndexZ = -m_UnloadRange + m_CurrentPosZ; int maxIndexZ = m_UnloadRange + m_CurrentPosZ; minIndexX = Mathf.Max(minIndexX, m_AreaLayerInfo.m_MinIndexX); maxIndexX = Mathf.Min(maxIndexX, m_AreaLayerInfo.m_MaxIndexX); minIndexY = Mathf.Max(minIndexY, m_AreaLayerInfo.m_MinIndexY); maxIndexY = Mathf.Min(maxIndexY, m_AreaLayerInfo.m_MaxIndexY); minIndexZ = Mathf.Max(minIndexZ, m_AreaLayerInfo.m_MinIndexZ); maxIndexZ = Mathf.Min(maxIndexZ, m_AreaLayerInfo.m_MaxIndexZ); for (int iX = minIndexX; iX <= maxIndexX; iX++) { for (int iY = minIndexY; iY <= maxIndexY; iY++) { for (int iZ = minIndexZ; iZ <= maxIndexZ; iZ++) { Vector3Int gridIndex = new Vector3Int(iX, iY, iZ); long hashCode = AreaLayerInfo.GetHashCode(gridIndex); bool hasGridIndex = m_GridIndexs.ContainsKey(hashCode); if (hasGridIndex) { if (m_LoadedGrids.Add(gridIndex)) { m_NeedLoadGrids.Add(gridIndex); if (m_NeedUnLoadGrids.Contains(gridIndex)) { m_NeedUnLoadGrids.Remove(gridIndex); } } } } } } }