예제 #1
0
        private void CleanNavMesh(NavMesh2DBehaviour navMesh)
        {
            if (navMesh.NavMesh != null)
                DestroyImmediate(navMesh.NavMesh);

            if (navMesh.NavMesh2DNodes != null && navMesh.NavMesh2DNodes.Length != 0)
            {
                navMesh.NavMesh2DNodes = null;
            }

            if(navMesh.NodeNodeIndexQuadTree != null)
            {
                DestroyImmediate(navMesh.NodeNodeIndexQuadTree);
            }
            navMesh.NavMesh2DNodes = null;
            navMesh.Contours = null;
        }
        public override void OnInspectorGUI()
        {
            NavMesh2DBehaviour _sceneNavmesh = target as NavMesh2DBehaviour;

            if (_sceneNavmesh == null)
            {
                return;
            }

            GUIStyle informationPanel = new GUIStyle(EditorStyles.miniLabel);

            informationPanel.normal.background = EditorGUIUtility.whiteTexture;
            informationPanel.padding           = new RectOffset(10, 10, 10, 10);
            informationPanel.margin            = new RectOffset(10, 15, 20, 20);


            Color restoreColor = GUI.color;

            GUI.color = new Color(0, 0, 0, 0.25f);
            GUILayout.BeginVertical(informationPanel);
            GUI.color = restoreColor;

            EditorGUILayout.BeginHorizontal(EditorStyles.miniBoldLabel);
            EditorGUILayout.LabelField("NavMesh2D Properties", EditorStyles.whiteMiniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Walkable Layer: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.WalkableColliderLayer, EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Obstruction Layer: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.ObstructionColliderLayer, EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Circle Subdivision Factor: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.CircleColliderSubdivisionFactor.ToString(), EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Float Precision: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(Mathf.Log10(_sceneNavmesh.GenerationInformation.CalculationScaleFactor).ToString(), EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Obstruction Padding: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.ColliderPadding.ToString(), EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Join Type: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.JoinType.ToString(), EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.LabelField("Baked Grid: ", EditorStyles.miniLabel);
            EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.UseGrid ? "Yes" : "No", EditorStyles.miniLabel);
            EditorGUILayout.EndHorizontal();

            if (_sceneNavmesh.GenerationInformation.UseGrid)
            {
                EditorGUILayout.BeginHorizontal();
                EditorGUILayout.LabelField("Grid Size: ", EditorStyles.miniLabel);
                EditorGUILayout.LabelField(_sceneNavmesh.GenerationInformation.GridSize.ToString(), EditorStyles.miniLabel);
                EditorGUILayout.EndHorizontal();
            }

            GUILayout.EndVertical();
        }
예제 #3
0
        void RefreshReferences()
        {
            _showNavmesh = EditorPrefs.GetBool("PCTK_NM2DG_ShowNavMesh", true);
            _showNavmeshWire = EditorPrefs.GetBool("PCTK_NM2DG_ShowNavMeshWire", false);
            _showQuadTree = EditorPrefs.GetBool("PCTK_NM2DG_ShowQuadTree", false);
            _pathTester = EditorPrefs.GetBool("PCTK_NM2DG_ShowPathTester", false);
            string[] gridDrawColor = EditorPrefs.GetString("PCTK_NM2DG_DrawColor", "0,0.627,1,0.5").Split(',');
            _drawColor = new Color(float.Parse(gridDrawColor[0]), float.Parse(gridDrawColor[1]),
                                   float.Parse(gridDrawColor[2]), float.Parse(gridDrawColor[3]));

            string[] gridSizeString = EditorPrefs.GetString("PCTK_NM2DG_GridSize", "1.5,1.5").Split(',');
            GenerationInformation = new NavMesh2DBehaviour.NavmeshGenerationInformation
            {
                CalculationScaleFactor = EditorPrefs.GetFloat("PCTK_NM2DG_CalculationScaleFactor", 100f),
                CircleColliderSubdivisionFactor = EditorPrefs.GetFloat("PCTK_NM2DG_CircleColliderSubdivisionFactor", 0.25f),
                ColliderPadding = EditorPrefs.GetFloat("PCTK_NM2DG_ColliderPadding", 0.1f),
                UseGrid = EditorPrefs.GetBool("PCTK_NM2DG_EnableGrid", true),
                GridSize =  new Vector2(float.Parse(gridSizeString[0]), float.Parse(gridSizeString[1])),
                JoinType = (global::PigeonCoopToolkit.Navmesh2D.NavMesh2DBehaviour.JoinType)EditorPrefs.GetInt("PCTK_NM2DG_JoinType", (int)JoinType.jtSquare),
                ObstructionColliderLayer = EditorPrefs.GetString("PCTK_NM2DG_ObstructionColliderLayer", ""),
                WalkableColliderLayer = EditorPrefs.GetString("PCTK_NM2DG_WalkableColliderLayer", ""),
            };

            _navMeshPreviewMat = Resources.Load<Material>("PCTK/NM2DG/NavmeshOverlay");

            NavMesh2DBehaviour[] sceneNavMeshes = FindObjectsOfType<NavMesh2DBehaviour>();

            if (sceneNavMeshes.Length > 1)
            {
                for (int i = 0; i < sceneNavMeshes.Length; i++)
                {
                    DestroyImmediate(sceneNavMeshes[i].gameObject);
                }

                _sceneNavmesh = null;
            }
            else if (sceneNavMeshes.Length == 1)
            {
                _sceneNavmesh = sceneNavMeshes[0];

                if (VersionInformation().Match(_sceneNavmesh.Version,true) == false)
                {
                    EditorUtility.DisplayDialog("NavMesh2D Version Mismatch",
                                                string.Format("The 2D navmesh for this scene was built with an older version ({0}) of NavMesh2D, you will have to rebuild it with this version ({1})", _sceneNavmesh.Version, VersionInformation()),"OK");
                    DestroySceneNavMesh();
                }
            }
            else
            {
                _sceneNavmesh = null;
            }

            SceneView.RepaintAll();
            Repaint();
        }
예제 #4
0
        void RunGeneration()
        {
            GameObject tempNavMeshObject = new GameObject("NavMesh2D");
            NavMesh2DBehaviour tempNavMesh = tempNavMeshObject.AddComponent<NavMesh2DBehaviour>();

            try
            {
                EditorUtility.ClearProgressBar();

                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Finding Scene Colliders", 0.05f))
                    throw new System.OperationCanceledException("User canceled.");

                Collider2D[] allColliders = FindObjectsOfType<Collider2D>();
                if (allColliders.Length == 0)
                {
                    throw new System.Exception("No Collider2D's in Scene");
                }

                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Generating and clipping polygons", 0.1f))
                            throw new System.OperationCanceledException("User canceled.");

                PolyTreeEdgesRetained treeEdgesRetained = PrepairPolyTree(
                                                                allColliders.Where(a => a.gameObject.layer == LayerMask.NameToLayer(GenerationInformation.WalkableColliderLayer)).ToArray(),
                                                                allColliders.Where(a => a.gameObject.layer == LayerMask.NameToLayer(GenerationInformation.ObstructionColliderLayer)).ToArray()
                                                            );

                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Extracting visual data from polygons", 0.2f))
                    throw new System.OperationCanceledException("User canceled.");

                tempNavMesh.Contours = ExtractMeshContour(treeEdgesRetained);
                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Triangulating polygons", 0.3f))
                    throw new System.OperationCanceledException("User canceled.");

                List<Polygon> triangulatedPolygons = TriangulatePolyTree(treeEdgesRetained.Tree);
                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Genearting preview mesh", 0.4f))
                    throw new System.OperationCanceledException("User canceled.");
                tempNavMesh.NavMesh = GenerateMesh(triangulatedPolygons);
                if(EditorUtility.DisplayCancelableProgressBar("Generating NavMesh2D", "Generating node network", 0.5f))
                    throw new System.OperationCanceledException("User canceled.");
                tempNavMesh.NavMesh2DNodes = GenerateNodeNetwork(triangulatedPolygons, tempNavMesh.transform);
                EditorUtility.DisplayProgressBar("Generating NavMesh2D", "Finishing up, may take a minute or two", 1f);

                tempNavMesh.NodeNodeIndexQuadTree = tempNavMesh.gameObject.AddComponent<NodeIndexQuadTree>();

                List<NodeIndexQuadTree.VectorIndexPair> QuadTreeObjects = new List<NodeIndexQuadTree.VectorIndexPair>();
                for (int i = 0; i < tempNavMesh.NavMesh2DNodes.Length; i++)
                {
                    QuadTreeObjects.Add(new NodeIndexQuadTree.VectorIndexPair
                                            {Obj = i, Position = tempNavMesh.NavMesh2DNodes[i].position});
                }

                tempNavMesh.NodeNodeIndexQuadTree.SetIndicies(QuadTreeObjects.ToArray());

                tempNavMesh.NodeNodeIndexQuadTree.GenerateQuadTree(GenerationInformation.ObstructionColliderLayer,6);

                tempNavMesh.GenerationInformation = GenerationInformation;

                tempNavMesh.Version = VersionInformation();

                EditorUtility.SetDirty(tempNavMesh);
                EditorUtility.SetDirty(tempNavMesh.NodeNodeIndexQuadTree);

                foreach (var l in FindObjectsOfType<OffMeshLink2D>())
                {
                    l.ForceReset();
                }
                DestroySceneNavMesh();
                _sceneNavmesh = tempNavMesh;

                SceneView.RepaintAll();
                EditorUtility.ClearProgressBar();
            }
            catch (Exception e)
            {
                EditorUtility.DisplayProgressBar("Generating NavMesh2D", "Cleaning up, discarding all my hard work",1);
                CleanNavMesh(tempNavMesh);
                DestroyImmediate(tempNavMeshObject);
                EditorUtility.ClearProgressBar();
                if (e is OperationCanceledException == false)
                {
                    if(e.Source == "Poly2Tri")
                    {
                        EditorUtility.DisplayDialog("Error", "Your mesh is too dense, try a larger padding, larger grid or pick Miter/Square as your join type instead of Round" , "OK");
                    }
                    else
                    {
                        EditorUtility.DisplayDialog("Error", e.Message, "OK");
                    }
                }

            }
        }