Exemplo n.º 1
0
    void UpdateNavMesh(bool asyncUpdate = false)
    {
        var defaultBuildSettings = NavMesh.GetSettingsByID(0);
        var bounds = QuantizedBounds();

        NavMeshBuilder.CollectSources(bounds, navMeshLayers, NavMeshCollectGeometry.RenderMeshes, 0, new List <NavMeshBuildMarkup>(), m_Sources);

        for (int i = 0; i < m_Sources.Count; i++)
        {
            if (m_Sources[i].component.transform.gameObject.layer == 19)
            {
                NavMeshBuildSource m = new NavMeshBuildSource();
                m            = m_Sources[i];
                m.area       = 3;
                m_Sources[i] = m;
            }
        }
        if (asyncUpdate)
        {
            m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
        }
        else
        {
            NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
        }
    }
    private void BuildNavMesh(bool Async)
    {
        Bounds navMeshBounds = new Bounds(Player.transform.position, NavMeshSize);
        List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();

        List <NavMeshModifier> modifiers;

        for (int index = 0; index < Surfaces.Length; index++)
        {
            if (Surfaces[index].collectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            for (int i = 0; i < modifiers.Count; i++)
            {
                if (((Surfaces[index].layerMask & (1 << modifiers[i].gameObject.layer)) == 1) &&
                    modifiers[i].AffectsAgentType(Surfaces[index].agentTypeID))
                {
                    markups.Add(new NavMeshBuildMarkup()
                    {
                        root            = modifiers[i].transform,
                        overrideArea    = modifiers[i].overrideArea,
                        area            = modifiers[i].area,
                        ignoreFromBuild = modifiers[i].ignoreFromBuild
                    });
                }
            }

            if (Surfaces[index].collectObjects == CollectObjects.Children)
            {
                NavMeshBuilder.CollectSources(transform, Surfaces[index].layerMask, Surfaces[index].useGeometry, Surfaces[index].defaultArea, markups, Sources);
            }
            else
            {
                NavMeshBuilder.CollectSources(navMeshBounds, Surfaces[index].layerMask, Surfaces[index].useGeometry, Surfaces[index].defaultArea, markups, Sources);
            }

            Sources.RemoveAll(RemoveNavMeshAgentPredicate);

            if (Async)
            {
                AsyncOperation navMeshUpdateOperation = NavMeshBuilder.UpdateNavMeshDataAsync(NavMeshDatas[index], Surfaces[index].GetBuildSettings(), Sources, navMeshBounds);
                navMeshUpdateOperation.completed += HandleNavMeshUpdate;
            }
            else
            {
                NavMeshBuilder.UpdateNavMeshData(NavMeshDatas[index], Surfaces[index].GetBuildSettings(), Sources, navMeshBounds);
                OnNavMeshUpdate?.Invoke(navMeshBounds);
            }
        }
    }
Exemplo n.º 3
0
        private List <NavMeshBuildSource> CollectSources()
        {
            List <NavMeshBuildSource> list  = new List <NavMeshBuildSource>();
            List <NavMeshBuildMarkup> list2 = new List <NavMeshBuildMarkup>();
            List <NavMeshModifier>    list3;

            if (this.m_CollectObjects == CollectObjects.Children)
            {
                list3 = new List <NavMeshModifier>(base.GetComponentsInChildren <NavMeshModifier>());
                list3.RemoveAll((NavMeshModifier x) => !x.isActiveAndEnabled);
            }
            else
            {
                list3 = NavMeshModifier.activeModifiers;
            }
            foreach (NavMeshModifier navMeshModifier in list3)
            {
                if ((this.m_LayerMask & 1 << navMeshModifier.gameObject.layer) != 0)
                {
                    if (navMeshModifier.AffectsAgentType(this.m_AgentTypeID))
                    {
                        list2.Add(new NavMeshBuildMarkup
                        {
                            root            = navMeshModifier.transform,
                            overrideArea    = navMeshModifier.overrideArea,
                            area            = navMeshModifier.area,
                            ignoreFromBuild = navMeshModifier.ignoreFromBuild
                        });
                    }
                }
            }
            if (this.m_CollectObjects == CollectObjects.All)
            {
                NavMeshBuilder.CollectSources(null, this.m_LayerMask, this.m_UseGeometry, this.m_DefaultArea, list2, list);
            }
            else if (this.m_CollectObjects == CollectObjects.Children)
            {
                NavMeshBuilder.CollectSources(base.transform, this.m_LayerMask, this.m_UseGeometry, this.m_DefaultArea, list2, list);
            }
            else if (this.m_CollectObjects == CollectObjects.Volume)
            {
                Matrix4x4 mat         = Matrix4x4.TRS(base.transform.position, base.transform.rotation, Vector3.one);
                Bounds    worldBounds = NavMeshSurface.GetWorldBounds(mat, new Bounds(this.m_Center, this.m_Size));
                NavMeshBuilder.CollectSources(worldBounds, this.m_LayerMask, this.m_UseGeometry, this.m_DefaultArea, list2, list);
            }
            if (this.m_IgnoreNavMeshAgent)
            {
                list.RemoveAll((NavMeshBuildSource x) => x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null);
            }
            if (this.m_IgnoreNavMeshObstacle)
            {
                list.RemoveAll((NavMeshBuildSource x) => x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null);
            }
            this.AppendModifierVolumes(ref list);
            return(list);
        }
    private void BuildNavMesh(bool Async)
    {
        Bounds navMeshBounds = new Bounds(Player.transform.position, NavMeshSize);
        List <NavMeshBuildMarkup> markups = new List <NavMeshBuildMarkup>();

        List <NavMeshModifier> modifiers;

        if (Surface.collectObjects == CollectObjects.Children)
        {
            modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
        }
        else
        {
            modifiers = NavMeshModifier.activeModifiers;
        }

        for (int i = 0; i < modifiers.Count; i++)
        {
            if (((Surface.layerMask & (1 << modifiers[i].gameObject.layer)) == 1) &&
                modifiers[i].AffectsAgentType(Surface.agentTypeID))
            {
                markups.Add(new NavMeshBuildMarkup()
                {
                    root            = modifiers[i].transform,
                    overrideArea    = modifiers[i].overrideArea,
                    area            = modifiers[i].area,
                    ignoreFromBuild = modifiers[i].ignoreFromBuild
                });
            }
        }

        if (Surface.collectObjects == CollectObjects.Children)
        {
            NavMeshBuilder.CollectSources(transform, Surface.layerMask, Surface.useGeometry, Surface.defaultArea, markups, Sources);
        }
        else
        {
            NavMeshBuilder.CollectSources(navMeshBounds, Surface.layerMask, Surface.useGeometry, Surface.defaultArea, markups, Sources);
        }

        Sources.RemoveAll(source => source.component != null && source.component.gameObject.GetComponent <NavMeshAgent>() != null);

        if (Async)
        {
            NavMeshBuilder.UpdateNavMeshDataAsync(NavMeshData, Surface.GetBuildSettings(), Sources, new Bounds(Player.transform.position, NavMeshSize));
        }
        else
        {
            NavMeshBuilder.UpdateNavMeshData(NavMeshData, Surface.GetBuildSettings(), Sources, new Bounds(Player.transform.position, NavMeshSize));
        }
    }
Exemplo n.º 5
0
    NavMeshData Build(NavMeshPrefabInstance instance)
    {
        var root    = instance.transform;
        var sources = new List <NavMeshBuildSource>();
        var markups = new List <NavMeshBuildMarkup>();

        NavMeshBuilder.CollectSources(root, ~0, NavMeshCollectGeometry.RenderMeshes, 0, markups, sources);
        var settings = NavMesh.GetSettingsByID(0);
        var bounds   = new Bounds(Vector3.zero, 1000.0f * Vector3.one);
        var navmesh  = NavMeshBuilder.BuildNavMeshData(settings, sources, bounds, root.position, root.rotation);

        navmesh.name = "Navmesh";
        return(navmesh);
    }
Exemplo n.º 6
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));

                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
            }
            else
