├── ProjectSettings
├── ProjectVersion.txt
├── AudioManager.asset
├── InputManager.asset
├── NavMeshAreas.asset
├── TagManager.asset
├── TimeManager.asset
├── EditorSettings.asset
├── NetworkManager.asset
├── DynamicsManager.asset
├── GraphicsSettings.asset
├── Physics2DSettings.asset
├── ProjectSettings.asset
├── QualitySettings.asset
├── UnityAdsSettings.asset
├── ClusterInputManager.asset
├── EditorBuildSettings.asset
└── UnityConnectSettings.asset
├── Assets
├── ball.prefab
├── Scene
│ ├── demo.unity
│ ├── demo
│ │ ├── NavMesh.asset
│ │ └── NavMesh.asset.meta
│ ├── demo.unity.meta
│ ├── demo.meta
│ ├── demo.cs.meta
│ ├── NavMeshExport.cs.meta
│ ├── demo.cs
│ └── NavMeshExport.cs
├── navmesh
│ ├── Materials
│ │ ├── _NavMeshMat.mat
│ │ └── _NavMeshMat.mat.meta
│ ├── navmesh_demo.lua.meta
│ ├── Materials.meta
│ ├── navmesh_demo.lua
│ ├── navmesh_demo.obj
│ └── navmesh_demo.obj.meta
├── ball.prefab.meta
├── Scene.meta
└── navmesh.meta
├── picture
├── photo1.png
└── photo2.png
├── README.md
├── .gitignore
└── LICENSE
/ProjectSettings/ProjectVersion.txt:
--------------------------------------------------------------------------------
1 | m_EditorVersion: 5.6.0f3
2 |
--------------------------------------------------------------------------------
/Assets/ball.prefab:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/Assets/ball.prefab
--------------------------------------------------------------------------------
/picture/photo1.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/picture/photo1.png
--------------------------------------------------------------------------------
/picture/photo2.png:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/picture/photo2.png
--------------------------------------------------------------------------------
/Assets/Scene/demo.unity:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/Assets/Scene/demo.unity
--------------------------------------------------------------------------------
/Assets/Scene/demo/NavMesh.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/Assets/Scene/demo/NavMesh.asset
--------------------------------------------------------------------------------
/ProjectSettings/AudioManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/AudioManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/InputManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/InputManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/NavMeshAreas.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/NavMeshAreas.asset
--------------------------------------------------------------------------------
/ProjectSettings/TagManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/TagManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/TimeManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/TimeManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/EditorSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/EditorSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/NetworkManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/NetworkManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/DynamicsManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/DynamicsManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/GraphicsSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/GraphicsSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/Physics2DSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/Physics2DSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/ProjectSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/ProjectSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/QualitySettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/QualitySettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/UnityAdsSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/UnityAdsSettings.asset
--------------------------------------------------------------------------------
/Assets/navmesh/Materials/_NavMeshMat.mat:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/Assets/navmesh/Materials/_NavMeshMat.mat
--------------------------------------------------------------------------------
/ProjectSettings/ClusterInputManager.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/ClusterInputManager.asset
--------------------------------------------------------------------------------
/ProjectSettings/EditorBuildSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/EditorBuildSettings.asset
--------------------------------------------------------------------------------
/ProjectSettings/UnityConnectSettings.asset:
--------------------------------------------------------------------------------
https://raw.githubusercontent.com/LingJiJian/NavmeshExport/HEAD/ProjectSettings/UnityConnectSettings.asset
--------------------------------------------------------------------------------
/Assets/Scene/demo.unity.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: b2c03a240c915684f98fefb7021df6a4
3 | timeCreated: 1464234566
4 | licenseType: Free
5 | DefaultImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Assets/ball.prefab.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: aa96db89aa5cad540b231b0a6be7cc53
3 | timeCreated: 1475370743
4 | licenseType: Free
5 | NativeFormatImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Assets/navmesh/navmesh_demo.lua.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: baba8cb7860323e4fa564756311fc02f
3 | timeCreated: 1475376512
4 | licenseType: Free
5 | DefaultImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Assets/Scene.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 18350bbb5f8756b4b884f745c1bdd49d
3 | folderAsset: yes
4 | timeCreated: 1464234566
5 | licenseType: Free
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/navmesh.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 144342742a97d584a8aced179b6ba6bb
3 | folderAsset: yes
4 | timeCreated: 1475374157
5 | licenseType: Free
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/navmesh/Materials/_NavMeshMat.mat.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 86c19d418f484dd4a9c9c3a9573bee5f
3 | timeCreated: 1475375188
4 | licenseType: Free
5 | NativeFormatImporter:
6 | userData:
7 | assetBundleName:
8 | assetBundleVariant:
9 |
--------------------------------------------------------------------------------
/Assets/Scene/demo.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 99d3a56b1caf0f946a04e765eee3c0e6
3 | folderAsset: yes
4 | timeCreated: 1464238661
5 | licenseType: Free
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/navmesh/Materials.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: c21c98da966605b4b8f30d26097982a1
3 | folderAsset: yes
4 | timeCreated: 1475375188
5 | licenseType: Free
6 | DefaultImporter:
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/Scene/demo/NavMesh.asset.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 6182f8309375f264e9c094746878428d
3 | timeCreated: 1500552352
4 | licenseType: Free
5 | NativeFormatImporter:
6 | mainObjectFileID: 23800000
7 | userData:
8 | assetBundleName:
9 | assetBundleVariant:
10 |
--------------------------------------------------------------------------------
/Assets/Scene/demo.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: 37b79eb08aca06b43a94c53660ba8f2b
3 | timeCreated: 1470534199
4 | licenseType: Free
5 | MonoImporter:
6 | serializedVersion: 2
7 | defaultReferences: []
8 | executionOrder: 0
9 | icon: {instanceID: 0}
10 | userData:
11 | assetBundleName:
12 | assetBundleVariant:
13 |
--------------------------------------------------------------------------------
/Assets/Scene/NavMeshExport.cs.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: ad2d5486e667e0747aa8e3e3086c5fcf
3 | timeCreated: 1464239247
4 | licenseType: Free
5 | MonoImporter:
6 | serializedVersion: 2
7 | defaultReferences: []
8 | executionOrder: 0
9 | icon: {instanceID: 0}
10 | userData:
11 | assetBundleName:
12 | assetBundleVariant:
13 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | Navmesh数据导出工具
2 | ====
3 | 
4 | 
5 | -------
6 | ### 说明
7 | unity自带的navmesh由于算法没公开,服务端要判断该移动点是否合法比较困难,很多时候需要自己实现一套A*费时费力,
8 | 本工具就是导出navmesh的各个三角形坐标数据,然后服务端只需要判断该点是否在三角形内就为之合法。
9 |
10 | ### 如何使用
11 | 1.首先你需要自己在unity里编辑好可行走范围
12 | 2.然后点击菜单Tools/Export NavMesh Data即可
13 |
14 | ### 如何测试
15 | 1.首先把navmesh目录下生成的mesh拖进场景
16 | 2.然后点击菜单Tools/NavMesh Data Test即可 (移动TestPoint即可)
17 |
18 | 联系
19 | -------
20 | qq342854406 qq群347085657
21 |
--------------------------------------------------------------------------------
/.gitignore:
--------------------------------------------------------------------------------
1 | *.sln
2 | *.userprefs
3 | *.pidb
4 | *.svd
5 | *.suo
6 | *.user
7 | *.booproj
8 | Assets/data.zip
9 | Assets/version.ver
10 | Assets/Slua/LuaObject/*
11 | /Library
12 | /Library/FailedAssetImports.txt
13 | /Library/cache/
14 | /Library/previews/
15 | /Library/ScriptAssemblies
16 | /obj
17 | /Temp
18 | .DS_Store
19 | .DS_Store?
20 | ehthumbs.db
21 | Thumbs.db
22 | *.swp
23 | *.Trashes
24 | ExportedObj/
25 | Temp/
26 |
27 | *~
28 |
29 | /Assembly-CSharp.csproj
30 |
31 | /Assembly-CSharp-vs.csproj
32 |
--------------------------------------------------------------------------------
/Assets/Scene/demo.cs:
--------------------------------------------------------------------------------
1 | using UnityEngine;
2 | using System.Collections;
3 | using UnityEngine.AI;
4 |
5 | public class demo : MonoBehaviour {
6 |
7 | private NavMeshAgent agent;
8 | void Start()
9 | {
10 | agent = GetComponent();
11 | }
12 |
13 | void Update()
14 | {
15 | RaycastHit hit;
16 | if (Input.GetMouseButtonDown(0))
17 | {
18 | Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
19 | if (Physics.Raycast(ray, out hit))
20 | agent.SetDestination(hit.point);
21 | }
22 | }
23 |
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/LICENSE:
--------------------------------------------------------------------------------
1 | The MIT License (MIT)
2 |
3 | Copyright (c) 2014 Lingjijian [B-y]
4 |
5 | 342854406@qq.com
6 |
7 | Permission is hereby granted, free of charge, to any person obtaining a copy
8 | of this software and associated documentation files (the "Software"), to deal
9 | in the Software without restriction, including without limitation the rights
10 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 | copies of the Software, and to permit persons to whom the Software is
12 | furnished to do so, subject to the following conditions:
13 |
14 | The above copyright notice and this permission notice shall be included in all
15 | copies or substantial portions of the Software.
16 |
17 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23 | SOFTWARE.
--------------------------------------------------------------------------------
/Assets/navmesh/navmesh_demo.lua:
--------------------------------------------------------------------------------
1 | local nav = {
2 | {{4.333333,0.08333334,-2.5},{3.333333,0.08333334,-2.833333},{3,0.08333334,-2.5}},
3 | {{3.333333,0.08333334,-2.833333},{4.333333,0.08333334,-2.5},{4.333333,0.08333334,-4.333333}},
4 | {{3.333333,0.08333334,-2.833333},{4.333333,0.08333334,-4.333333},{3.333333,0.08333334,-4.333333}},
5 | {{3,0.08333334,-2.5},{4.333333,0.08333334,0},{4.333333,0.08333334,-2.5}},
6 | {{4.333333,0.08333334,0},{3,0.08333334,-2.5},{1.5,0.08333334,-2.5}},
7 | {{4.333333,0.08333334,0},{1.5,0.08333334,-2.5},{0,0.08333334,0}},
8 | {{1.166667,0.08333334,-2.833333},{0,0.08333334,0},{1.5,0.08333334,-2.5}},
9 | {{0,0.08333334,-4.333333},{0,0.08333334,0},{1.166667,0.08333334,-2.833333}},
10 | {{0,0.08333334,-4.333333},{1.166667,0.08333334,-2.833333},{1.166667,0.08333334,-4.333333}},
11 | {{0,0.08333334,0},{0,0.08333334,-4.333333},{-4.333333,0.08333334,-4.333333}},
12 | {{0,0.08333334,0},{-4.333333,0.08333334,-4.333333},{-4.333333,0.08333334,0}},
13 | {{0,0.08333334,4.333333},{4.333333,0.08333334,0},{0,0.08333334,0}},
14 | {{0,0.08333334,4.333333},{0,0.08333334,0},{-4.333333,0.08333334,0}},
15 | {{0,0.08333334,4.333333},{4.333333,0.08333334,4.333333},{4.333333,0.08333334,0}},
16 | {{0,0.08333334,4.333333},{-4.333333,0.08333334,0},{-4.333333,0.08333334,4.333333}},
17 | }
18 | return nav
--------------------------------------------------------------------------------
/Assets/navmesh/navmesh_demo.obj:
--------------------------------------------------------------------------------
1 | g _NavMesh
2 | v -4.333333 0.08333334 -2.5
3 | v -3 0.08333334 -2.5
4 | v -3.333333 0.08333334 -2.833333
5 | v -3.333333 0.08333334 -2.833333
6 | v -3.333333 0.08333334 -4.333333
7 | v -4.333333 0.08333334 -4.333333
8 | v -4.333333 0.08333334 -2.5
9 | v -4.333333 0.08333334 0
10 | v 0 0.08333334 0
11 | v -1.5 0.08333334 -2.5
12 | v -3 0.08333334 -2.5
13 | v -3 0.08333334 -2.5
14 | v -4.333333 0.08333334 -2.5
15 | v -4.333333 0.08333334 0
16 | v 0 0.08333334 -4.333333
17 | v -1.166667 0.08333334 -4.333333
18 | v -1.166667 0.08333334 -2.833333
19 | v 0 0.08333334 0
20 | v -1.166667 0.08333334 -2.833333
21 | v -1.5 0.08333334 -2.5
22 | v 0 0.08333334 0
23 | v 0 0.08333334 0
24 | v 4.333333 0.08333334 0
25 | v 4.333333 0.08333334 -4.333333
26 | v 0 0.08333334 -4.333333
27 | v 0 0.08333334 4.333333
28 | v 0 0.08333334 0
29 | v -4.333333 0.08333334 0
30 | v -4.333333 0.08333334 4.333333
31 | v 0 0.08333334 4.333333
32 | v 4.333333 0.08333334 4.333333
33 | v 4.333333 0.08333334 0
34 | v 0 0.08333334 0
35 |
36 |
37 |
38 | f 1/1/1 2/2/2 3/3/3
39 | f 4/4/4 5/5/5 6/6/6
40 | f 4/4/4 6/6/6 7/7/7
41 | f 8/8/8 9/9/9 10/10/10
42 | f 8/8/8 10/10/10 11/11/11
43 | f 12/12/12 13/13/13 14/14/14
44 | f 15/15/15 16/16/16 17/17/17
45 | f 15/15/15 17/17/17 18/18/18
46 | f 19/19/19 20/20/20 21/21/21
47 | f 22/22/22 23/23/23 24/24/24
48 | f 22/22/22 24/24/24 25/25/25
49 | f 26/26/26 27/27/27 28/28/28
50 | f 26/26/26 28/28/28 29/29/29
51 | f 30/30/30 31/31/31 32/32/32
52 | f 30/30/30 32/32/32 33/33/33
53 |
--------------------------------------------------------------------------------
/Assets/navmesh/navmesh_demo.obj.meta:
--------------------------------------------------------------------------------
1 | fileFormatVersion: 2
2 | guid: ce32cfaca95d40541944d9c0a877cd89
3 | timeCreated: 1500552304
4 | licenseType: Free
5 | ModelImporter:
6 | serializedVersion: 19
7 | fileIDToRecycleName:
8 | 100000: _NavMesh
9 | 100002: //RootNode
10 | 400000: _NavMesh
11 | 400002: //RootNode
12 | 2300000: _NavMesh
13 | 3300000: _NavMesh
14 | 4300000: _NavMesh
15 | materials:
16 | importMaterials: 1
17 | materialName: 0
18 | materialSearch: 1
19 | animations:
20 | legacyGenerateAnimations: 4
21 | bakeSimulation: 0
22 | resampleCurves: 1
23 | optimizeGameObjects: 0
24 | motionNodeName:
25 | rigImportErrors:
26 | rigImportWarnings:
27 | animationImportErrors:
28 | animationImportWarnings:
29 | animationRetargetingWarnings:
30 | animationDoRetargetingWarnings: 0
31 | animationCompression: 1
32 | animationRotationError: 0.5
33 | animationPositionError: 0.5
34 | animationScaleError: 0.5
35 | animationWrapMode: 0
36 | extraExposedTransformPaths: []
37 | clipAnimations: []
38 | isReadable: 1
39 | meshes:
40 | lODScreenPercentages: []
41 | globalScale: 1
42 | meshCompression: 0
43 | addColliders: 0
44 | importBlendShapes: 1
45 | swapUVChannels: 0
46 | generateSecondaryUV: 0
47 | useFileUnits: 1
48 | optimizeMeshForGPU: 1
49 | keepQuads: 0
50 | weldVertices: 1
51 | secondaryUVAngleDistortion: 8
52 | secondaryUVAreaDistortion: 15.000001
53 | secondaryUVHardAngle: 88
54 | secondaryUVPackMargin: 4
55 | useFileScale: 1
56 | tangentSpace:
57 | normalSmoothAngle: 60
58 | normalImportMode: 0
59 | tangentImportMode: 3
60 | importAnimation: 1
61 | copyAvatar: 0
62 | humanDescription:
63 | serializedVersion: 2
64 | human: []
65 | skeleton: []
66 | armTwist: 0.5
67 | foreArmTwist: 0.5
68 | upperLegTwist: 0.5
69 | legTwist: 0.5
70 | armStretch: 0.05
71 | legStretch: 0.05
72 | feetSpacing: 0
73 | rootMotionBoneName:
74 | rootMotionBoneRotation: {x: 0, y: 0, z: 0, w: 1}
75 | hasTranslationDoF: 0
76 | hasExtraRoot: 0
77 | skeletonHasParents: 1
78 | lastHumanDescriptionAvatarSource: {instanceID: 0}
79 | animationType: 0
80 | humanoidOversampling: 1
81 | additionalBone: 0
82 | userData:
83 | assetBundleName:
84 | assetBundleVariant:
85 |
--------------------------------------------------------------------------------
/Assets/Scene/NavMeshExport.cs:
--------------------------------------------------------------------------------
1 | using System.IO;
2 | using System.Text;
3 | using UnityEditor;
4 | using UnityEngine.SceneManagement;
5 | using UnityEngine;
6 | using UnityEngine.AI;
7 |
8 | //navmesh导出数据
9 | public class NavMeshExport : MonoBehaviour
10 | {
11 | private GameObject _testMap;
12 |
13 | [MenuItem("Tools/Export NavMesh Data")]
14 | private static void Export()
15 | {
16 | NavMeshTriangulation triangulatedNavMesh = NavMesh.CalculateTriangulation();
17 |
18 | Mesh mesh = new Mesh();
19 | mesh.name = "_NavMesh";
20 | mesh.vertices = triangulatedNavMesh.vertices;
21 | mesh.triangles = triangulatedNavMesh.indices;
22 |
23 | string baseName = "navmesh_" + SceneManager.GetActiveScene().name;
24 | string fileName = Application.dataPath + "/navmesh/" + baseName + ".obj";
25 | ExportNavmesh(mesh, fileName);
26 |
27 | AssetDatabase.Refresh();
28 | string assetName = fileName.Replace(Application.dataPath, "Assets");
29 | GameObject navMesh = Instantiate(AssetDatabase.LoadAssetAtPath(assetName));
30 | navMesh.name = baseName;
31 | ExportNavData(navMesh);
32 | Debug.Log("导出完成:" + baseName);
33 | AssetDatabase.Refresh();
34 | }
35 |
36 | [MenuItem("Tools/NavMesh Data Test")]
37 | private static void Test()
38 | {
39 | GameObject obj = GameObject.Find("_NavMesh");
40 | Vector3[] localVectors = obj.GetComponent().sharedMesh.vertices;
41 | int[] triangles = obj.GetComponent().sharedMesh.triangles;
42 |
43 | //把mesh的本地坐标转成世界坐标
44 | Vector3[] worldVectors = new Vector3[localVectors.Length];
45 | for (int i = 0; i < localVectors.Length; ++i)
46 | {
47 | Vector3 pos = obj.transform.TransformPoint(localVectors[i]);
48 | worldVectors[i] = pos;
49 | }
50 |
51 | //检测点
52 | Vector3 checkPoint = GameObject.Find("TestPoint").transform.position;
53 | bool _isInside = false;
54 |
55 | for (int i = 0; i < triangles.Length; i += 3)
56 | {
57 | //Debug.Log(string.Format("{0},{1},{2}", triangles[i], triangles[i + 1], triangles[i + 2]));
58 |
59 | if (IsInside(worldVectors[triangles[i]], worldVectors[triangles[i + 1]], worldVectors[triangles[i + 2]], checkPoint))
60 | {
61 | _isInside = true;
62 | break;
63 | }
64 | }
65 |
66 | if (_isInside)
67 | Debug.Log("该点合法");
68 | else
69 | Debug.Log("该点非法");
70 | }
71 |
72 | private static string ParseMesh(Mesh mesh)
73 | {
74 | StringBuilder sb = new StringBuilder();
75 |
76 | sb.Append("g ").Append(mesh.name).Append("\n");
77 | foreach (Vector3 v in mesh.vertices)
78 | {
79 | sb.Append(string.Format("v {0} {1} {2}\n", v.x, v.y, v.z));
80 | }
81 | sb.Append("\n");
82 | foreach (Vector3 v in mesh.normals)
83 | {
84 | sb.Append(string.Format("vn {0} {1} {2}\n", v.x, v.y, v.z));
85 | }
86 | sb.Append("\n");
87 | foreach (Vector3 v in mesh.uv)
88 | {
89 | sb.Append(string.Format("vt {0} {1}\n", v.x, v.y));
90 | }
91 | for (int m = 0; m < mesh.subMeshCount; m++)
92 | {
93 | sb.Append("\n");
94 |
95 | int[] triangles = mesh.GetTriangles(m);
96 | for (int i = 0; i < triangles.Length; i += 3)
97 | {
98 | sb.Append(string.Format("f {0}/{0}/{0} {1}/{1}/{1} {2}/{2}/{2}\n", triangles[i] + 1, triangles[i + 1] + 1, triangles[i + 2] + 1));
99 | }
100 | }
101 | return sb.ToString();
102 | }
103 |
104 | private static void ExportNavmesh(Mesh mesh, string filename)
105 | {
106 | using (StreamWriter sw = new StreamWriter(filename))
107 | {
108 | sw.Write(ParseMesh(mesh));
109 | }
110 | }
111 |
112 | private static void ExportNavData(GameObject obj)
113 | {
114 | Vector3[] localVectors = obj.transform.Find("_NavMesh").GetComponent().sharedMesh.vertices;
115 | int[] triangles = obj.transform.Find("_NavMesh").GetComponent().sharedMesh.triangles;
116 | //把mesh的本地坐标转成世界坐标
117 | Vector3[] worldVectors = new Vector3[localVectors.Length];
118 | for (int i = 0; i < localVectors.Length; i++)
119 | {
120 | Vector3 pos = obj.transform.TransformPoint(localVectors[i]);
121 | worldVectors[i] = pos;
122 | }
123 | StringBuilder sb = new StringBuilder();
124 | sb.Append("local nav = {\n");
125 | for (int i = 0; i < triangles.Length; i += 3)
126 | {
127 | sb.AppendFormat("\t{{{0},{1},{2}}},\n", _VectorToLua(worldVectors[triangles[i]]), _VectorToLua(worldVectors[triangles[i + 1]]), _VectorToLua(worldVectors[triangles[i + 2]]));
128 | }
129 | sb.Append("}\n");
130 | sb.Append("return nav");
131 | using (StreamWriter sw = new StreamWriter(Application.dataPath + "/navmesh/" + obj.name + ".lua"))
132 | {
133 | sw.Write(sb.ToString());
134 | }
135 | DestroyImmediate(obj);
136 | }
137 |
138 | private static string _VectorToLua(Vector3 vec)
139 | {
140 | return string.Format("{{{0},{1},{2}}}", vec.x, vec.y, vec.z);
141 | }
142 |
143 | //判断点是否在三角形内
144 | public static bool IsInside(Vector3 A, Vector3 B, Vector3 C, Vector3 P)
145 | {
146 | Vector3 v0 = C - A;
147 | Vector3 v1 = B - A;
148 | Vector3 v2 = P - A;
149 |
150 | float dot00 = Vector3.Dot(v0, v0);
151 | float dot01 = Vector3.Dot(v0, v1);
152 | float dot02 = Vector3.Dot(v0, v2);
153 | float dot11 = Vector3.Dot(v1, v1);
154 | float dot12 = Vector3.Dot(v1, v2);
155 |
156 | float inverDeno = 1 / (dot00 * dot11 - dot01 * dot01);
157 |
158 | float u = (dot11 * dot02 - dot01 * dot12) * inverDeno;
159 | if (u < 0 || u > 1) // if u out of range, return directly
160 | {
161 | return false;
162 | }
163 |
164 | float v = (dot00 * dot12 - dot01 * dot02) * inverDeno;
165 | if (v < 0 || v > 1) // if v out of range, return directly
166 | {
167 | return false;
168 | }
169 |
170 | return u + v <= 1;
171 |
172 | }
173 | }
--------------------------------------------------------------------------------