/// <summary> /// 初始化 /// </summary> public void Initialize(AreaLayerInfo areaLayerInfo, AssetInfo[] assetInfos, AreaController areaController, int unloadRange = 3) { m_AreaController = areaController; m_UnloadRange = unloadRange; m_AreaLayerInfo = areaLayerInfo; m_GridIndexs = new Dictionary <long, int>(); m_LoadedGrids = new HashSet <Vector3Int>(); m_NeedLoadGrids = new List <Vector3Int>(); m_NeedUnLoadGrids = new List <Vector3Int>(); m_UnitController = new UnitController(); m_UnitController.Initialize(areaLayerInfo.m_Units, assetInfos, m_AreaController); //初始化虚拟格子 if (m_AreaLayerInfo != null) { List <long> gridIndexs = m_AreaLayerInfo.AreaVirtualGridIndexs; if (gridIndexs != null && gridIndexs.Count > 0) { for (int iGrid = 0; iGrid < gridIndexs.Count; iGrid++) { //导出索引时 要有个检查过程 判断是否有重复 m_GridIndexs.Add(gridIndexs[iGrid], iGrid); } } } }
/// <summary> /// 初始化区域层 /// </summary> private void InitAreaLayer() { if (m_AreaDetailInfo == null) { return; } if (m_AreaLayerControllers == null) { m_AreaLayerControllers = new List <AreaLayerController>(); } AreaLayerInfo[] areaLayerInfos = m_AreaDetailInfo.AreaLayerInfos; if (areaLayerInfos != null && areaLayerInfos.Length > 0) { for (int iLayer = 0; iLayer < areaLayerInfos.Length; iLayer++) { AreaLayerInfo areaLayerInfo = areaLayerInfos[iLayer]; if (areaLayerInfo == null) { continue; } AreaLayerController areaLayerController = new AreaLayerController(); areaLayerController.Initialize(areaLayerInfo, m_AreaDetailInfo.AssetInfos, this, areaLayerInfo.m_Offest); m_AreaLayerControllers.Add(areaLayerController); } } }
/// <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> /// AreaDetialInfo写入二进制文件 /// </summary> /// <param name="binaryFile"></param> /// <param name="areaDetailInfo"></param> private void WriteAreaDetailInfoBinary(BinaryFile binaryFile) { BinaryWriter writer = binaryFile.m_Writer; AssetInfo[] assetInfos = this.AssetInfos; if (assetInfos == null || assetInfos.Length <= 0) { writer.Write(0); } else { writer.Write(assetInfos.Length); for (int iAsset = 0; iAsset < assetInfos.Length; iAsset++) { AssetInfo assetInfo = assetInfos[iAsset]; assetInfo.Serialize(writer); } } AreaLayerInfo[] areaLayerInfos = this.AreaLayerInfos; if (areaLayerInfos == null || areaLayerInfos.Length <= 0) { writer.Write(0); } else { writer.Write(areaLayerInfos.Length); for (int iLayer = 0; iLayer < areaLayerInfos.Length; iLayer++) { AreaLayerInfo layerInfo = areaLayerInfos[iLayer]; layerInfo.Serialize(writer); } } }
private void OnDrawGizmosSelected() { if (m_AreaLayerInfos != null && m_AreaLayerInfos.Length > 0) { for (int iLayer = 0; iLayer < m_AreaLayerInfos.Length; iLayer++) { //if(m_ShowLayer[iLayer]) { AreaLayerInfo layerInfo = m_AreaLayerInfos[iLayer]; if (layerInfo != null) { List <AreaVirtualGridInfo> areaVirtualGridInfos = layerInfo.AreaVirtualGridInfos; if (areaVirtualGridInfos != null && areaVirtualGridInfos.Count > 0) { for (int iArea = 0; iArea < areaVirtualGridInfos.Count; iArea++) { AreaVirtualGridInfo gridInfo = areaVirtualGridInfos[iArea]; if (m_ShowLayer[iLayer]) { Gizmos.color = m_Colors[iLayer]; Gizmos.DrawWireCube(gridInfo.m_Position, new Vector3(layerInfo.m_GridSize, layerInfo.m_GridSize, layerInfo.m_GridSize)); } if (m_ShowUnitAB[iLayer]) { List <int> unitIndexs = gridInfo.m_UnitIndexs; if (unitIndexs != null && unitIndexs.Count > 0) { for (int iUnit = 0; iUnit < unitIndexs.Count; iUnit++) { SceneUnitInfo unitInfo = layerInfo.m_Units[unitIndexs[iUnit]]; Gizmos.color = new Color(1, 1, 1, 0.2f); Gizmos.DrawWireCube(unitInfo._AABB.center, unitInfo._AABB.size); } } } } } } } } } }
public void Deserialize(byte[] bytes) { if (bytes == null) { return; } BinaryFile file = new BinaryFile(System.Text.Encoding.Unicode); if (!file.OpenRead(bytes)) { file.Close(); return; } BinaryReader reader = file.m_Reader; int assetInfoCount = reader.ReadInt32(); if (assetInfoCount > 0) { AssetInfos = new AssetInfo[assetInfoCount]; for (int iAsset = 0; iAsset < assetInfoCount; iAsset++) { AssetInfo assetInfo = new AssetInfo(); assetInfo.DeSerialize(reader); AssetInfos[iAsset] = assetInfo; } } int areaLayerCount = reader.ReadInt32(); if (areaLayerCount > 0) { AreaLayerInfos = new AreaLayerInfo[areaLayerCount]; for (int iLayer = 0; iLayer < areaLayerCount; iLayer++) { AreaLayerInfo areaLayerInfo = new AreaLayerInfo(); areaLayerInfo.DeSerialize(reader); AreaLayerInfos[iLayer] = areaLayerInfo; } } file.Close(); }
public void ShowSceneUI() { return; if (m_AreaLayerInfos != null && m_AreaLayerInfos.Length > 0) { for (int iLayer = 0; iLayer < m_AreaLayerInfos.Length; iLayer++) { if (m_ShowLayer[iLayer]) { AreaLayerInfo layerInfo = m_AreaLayerInfos[iLayer]; if (layerInfo != null) { List <AreaVirtualGridInfo> areaVirtualGridInfos = layerInfo.AreaVirtualGridInfos; if (areaVirtualGridInfos != null && areaVirtualGridInfos.Count > 0) { for (int iArea = 0; iArea < areaVirtualGridInfos.Count; iArea++) { AreaVirtualGridInfo gridInfo = areaVirtualGridInfos[iArea]; List <int> unitIndexs = gridInfo.m_UnitIndexs; if (unitIndexs != null && unitIndexs.Count > 0) { for (int iUnit = 0; iUnit < unitIndexs.Count; iUnit++) { SceneUnitInfo unitInfo = layerInfo.m_Units[unitIndexs[iUnit]]; AssetInfo assetInfo = m_AreaDetailInfo.AssetInfos[unitInfo.AssetIndex]; Handles.Label(unitInfo.LocalPosition, assetInfo.AddressableKey); } } } } } } } } }
/// <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); } } } } } } }