#endif
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                    NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
            }

            foreach (NavMeshBuildSource a in sources)
            {
                if (a.component && a.component.transform.localPosition.sqrMagnitude > 10000 * 10000)
                {
                    Debug.LogError("too far away " + a.component.name, a.component);
                }
            }
            #if UNITY_EDITOR
            sources.RemoveAll((x) => (x.component != null && (GameObjectUtility.GetStaticEditorFlags(x.component.gameObject) & StaticEditorFlags.NavigationStatic) == 0));
            #endif

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);
#if UNITY_EDITOR
            for (var index = 0; index < sources.Count; index++)
            {
                var a = sources[index];
                a.area         = GameObjectUtility.GetNavMeshArea(a.component.gameObject);
                sources[index] = a;
            }
#endif

            return(sources);
        }
Exemplo n.º 7
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));

                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
            }
            else
#endif
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                    NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
            }

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects2d.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

            if (m_CollectObjects == CollectObjects2d.All)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Children)
            {
                NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Volume)
            {
                Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            if (m_CollectObjects == CollectObjects2d.Grid)
            {
                NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                var  grid = FindObjectOfType <Grid>();
                bool flag = true;
                foreach (var tilemap in grid.GetComponentsInChildren <Tilemap>())
                {
                    if (flag)
                    {
                        Debug.Log("Walkable " + tilemap.localBounds);
                        sources.Add(BoxSource(tilemap.localBounds.size.x, tilemap.localBounds.size.y));
                        flag = false;
                    }
                    int area     = m_DefaultArea;
                    var modifier = tilemap.GetComponent <NavMeshModifier>();
                    if (modifier != null && modifier.overrideArea)
                    {
                        area = modifier.area;
                    }
                    if (modifier != null && !modifier.ignoreFromBuild)
                    {
                        CollectGridSources(sources, tilemap, area);
                    }
                    else
                    {
                        var collider = tilemap.GetComponent <TilemapCollider2D>();
                        if (collider != null &&
                            (modifier == null || (modifier != null && !modifier.ignoreFromBuild)))
                        {
                            CollectGridSources(sources, tilemap, area);
                        }
                    }
                }

                Debug.Log("Sources " + sources.Count);
            }

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }
Exemplo n.º 9
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));

                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
            }
            else
