1.先添加 AI Navigation组件

2.Windows-->AI-->Navigation(Obsolete) 这样子就可以看到烘焙按钮
3.将物体标记为行走和不可行走

4.添加一个Plane和一些球体,并把需要形成NavMesh的物体选择为静态 // 因为只能烘焙静态的
之后可以看出烘焙后,看着被挖空了一样,有些多边形

5.添加一个Player 和 Target,其中Player添加上导航组件
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
public class AI : MonoBehaviour
{
private NavMeshAgent agent;
private Transform target;
void Start()
{
agent = GetComponent<NavMeshAgent>();
target = GameObject.Find("Target").transform;
}
void Update()
{
agent.SetDestination(target.position);
}
}
这样子Unity启动的时候,就会躲开障碍了。
6.接下来,我们是要导出这个数据给java服务器使用了,我们思考下如何搞?
初步的思考:可以看出,其实这个NavMesh就是一些多边形信息而已。那我们可以想象下,为了给服务端使用,我们暴露的接口是什么? 必然是:根据这些多边形信息构建出一个对象,NavMeshData, 然后传入 start 和 end 2个坐标,然后返回一个List<float[]> list,仅此而已。
然而,NavMesh导出后,仅仅是一些多边形信息,根本是没办法寻路的,要想寻路。