Example #1
0
    void OnGUI()
    {
        GUILayout.BeginVertical();

        // instructions
        GUILayout.Label("Navigation2D by noobtuts.com\n");
        GUILayout.Label("Instructions:", EditorStyles.boldLabel);
        GUILayout.Label(" 1. Make Box/Circle-Collider2Ds Static");
        GUILayout.Label(" 2. Press Bake and wait until it's done");
        GUILayout.Label(" 3. Add NavMeshAgent2D to agents\n");
        GUILayout.Label("Notes:", EditorStyles.boldLabel);
        GUILayout.Label(" - Modify Window->Navigation->Agent Radius for path width");
        GUILayout.Label(" - Use GroundScale to cover the outside of your level\n");

        // options
        groundScale = EditorGUILayout.Slider("Ground Scale", groundScale, 1, 100);
        showNavMesh = EditorGUILayout.IntPopup("Show Navmesh", showNavMesh, new string[] { "Hide", "Wireframe", "Full" }, new int[] { 0, 1, 2 });

        // repaint scene if showNavMesh option changed
        if (GUI.changed)
        {
            SceneView.RepaintAll();
        }

        // buttons
        GUILayout.BeginHorizontal();
        if (GUILayout.Button("Clear"))
        {
            NavMeshBuilder.ClearAllNavMeshes();
            if (gizmesh != null)
            {
                gizmesh.Clear();
            }
        }
        if (NavMeshBuilder.isRunning)
        {
            if (GUILayout.Button("Cancel"))
            {
                NavMeshBuilder.Cancel();
            }
        }
        else
        {
            if (GUILayout.Button("Bake"))
            {
                BakeNavMesh2D();
            }
        }
        GUILayout.EndHorizontal();

        GUILayout.EndVertical();
    }
    public override IEnumerator Execute(UTContext context)
    {
        if (UTPreferences.DebugMode)
        {
            Debug.Log("Setting up nav mesh settings.");
        }
        var settingsObject               = new SerializedObject(NavMeshBuilder.navMeshSettingsObject);
        var agentRadiusSetting           = settingsObject.FindProperty("m_BuildSettings.agentRadius");
        var agentHeightSetting           = settingsObject.FindProperty("m_BuildSettings.agentHeight");
        var agentSlopeSetting            = settingsObject.FindProperty("m_BuildSettings.agentSlope");
        var ledgeDropHeightSetting       = settingsObject.FindProperty("m_BuildSettings.ledgeDropHeight");
        var agentClimbSetting            = settingsObject.FindProperty("m_BuildSettings.agentClimb");
        var maxJumpAcrossDistanceSetting = settingsObject.FindProperty("m_BuildSettings.maxJumpAcrossDistance");
        var accuratePlacementSetting     = settingsObject.FindProperty("m_BuildSettings.accuratePlacement");
        var minRegionAreaSetting         = settingsObject.FindProperty("m_BuildSettings.minRegionArea");
        var widthInaccuracySetting       = settingsObject.FindProperty("m_BuildSettings.widthInaccuracy");
        var heightInaccuracySetting      = settingsObject.FindProperty("m_BuildSettings.heightInaccuracy");

        agentRadiusSetting.floatValue           = agentRadius.EvaluateIn(context);
        agentHeightSetting.floatValue           = agentHeight.EvaluateIn(context);
        agentSlopeSetting.floatValue            = maxSlope.EvaluateIn(context);
        ledgeDropHeightSetting.floatValue       = dropHeight.EvaluateIn(context);
        agentClimbSetting.floatValue            = stepHeight.EvaluateIn(context);
        maxJumpAcrossDistanceSetting.floatValue = jumpDistance.EvaluateIn(context);
        accuratePlacementSetting.boolValue      = heightMesh.EvaluateIn(context);
        minRegionAreaSetting.floatValue         = minRegionArea.EvaluateIn(context);
        widthInaccuracySetting.floatValue       = widthInaccuracy.EvaluateIn(context);
        heightInaccuracySetting.floatValue      = heightInaccuracy.EvaluateIn(context);

        settingsObject.ApplyModifiedProperties();

        Debug.Log("Starting baking of nav mesh.");
        NavMeshBuilder.BuildNavMeshAsync();
        do
        {
            yield return("");

            if (context.CancelRequested)
            {
                NavMeshBuilder.Cancel();
            }
        } while(NavMeshBuilder.isRunning);
        Debug.Log("Nav mesh bake process finished.");
    }
        /// <summary>
        /// 创建/更新指定AgentType的指定范围的的Navmesh
        /// </summary>
        /// <param name="_agentType">AgentTypeID</param>
        /// <param name="_bounds">区域</param>
        /// <param name="_async">是否异步</param>
        private void UpdateNavmesh(int _agentType, Bounds _bounds, bool _async)
        {
            //如果不存在,则添加
            if (!navmeshTracker.ContainsKey(_agentType))
            {
                var data     = new NavMeshData(_agentType);
                var instance = NavMesh.AddNavMeshData(data);

                navmeshTracker.Add(_agentType, new NavmeshDataPair()
                {
                    data            = data,
                    settings        = settings.ContainsKey(_agentType) ? settings[_agentType] : NavMesh.GetSettingsByID(_agentType),
                    instance        = instance,
                    updateOperation = null
                });

                _async = false; //第一次必须非异步
            }

            var pair = navmeshTracker[_agentType];

            if (_async)
            {
                if (pair.updateOperation == null || pair.updateOperation.isDone)
                {
                    pair.updateOperation = null;
                    pair.updateOperation = NavMeshBuilder.UpdateNavMeshDataAsync(pair.data, pair.settings, sources, _bounds);
                }
            }
            else
            {
                if (pair.updateOperation != null && !pair.updateOperation.isDone)
                {
                    NavMeshBuilder.Cancel(pair.data);
                }

                pair.updateOperation = null;
                NavMeshBuilder.UpdateNavMeshData(pair.data, pair.settings, sources, _bounds);
            }
        }