Exemplo n.º 1
0
 void OnDestroy()
 {
     RecastNavMesh.Release();
 }
Exemplo n.º 2
0
    /// <summary>
    /// Draws all the editor properties needed to generate a NavMesh with Recast
    /// </summary>
    private void DrawPropertiesPanel()
    {
        var catagoryStyle = new GUIStyle();

        catagoryStyle.fontStyle = FontStyle.Bold;

        _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, false, false); //Last two bools are if we want to show the scroll bar always.
        EditorGUILayout.BeginVertical();
        EditorGUILayout.PrefixLabel("Properties", GUIStyle.none, catagoryStyle);

        Tags = EditorGUILayout.MaskField("Tags", Tags, UnityEditorInternal.InternalEditorUtility.tags);

        if (Tags != OldTags)
        {
            BuildConfig();
            OldTags = Tags;
        }

        // -1 = everything
        // 0 = nothing
        // >0 = tags up to "everything"
        if (Tags != 0)
        {
            EditorGUILayout.LabelField(
                string.Format("Verts: {0}", Verts),
                string.Format("Tris: {0}", Tris));
        }
        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Rasterization", GUIStyle.none, catagoryStyle);
        CellSize   = EditorGUILayout.Slider("Cell Size", CellSize, 0f, 1f);
        CellHeight = EditorGUILayout.Slider("Cell Height", CellHeight, 0f, 1f);

        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Agent", GUIStyle.none, catagoryStyle);
        AgentHeight   = EditorGUILayout.Slider("Height", AgentHeight, 0f, 5f);
        AgentRadius   = EditorGUILayout.Slider("Radius", AgentRadius, 0f, 5f);
        AgentMaxClimb = EditorGUILayout.Slider("Max Climb", AgentMaxClimb, 0f, 5f);
        AgentMaxSlope = EditorGUILayout.IntSlider("Max Slope", AgentMaxSlope, 0, 90);

        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Region", GUIStyle.none, catagoryStyle);
        RegionMinSize   = EditorGUILayout.IntSlider("Min Region Size", RegionMinSize, 0, 150);
        RegionMergeSize = EditorGUILayout.IntSlider("Merged Region Size", RegionMergeSize, 0, 150);

        //EditorGUILayout.BeginHorizontal();
        //MonotonePartitioning = EditorGUILayout.Toggle(MonotonePartitioning, GUILayout.Width(20));
        //EditorGUILayout.LabelField(new GUIContent("Monotone Partitioning", "Not yet implemented"), catagoryStyle);
        //EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Polygonization", GUIStyle.none, catagoryStyle);
        EdgeMaxLen   = EditorGUILayout.IntSlider("Max Edge Length", EdgeMaxLen, 0, 50);
        EdgeMaxError = EditorGUILayout.Slider("Max Edge Error", EdgeMaxError, 0.1f, 3f);
        VertsPerPoly = EditorGUILayout.IntSlider("Verts Per Poly", VertsPerPoly, 3, 12);

        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Detail Mesh", GUIStyle.none, catagoryStyle);
        DetailSampleDist     = EditorGUILayout.IntSlider("Sample Distance", DetailSampleDist, 0, 50);
        DetailSampleMaxError = EditorGUILayout.IntSlider("Max Sample Error", DetailSampleMaxError, 0, 16);

        //EditorGUILayout.Space();
        //EditorGUILayout.BeginHorizontal();
        //KeepItermediate = EditorGUILayout.Toggle(KeepItermediate, GUILayout.Width(20));
        //EditorGUILayout.LabelField("Keep Intermediate Results", catagoryStyle);
        //EditorGUILayout.EndHorizontal();

        EditorGUILayout.Separator();

        EditorGUILayout.PrefixLabel("Tiling", GUIStyle.none, catagoryStyle);
        TileSize = EditorGUILayout.IntSlider("TileSize", TileSize, 16, 1024);
        if (TileSize != OldTileSize && _geom != null)
        {
            BuildConfig();
            OldTileSize = TileSize;
        }
        EditorGUILayout.LabelField(
            string.Format("Tiles: {0} x {1}", TileWidth, TileHeight),
            string.Format("Max Tiles: {0} Max Polys: {1}", MaxTiles, MaxPolysPerTile));

        EditorGUILayout.Space();
        EditorGUILayout.LabelField(string.Format("Build Time: {0}ms", BuildTime));
        if (GUILayout.Button("Build"))
        {
            // create nav mesh object and build nav data (build all tiles)
            GameObject go = new GameObject("RecastNavMesh");
            go.AddComponent <RecastNavMesh>();
            RecastNavMesh navMesh = go.GetComponent <RecastNavMesh>();
            BuildTime = navMesh.BuildAllTiles(_config, _geom, TileWidth, TileHeight, MaxPolysPerTile, MaxTiles);
        }
        if (GUILayout.Button("Reset"))
        {
            ResetDefaults();
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();
    }
    /// <summary>
    /// Sets up the GUI so users can export the NavMesh data into XML or Binary formats
    /// </summary>
    public override void OnInspectorGUI()
    {
        var catagoryStyle = new GUIStyle();

        catagoryStyle.fontStyle = FontStyle.Bold;
        RecastNavMesh recastNavMesh = target as RecastNavMesh;

        if (recastNavMesh == null || recastNavMesh.NavMesh == null)
        {
            return;
        }
        _scrollPosition = EditorGUILayout.BeginScrollView(_scrollPosition, false, false); //Last two bools are if we want to show the scroll bar always.

        EditorGUILayout.BeginVertical();
        if (fold == null)
        {
            fold = new bool[recastNavMesh.NavMesh._tiles.Length];
        }
        if (fold.Length != recastNavMesh.NavMesh._tiles.Length)
        {
            var temp = new bool[recastNavMesh.NavMesh._tiles.Length];
            Array.Copy(fold, 0, temp, 0, fold.Length);
            fold = temp;
        }
        for (int i = 0; i < recastNavMesh.NavMesh._tiles.Length; i++)
        {
            if (recastNavMesh.NavMesh._tiles[i] != null && recastNavMesh.NavMesh._tiles[i].Verts != null && recastNavMesh.NavMesh._tiles[i].Verts.Length != 0)
            {
                fold[i] = EditorGUILayout.Foldout(fold[i], new GUIContent(string.Format("{0}, {1}", recastNavMesh.NavMesh._tiles[i].Header.X, recastNavMesh.NavMesh._tiles[i].Header.Y)));
                if (fold[i])
                {
                    if (GUILayout.Button("Remove Tile"))
                    {
                        recastNavMesh.RemoveTile(recastNavMesh.NavMesh._tiles[i].Header.X, recastNavMesh.NavMesh._tiles[i].Header.Y);
                        recastNavMesh.BuildGeometry();
                    }
                    if (GUILayout.Button("Rebuild Tile"))
                    {
                        recastNavMesh.BuildTile(recastNavMesh.NavMesh._tiles[i].Header.X, recastNavMesh.NavMesh._tiles[i].Header.Y);
                        recastNavMesh.BuildGeometry();
                    }
                }
            }
        }
        EditorGUILayout.Separator();
        EditorGUILayout.PrefixLabel("Rebuild", GUIStyle.none, catagoryStyle);

        if (GUILayout.Button("Rebuild Geometry"))
        {
            recastNavMesh.RebuildTiles();
        }
        EditorGUILayout.Separator();
        EditorGUILayout.PrefixLabel("Visualize", GUIStyle.none, catagoryStyle);

        recastNavMesh.Mat = EditorGUILayout.ObjectField("Material", recastNavMesh.Mat, typeof(Material), true) as Material;
        if (GUILayout.Button("Toggle Geometry"))
        {
            recastNavMesh.Toggle();
        }

        //if (GUILayout.Button("Draw Voxel Geometry"))
        //{
        //    recastNavMesh.BuildVoxelGeometry();
        //    recastNavMesh.DrawVoxelGeometry();
        //}
        //if (GUILayout.Button("Create Walkable Voxel Geometry"))
        //{
        //    recastNavMesh.BuildWalkableVoxelGeometry();
        //    recastNavMesh.DrawWalkableVoxelGeometry();
        //}

        //if (GUILayout.Button("Create PolyMesh"))
        //{
        //    recastNavMesh.BuildPolyMeshGeometry();
        //    recastNavMesh.DrawPolyMeshGeometry();
        //}

        EditorGUILayout.Separator();
        EditorGUILayout.PrefixLabel("Output", GUIStyle.none, catagoryStyle);
        if (GUILayout.Button("Export XML"))
        {
            var path = EditorUtility.SaveFilePanel("Export NavMesh", "", "NavMesh.xml", "xml");

            if (path.Length > 0)
            {
                FileStream f = null;
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                f = File.Create(path);
                NavMeshSerializer serializer = new NavMeshSerializer(recastNavMesh.NavMesh);

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(NavMeshSerializer));
                xmlSerializer.Serialize(f, serializer);

                f.Close();
            }
        }
        if (GUILayout.Button("Export JSON"))
        {
            var path = EditorUtility.SaveFilePanel("Export NavMesh", "", "NavMesh.json", "json");

            if (path.Length > 0)
            {
                FileStream f = null;
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                f = File.Create(path);
                NavMeshSerializer serializer = new NavMeshSerializer(recastNavMesh.NavMesh);

                using (StreamWriter sw = new StreamWriter(f))
                    using (JsonWriter jw = new JsonTextWriter(sw))
                    {
                        jw.Formatting = (Recast.Json.Formatting)Formatting.Indented;

                        JsonSerializer b = new JsonSerializer();
                        b.Serialize(jw, serializer);
                    }
                f.Close();
            }
        }
        if (GUILayout.Button("Export Binary JSON"))
        {
            var path = EditorUtility.SaveFilePanel("Export NavMesh", "", "NavMesh.dat", "dat");

            if (path.Length > 0)
            {
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                NavMeshSerializer serializer = new NavMeshSerializer(recastNavMesh.NavMesh);

                using (FileStream f = File.Create(path))
                    using (BsonWriter writer = new BsonWriter(f))
                    {
                        JsonSerializer b = new JsonSerializer();
                        b.Serialize(writer, serializer);
                    }
            }
        }

        if (GUILayout.Button("Export for WebPlayer"))
        {
            var path = EditorUtility.SaveFilePanel("Export NavMesh", "", "NavMesh.bytes", "bytes");

            if (path.Length > 0)
            {
                FileStream f = null;
                if (File.Exists(path))
                {
                    File.Delete(path);
                }

                f = File.Create(path);
                NavMeshSerializer serializer = new NavMeshSerializer(recastNavMesh.NavMesh);

                XmlSerializer xmlSerializer = new XmlSerializer(typeof(NavMeshSerializer));
                var           xmlWriter     = new XmlTextWriter(f, Encoding.UTF8);
                xmlSerializer.Serialize(xmlWriter, serializer);

                f.Close();
            }
        }
        EditorGUILayout.EndVertical();
        EditorGUILayout.EndScrollView();
    }