AI优化:MoveAreaView性能优化。1.提前创建所有格子对象,避免运行时的动态创建销毁。2.通过控制对象激活状态管理显示,减少性能开销。3.移除了不必要的数据结构,提高代码清晰度。

main
刘涛 2025-03-26 17:30:00 +08:00
parent 356fccddf8
commit f62b3a1a2d
1 changed files with 42 additions and 49 deletions

View File

@ -17,8 +17,7 @@ namespace Gameplay.Area
public GameUnit owner;
public List<int> moveCellList = new List<int>();
public List<int> canNotMoveCellList = new List<int>();
public GameObject rootObj;
private bool _needRefresh;
@ -31,6 +30,11 @@ namespace Gameplay.Area
private List<BoardViewData> _boardViewDataList = new List<BoardViewData>();
public BoardLineObjPool boardLineObjPool = new BoardLineObjPool();
// 存储所有格子对象的数组
private GameObject[] _cellObjects;
// 标记是否已初始化格子对象
private bool _isInitialized = false;
public void Init(Map map)
{
@ -39,6 +43,9 @@ namespace Gameplay.Area
_map = map;
_InitBuffCellNotices(_buffNoticeObj.transform);
boardLineObjPool.Init();
// 提前初始化所有格子对象,避免首次刷新时卡顿
_InitializeCellObjects();
}
private void _InitBuffCellNotices(Transform parent)
@ -471,57 +478,39 @@ namespace Gameplay.Area
allCellList[cellIndex].isVisited = true;
}
// 重置并预分配容量
canNotMoveCellList.Clear();
if (canNotMoveCellList.Capacity < cellCount)
{
canNotMoveCellList.Capacity = cellCount;
}
// 收集不可移动格子
// 更新所有格子的显示状态
for (int i = 0; i < cellCount; i++)
{
var cell = allCellList[i];
if (!cell.isVisited)
{
canNotMoveCellList.Add(cell.index);
}
bool canNotMove = !allCellList[i].isVisited;
_cellObjects[i].SetActiveEx(canNotMove);
}
}
// 初始化所有格子对象的方法
private void _InitializeCellObjects()
{
if (_isInitialized) return;
var terrainGridSystem = LevelManager.Instance.CurrentLevel.Map.TerrainGridSystem;
var cellCount = terrainGridSystem.cells.Count;
// 创建对象数组
_cellObjects = new GameObject[cellCount];
// 预加载对象模板
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.MoveArea.CANNOT_MOVE_AREA_POINT_PATH);
// 批量创建所有对象
for (int i = 0; i < cellCount; i++)
{
_cellObjects[i] = Object.Instantiate(sampleObj, rootObj.transform);
// 设置到对应格子的世界位置
_cellObjects[i].transform.position = AreaManager.instance.GetWorldPosByIndex(i);
// 初始时全部隐藏
_cellObjects[i].SetActive(false);
}
int currentChildCount = rootObj.transform.childCount;
int targetChildCount = canNotMoveCellList.Count;
// 只有在需要时才调整子对象数量
if (targetChildCount != currentChildCount)
{
if (targetChildCount < currentChildCount)
{
// 从后向前删除多余对象
for (int i = currentChildCount - 1; i >= targetChildCount; i--)
{
var child = rootObj.transform.GetChild(i);
Object.Destroy(child.gameObject);
}
}
else
{
// 批量实例化所需对象
var sampleObj = AssetManager.Instance.GetPreLoadResult<GameObject>(PathDefine.MoveArea.CANNOT_MOVE_AREA_POINT_PATH);
for (int i = 0; i < targetChildCount - currentChildCount; i++)
{
Object.Instantiate(sampleObj, rootObj.transform);
}
}
}
// 更新位置
for (int i = 0; i < canNotMoveCellList.Count; i++)
{
var cellIndex = canNotMoveCellList[i];
var worldPos = AreaManager.instance.GetWorldPosByIndex(cellIndex);
var child = rootObj.transform.GetChild(i);
child.position = worldPos;
}
_isInitialized = true;
}
private void _RefreshMoveArea()
@ -563,6 +552,10 @@ namespace Gameplay.Area
notice.Dispose();
}
_buffCellNotices.Clear();
// 清理格子对象
_cellObjects = null;
_isInitialized = false;
if (rootObj != null)
{