using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UnityEditor;
using UnityEngine;
using UnityEngine.AI;
using UnityEngine.SceneManagement;

public class NavMeshExport
{
#if UNITY_EDITOR
    [MenuItem("LCFrameWork/NavMeshExport")]
    static void Export()
    {
        Debug.Log("NavMesh Export Start");

        NavMeshTriangulation navMeshTriangulation = NavMesh.CalculateTriangulation();

        //文件路径 路径文件夹不存在会报错
        string path = Application.dataPath + "/" + SceneManager.GetActiveScene().name + ".obj";
        

        //新建文件
        StreamWriter streamWriter = new StreamWriter(path);
        //GameObject speresGO = new GameObject("speres");

        //顶点
        for (int i = 0; i < navMeshTriangulation.vertices.Length; i++)
        {
            //streamWriter.WriteLine("v  " + navMeshTriangulation.vertices[i].x + " " + navMeshTriangulation.vertices[i].y + " " + navMeshTriangulation.vertices[i].z);
            streamWriter.WriteLine("v  " + (int)navMeshTriangulation.vertices[i].x + " " + 0 + " " + (int)navMeshTriangulation.vertices[i].z);


            //GameObject sphere = GameObject.CreatePrimitive(PrimitiveType.Sphere);
            //sphere.GetComponent<Renderer>().material.color = Color.red;
            //sphere.transform.localScale = Vector3.one * 0.5f;
            //sphere.transform.parent = speresGO.transform;
            //sphere.transform.position = navMeshTriangulation.vertices[i];

        }

        streamWriter.WriteLine("g pPlane1");

        //索引
        for (int i = 0; i < navMeshTriangulation.indices.Length;)
        {
            streamWriter.WriteLine("f " + (navMeshTriangulation.indices[i] + 1) + " " + (navMeshTriangulation.indices[i + 1] + 1) + " " + (navMeshTriangulation.indices[i + 2] + 1));
            i = i + 3;
        }

        streamWriter.Flush();
        streamWriter.Close();

        AssetDatabase.Refresh();

        Debug.Log("NavMesh Export Success");
    }
#endif
}