internal static void CollectGridSources(List <NavMeshBuildSource> sources, NavMeshBuilder2dWrapper builder)
        {
            var grid = GameObject.FindObjectOfType <Grid>();

            foreach (var modifier in grid.GetComponentsInChildren <NavMeshModifier>())
            {
                if (((0x1 << modifier.gameObject.layer) & builder.layerMask) == 0)
                {
                    continue;
                }
                if (!modifier.AffectsAgentType(builder.agentID))
                {
                    continue;
                }
                int area = builder.defaultArea;
                //if it is walkable
                if (builder.defaultArea != 1 && !modifier.ignoreFromBuild)
                {
                    var tilemap = modifier.GetComponent <Tilemap>();
                    if (tilemap != null)
                    {
                        if (builder.compressBounds)
                        {
                            tilemap.CompressBounds();
                        }

                        Debug.Log($"Walkable Bounds [{tilemap.name}]: {tilemap.localBounds}");
                        var box = BoxBoundSource(NavMeshSurface2d.GetWorldBounds(tilemap.transform.localToWorldMatrix, tilemap.localBounds));
                        box.area = builder.defaultArea;
                        sources.Add(box);
                    }
                }

                if (modifier.overrideArea)
                {
                    area = modifier.area;
                }
                if (!modifier.ignoreFromBuild)
                {
                    if (builder.CollectGeometry == NavMeshCollectGeometry.PhysicsColliders)
                    {
                        CollectSources(sources, modifier, area, builder);
                    }
                    else
                    {
                        var tilemap = modifier.GetComponent <Tilemap>();
                        if (tilemap != null)
                        {
                            CollectTileSources(sources, tilemap, area, builder);
                        }
                        var sprite = modifier.GetComponent <SpriteRenderer>();
                        if (sprite != null)
                        {
                            CollectSources(sources, sprite, area, builder);
                        }
                    }
                }
            }
            Debug.Log("Sources " + sources.Count);
        }
Example #2
0
        static void Unregister(NavMeshSurface2d surface)
        {
            s_NavMeshSurfaces.Remove(surface);

            if (s_NavMeshSurfaces.Count == 0)
            {
                NavMesh.onPreUpdate -= UpdateActive;
            }
        }
        static void Register(NavMeshSurface2d surface)
        {
            if (s_NavMeshSurfaces.Count == 0)
            {
                NavMesh.onPreUpdate += UpdateActive;
            }

            if (!s_NavMeshSurfaces.Contains(surface))
            {
                s_NavMeshSurfaces.Add(surface);
            }
        }
        internal static void CollectGridSources(List <NavMeshBuildSource> sources, int defaultArea, int layerMask, bool overrideByGrid, GameObject useMeshPrefab, bool compressBounds, Vector3 overrideVector)
        {
            var builder = new NavMeshBuilder2dWrapper();

            builder.defaultArea    = defaultArea;
            builder.layerMask      = layerMask;
            builder.useMeshPrefab  = useMeshPrefab;
            builder.overrideByGrid = overrideByGrid;
            builder.compressBounds = compressBounds;
            builder.overrideVector = overrideVector;
            var grid = GameObject.FindObjectOfType <Grid>();

            foreach (var tilemap in grid.GetComponentsInChildren <Tilemap>())
            {
                if (((0x1 << tilemap.gameObject.layer) & layerMask) == 0)
                {
                    continue;
                }
                int area     = defaultArea;
                var modifier = tilemap.GetComponent <NavMeshModifier>();
                //if it is walkable
                if (defaultArea != 1 && (modifier == null || (modifier != null && !modifier.ignoreFromBuild)))
                {
                    if (compressBounds)
                    {
                        tilemap.CompressBounds();
                    }

                    Debug.Log($"Walkable Bounds [{tilemap.name}]: {tilemap.localBounds}");
                    var box = BoxBoundSource(NavMeshSurface2d.GetWorldBounds(tilemap.transform.localToWorldMatrix, tilemap.localBounds));
                    box.area = defaultArea;
                    sources.Add(box);
                }

                if (modifier != null && modifier.overrideArea)
                {
                    area = modifier.area;
                }
                if (modifier != null && !modifier.ignoreFromBuild)
                {
                    CollectTileSources(sources, tilemap, area, builder);
                }
            }
            Debug.Log("Sources " + sources.Count);
        }
Example #5
0
        static void Register(NavMeshSurface2d surface)
        {
#if UNITY_EDITOR
            var isInPreviewScene = EditorSceneManager.IsPreviewSceneObject(surface);
            var isPrefab         = isInPreviewScene || EditorUtility.IsPersistent(surface);
            if (isPrefab)
            {
                //Debug.LogFormat("NavMeshData from {0}.{1} will not be added to the NavMesh world because the gameObject is a prefab.",
                //    surface.gameObject.name, surface.name);
                return;
            }
#endif
            if (s_NavMeshSurfaces.Count == 0)
            {
                NavMesh.onPreUpdate += UpdateActive;
            }

            if (!s_NavMeshSurfaces.Contains(surface))
            {
                s_NavMeshSurfaces.Add(surface);
            }
        }