void BakeNavMesh2D() { // create a temporary parent GameObject var obj = new GameObject(); // find all static box colliders, add them to projection AddBoxCollider2Ds(obj.transform); // find all static circle colliders, add them to projection AddCircleCollider2Ds(obj.transform); // find all static polygon colliders, add them to projection AddPolygonCollider2Ds(obj.transform); // find all edge polygon colliders, add them to projection AddEdgeCollider2Ds(obj.transform); // find all tilemap colliders, add them to projection #if UNITY_2017_2_OR_NEWER AddTilemapCollider2Ds(obj.transform); #endif // min and max point from 2D colliders projected to 3D. // (scanning through 3D colliders doesn't work well because the polygon // GameObjects are pure meshes without colliders) var cols = GameObject.FindObjectsOfType <Collider2D>(); if (cols.Length > 0) { var min = new float2(float.MaxValue, float.MaxValue); var max = -min; foreach (var c in cols) { var minmax = NavMeshUtils2D.AdjustMinMax(c, min, max); min = minmax[0]; max = minmax[1]; } // create ground (cube instead of plane because it has unit size) // (pos between min and max; scaled to fit min and max * scale) // note: scale.y=0 so that *navmeshExtends doesn't make it too high var go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.name = "Ground"; // for debugging go.isStatic = true; go.transform.parent = obj.transform; float w = max.x - min.x; float h = max.y - min.y; go.transform.position = new Vector3(min.x + w / 2, -0.5f, min.y + h / 2); go.transform.localScale = new Vector3(w, 0, h) * navmeshExtends; } // bake navmesh asynchronously, clear mesh UnityEditor.AI.NavMeshBuilder.BuildNavMeshAsync(); // Async causes weird results if (gizmesh != null) { gizmesh.Clear(); } needsRebuild = true; // rebuild as soon as async baking is finished // delete the gameobjects now that the path was created GameObject.DestroyImmediate(obj); }
void BakeNavMesh2D() { // create a temporary parent GameObject var obj = new GameObject(); // find all static box colliders, add them to projection AddBoxCollider2Ds(obj.transform); // find all static circle colliders, add them to projection AddCircleCollider2Ds(obj.transform); // find all static polygon colliders, add them to projection AddPolygonCollider2Ds(obj.transform); // min and max point needed for ground plane (from 3d colliders) var cols = GameObject.FindObjectsOfType <Collider>(); if (cols.Length > 0) { var min = new Vector2(Mathf.Infinity, Mathf.Infinity); var max = -min; foreach (var c in cols) { var minmax = NavMeshUtils2D.AdjustMinMax(c, min, max); min = minmax[0]; max = minmax[1]; } // create ground (cube instead of plane because it has unit size) // (pos between min and max; scaled to fit min and max * scale) // note: scale.y=0 so that *groundScale doesn't make it too high var go = GameObject.CreatePrimitive(PrimitiveType.Cube); go.isStatic = true; go.transform.parent = obj.transform; float w = max.x - min.x; float h = max.y - min.y; go.transform.position = new Vector3(min.x + w / 2, -0.5f, min.y + h / 2); go.transform.localScale = new Vector3(w, 0, h) * groundScale; } // bake navmesh asynchronously, clear mesh NavMeshBuilder.BuildNavMeshAsync(); // Async causes weird results if (gizmesh != null) { gizmesh.Clear(); } needsRebuild = true; // rebuild as soon as async baking is finished // delete the gameobjects now that the path was created GameObject.DestroyImmediate(obj); }