提问人:Povst 提问时间:11/14/2023 更新时间:11/16/2023 访问量:37
在 unity2D 中运行时异步构建导航网格
Building navmesh asynchronously during runtime in unity2D
问:
我面临的问题是,我的游戏场景会根据玩家的行为而变化(他可以设置障碍物),导航网格代理应该考虑这些变化并销毁它们。
所以我需要在游戏时重新计算 navmesh 代理的路径,我找到了函数 navMeshSurface.BuildNavMeshAsync();希望我能烘烤,游戏不会停止。调用此方法时,游戏将冻结。
异步可能有什么问题?
也许还有重新烘烤的替代方案,请给我出主意
上次迭代
private async void Update()
{
if(Input.GetKeyDown(KeyCode.N))
{
SpawnNextAtRandomSpawner();
navMeshSurface = GameObject.Find("Navmesh").GetComponent<NavMeshSurface>();
await UpdateNavMeshAsync();
}
}
async Task UpdateNavMeshAsync()
{
navMeshSurface.RemoveData();
navMeshSurface.BuildNavMeshAsync(); //Freeze moment
}
答:
1赞
BugFinder
11/16/2023
#1
你想得太多了
因此,此链接将带您观看以下操作的视频。
public class spawner : MonoBehaviour
{
public int Delay = 5;
public GameObject cube;
public NavMeshSurface surface;
private float nextspawntime = 0;
Vector3 newpos()
{
return new Vector3(Random.Range(-24,24),0,Random.Range(-24,24));
}
void Update()
{
nextspawntime += Time.deltaTime;
if (nextspawntime> Delay)
{
nextspawntime = 0;
Instantiate(cube, newpos(), Quaternion.identity);
}
}
}
评论
0赞
Povst
11/16/2023
从未听说过 2d 项目的上下文中该组件,但它工作得很好。多谢!
0赞
BugFinder
11/16/2023
NavMesh 不是特定于 2D 的,它也是 3D 的
评论
Update
Update