#endif
            {
                if (m_CollectObjects == CollectObjects.All)
                {
                    NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Children)
                {
                    NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                    NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
            }

            // --- --- --- Terrain trees --- --- ---

            Terrain[] terrains = GameObject.FindObjectsOfType <Terrain>();

            if (terrains.Length > 0)
            {
                foreach (Terrain item in terrains)
                {
                    Terrain     terrain     = item.GetComponent <Terrain>();
                    TerrainData terrainData = terrain.terrainData;
                    Vector3     size        = Terrain.activeTerrain.terrainData.size;
                    Vector3     terrainPos  = terrain.GetPosition();
                    if (m_Debug)
                    {
                        Debug.Log("terrainPos = " + terrainPos);
                    }
                    int treesArea = NavMesh.GetAreaFromName("Not Walkable");
                    if (treesArea < 0)
                    {
                        Debug.LogError("Unrecognized area name! The default area will be used instead.");
                        treesArea = 0;
                    }

                    TreePrototype[] treePrototypes = terrainData.treePrototypes;
                    TreeInstance[]  treeInstances  = terrainData.treeInstances;
                    if (m_Debug)
                    {
                        Debug.Log("trees found = " + treeInstances.Length);
                    }
                    for (int i = 0; i < treeInstances.Length; i++)
                    {
                        //treeInstances[i] is the current ACTUAL tree we're going over.
                        //the tree prototype is the "template" used by this tree.
                        TreePrototype prototype = treePrototypes[treeInstances[i].prototypeIndex];
                        GameObject    prefab    = prototype.prefab;

                        NavMeshObstacle obstacle     = prefab.GetComponent <NavMeshObstacle>();
                        Collider        collider     = prefab.GetComponentInChildren <Collider>();
                        bool            obstacleBool = obstacle != null && (m_SelectionType == SelectionType.All || m_SelectionType == SelectionType.Obstacles);
                        bool            colliderBool = collider != null && (m_SelectionType == SelectionType.All || m_SelectionType == SelectionType.Colliders);
                        if (obstacleBool || colliderBool)
                        {
                            Vector3 center = Vector3.zero;
                            if (obstacle != null)
                            {
                                center = obstacle.center;
                            }
                            else if (collider != null)
                            {
                                center = collider.bounds.center;
                            }

                            if (m_Debug)
                            {
                                Debug.Log("treeInstances[" + i + "] info:\n" + treeInstances[i].position + " " + treeInstances[i].rotation + " " + treeInstances[i].widthScale + " " + treeInstances[i].heightScale);
                            }
                            Vector3 worldTreePos = terrainPos + Vector3.Scale(treeInstances[i].position, size) + center;

                            Quaternion worldTreeRot   = Quaternion.Euler(0, treeInstances[i].rotation * Mathf.Rad2Deg, 0);
                            Vector3    worldTreeScale = new Vector3(treeInstances[i].widthScale, treeInstances[i].heightScale, treeInstances[i].widthScale);
                            if (m_Debug)
                            {
                                Debug.Log("CREATED MATRIX FOR TRS:\nworldTreePos = " + worldTreePos + "\nworldTreeRot = " + worldTreeRot + "\nworldTreeScale = " + worldTreeScale);
                            }

                            NavMeshBuildSource src = new NavMeshBuildSource();
                            src.transform = Matrix4x4.TRS(worldTreePos, worldTreeRot, worldTreeScale);

                            if (obstacleBool)
                            {
                                switch (obstacle.shape)
                                {
                                case NavMeshObstacleShape.Capsule:
                                    src.shape = NavMeshBuildSourceShape.Capsule;

                                    //Unity 2019.2.0f1: BUG!! navMeshObstacle.height returns exactly HALF of the actual height of the obstacle.
                                    //Use the size property instead.
                                    src.size = obstacle.size;
                                    break;

                                case NavMeshObstacleShape.Box:
                                    src.shape = NavMeshBuildSourceShape.Box;
                                    src.size  = obstacle.size;
                                    break;

                                default:
                                    Debug.LogError("Unsupported type of " + typeof(NavMeshObstacleShape).Name
                                                   + " for the building of the " + typeof(NavMeshSurface).Name + "! (" + obstacle.shape + ")");
                                    break;
                                }
                            }
                            else if (colliderBool)
                            {
                                if (collider is CapsuleCollider)
                                {
                                    src.shape = NavMeshBuildSourceShape.Capsule;

                                    //Unity 2019.2.0f1: BUG!! navMeshObstacle.height returns exactly HALF of the actual height of the obstacle.
                                    //Use the size property instead.
                                    src.size = ((CapsuleCollider)collider).radius * Vector3.one * m_ColliderTolerance;
                                }
                                else if (collider is BoxCollider)
                                {
                                    src.shape = NavMeshBuildSourceShape.Box;
                                    src.size  = collider.bounds.size * m_ColliderTolerance;
                                }
                                else
                                {
                                    Debug.LogError("Unsupported type of " + typeof(NavMeshObstacleShape).Name
                                                   + " for the building of the " + typeof(NavMeshSurface).Name + "! (" + obstacle.shape + ")");
                                }
                            }

                            // Scale size
                            src.size = Vector3.Scale(src.size, prefab.transform.localScale);
                            if (m_Debug)
                            {
                                Debug.Log("src.size = " + src.size);
                            }
                            src.area = treesArea;
                            sources.Add(src);
                        }
                    }
                }
            }

            // --- --- --- End of Terrain trees --- --- ---

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }
Exemplo n.º 10
0
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects2d.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

            if (m_CollectObjects == CollectObjects2d.All)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Children)
            {
                NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            else if (m_CollectObjects == CollectObjects2d.Volume)
            {
                Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
            }
            if (m_CollectObjects == CollectObjects2d.Grid)
            {
                NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                var grid    = FindObjectOfType <Grid>();
                var colider = grid.GetComponentInChildren <TilemapCollider2D>();
                var tilemap = colider.GetComponent <Tilemap>();
                var bound   = tilemap.cellBounds;
                var vec3int = new Vector3Int(0, 0, 0);
                var size    = new Vector3(tilemap.layoutGrid.cellSize.x, .1f, tilemap.layoutGrid.cellSize.y);
                for (int i = bound.xMin; i < bound.xMax; i++)
                {
                    for (int j = bound.yMin; j < bound.yMax; j++)
                    {
                        vec3int.x = i;
                        vec3int.y = j;
                        if (!tilemap.HasTile(vec3int))
                        {
                            var src = new NavMeshBuildSource();
                            src.transform = Matrix4x4.Translate(tilemap.GetCellCenterWorld(vec3int));
                            Debug.Log(src.transform);
                            src.shape = NavMeshBuildSourceShape.Box;
                            src.size  = size;
                            sources.Add(src);
                        }
                    }
                }
                //var composite = grid.GetComponentInChildren<CompositeCollider2D>();
                //var count = composite.pathCount;
                ////TODO: 100
                //var points = new Vector2[100];
                //for (int i = 0; i < count; i++)
                //{
                //    var len = composite.GetPath(i, points);
                //    for (int j = 0; j < len; j++)
                //    {
                //        Debug.Log("Box:" + i + "-" + j + " = [" + points[j].x + ";" + points[j].y + "]");
                //    }
                //}
            }

            //sources.Add(BoxSource(40));

            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }
        List <NavMeshBuildSource> CollectSources()
        {
            var sources = new List <NavMeshBuildSource>();
            var markups = new List <NavMeshBuildMarkup>();

            List <NavMeshModifier> modifiers;

            if (m_CollectObjects == CollectObjects2d.Children)
            {
                modifiers = new List <NavMeshModifier>(GetComponentsInChildren <NavMeshModifier>());
                modifiers.RemoveAll(x => !x.isActiveAndEnabled);
            }
            else
            {
                modifiers = NavMeshModifier.activeModifiers;
            }

            foreach (var m in modifiers)
            {
                if ((m_LayerMask & (1 << m.gameObject.layer)) == 0)
                {
                    continue;
                }
                if (!m.AffectsAgentType(m_AgentTypeID))
                {
                    continue;
                }
                var markup = new NavMeshBuildMarkup();
                markup.root            = m.transform;
                markup.overrideArea    = m.overrideArea;
                markup.area            = m.area;
                markup.ignoreFromBuild = m.ignoreFromBuild;
                markups.Add(markup);
            }

#if UNITY_EDITOR
            if (!EditorApplication.isPlaying)
            {
                if (m_CollectObjects == CollectObjects2d.All)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects2d.Children)
                {
                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                else if (m_CollectObjects == CollectObjects2d.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));

                    UnityEditor.AI.NavMeshBuilder.CollectSourcesInStage(
                        worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, gameObject.scene, sources);
                }
                if (!hideEditorLogs && !Mathf.Approximately(transform.eulerAngles.x, 270f))
                {
                    Debug.LogWarning("NavMeshSurface2d is not rotated respectively to (x-90;y0;z0). Apply rotation unless intended.");
                }
                var builder = new NavMeshBuilder2dWrapper();
                builder.defaultArea     = defaultArea;
                builder.layerMask       = layerMask;
                builder.agentID         = agentTypeID;
                builder.useMeshPrefab   = useMeshPrefab;
                builder.overrideByGrid  = overrideByGrid;
                builder.compressBounds  = compressBounds;
                builder.overrideVector  = overrideVector;
                builder.CollectGeometry = useGeometry;
                builder.CollectObjects  = collectObjects;
                builder.parent          = gameObject;
                builder.hideEditorLogs  = hideEditorLogs;
                NavMeshBuilder2d.CollectSources(sources, builder);
            }
            else
#endif
            {
                if (m_CollectObjects == CollectObjects2d.All)
                {
                    NavMeshBuilder.CollectSources(null, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects2d.Children)
                {
                    NavMeshBuilder.CollectSources(transform, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                else if (m_CollectObjects == CollectObjects2d.Volume)
                {
                    Matrix4x4 localToWorld = Matrix4x4.TRS(transform.position, transform.rotation, Vector3.one);
                    var       worldBounds  = GetWorldBounds(localToWorld, new Bounds(m_Center, m_Size));
                    NavMeshBuilder.CollectSources(worldBounds, m_LayerMask, m_UseGeometry, m_DefaultArea, markups, sources);
                }
                if (!hideEditorLogs && !Mathf.Approximately(transform.eulerAngles.x, 270f))
                {
                    Debug.LogWarning("NavMeshSurface2d is not rotated respectively to (x-90;y0;z0). Apply rotation unless intended.");
                }
                var builder = new NavMeshBuilder2dWrapper();
                builder.defaultArea     = defaultArea;
                builder.layerMask       = layerMask;
                builder.agentID         = agentTypeID;
                builder.useMeshPrefab   = useMeshPrefab;
                builder.overrideByGrid  = overrideByGrid;
                builder.compressBounds  = compressBounds;
                builder.overrideVector  = overrideVector;
                builder.CollectGeometry = useGeometry;
                builder.CollectObjects  = collectObjects;
                builder.parent          = gameObject;
                builder.hideEditorLogs  = hideEditorLogs;
                NavMeshBuilder2d.CollectSources(sources, builder);
            }
            if (m_IgnoreNavMeshAgent)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshAgent>() != null));
            }

            if (m_IgnoreNavMeshObstacle)
            {
                sources.RemoveAll((x) => (x.component != null && x.component.gameObject.GetComponent <NavMeshObstacle>() != null));
            }

            AppendModifierVolumes(ref sources);

            return(sources);
        }