Skip to content

If a NavMeshAsset exists; replace it rather than delete+create new #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: 2017.1
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 28 additions & 9 deletions Assets/NavMeshComponents/Editor/NavMeshSurfaceEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
using UnityEditor.IMGUI.Controls;
using UnityEditor.SceneManagement;
using UnityEditorInternal;
using UnityEngine.AI;
using UnityEngine;
using UnityEngine.AI;

namespace UnityEditor.AI
{
Expand All @@ -25,6 +25,7 @@ class NavMeshSurfaceEditor : Editor
SerializedProperty m_TileSize;
SerializedProperty m_UseGeometry;
SerializedProperty m_VoxelSize;
SerializedProperty m_NavMeshData;

class Styles
{
Expand Down Expand Up @@ -77,6 +78,7 @@ void OnEnable()
m_TileSize = serializedObject.FindProperty("m_TileSize");
m_UseGeometry = serializedObject.FindProperty("m_UseGeometry");
m_VoxelSize = serializedObject.FindProperty("m_VoxelSize");
m_NavMeshData = serializedObject.FindProperty("m_NavMeshData");

NavMeshVisualizationSettings.showNavigation++;
}
Expand Down Expand Up @@ -108,7 +110,7 @@ static void CreateNavMeshAsset(NavMeshSurface surface)
AssetDatabase.CreateAsset(surface.navMeshData, combinedAssetPath);
}

static NavMeshData GetNavMeshAssetToDelete(NavMeshSurface navSurface)
static NavMeshData GetExistingNavMeshAsset(NavMeshSurface navSurface)
{
var prefabType = PrefabUtility.GetPrefabType(navSurface);
if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
Expand All @@ -123,7 +125,7 @@ static NavMeshData GetNavMeshAssetToDelete(NavMeshSurface navSurface)

void ClearSurface(NavMeshSurface navSurface)
{
var assetToDelete = GetNavMeshAssetToDelete(navSurface);
var assetToDelete = GetExistingNavMeshAsset(navSurface);
navSurface.RemoveData();
navSurface.navMeshData = null;
EditorUtility.SetDirty(navSurface);
Expand All @@ -142,6 +144,11 @@ public override void OnInspectorGUI()

serializedObject.Update();

using (new EditorGUI.DisabledScope(true))
{
EditorGUILayout.ObjectField(m_NavMeshData);
}

var bs = NavMesh.GetSettingsByID(m_AgentTypeID.intValue);

if (bs.agentTypeID != -1)
Expand Down Expand Up @@ -236,6 +243,7 @@ public override void OnInspectorGUI()
}

EditorGUILayout.Space();

EditorGUI.indentLevel--;
}

Expand Down Expand Up @@ -351,6 +359,11 @@ static NavMeshData InitializeBakeData(NavMeshSurface surface)
, surface.transform.position, surface.transform.rotation);
}

static void ReplaceData(NavMeshData to, NavMeshData from)
{
EditorUtility.CopySerializedIfDifferent(from, to);
}

static void UpdateAsyncBuildOperations()
{
foreach (var oper in s_BakeOperations)
Expand All @@ -361,15 +374,21 @@ static void UpdateAsyncBuildOperations()
if (oper.bakeOperation.isDone)
{
var surface = oper.surface;
var delete = GetNavMeshAssetToDelete(surface);
if (delete != null)
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(delete));

surface.RemoveData();
surface.navMeshData = oper.bakeData;

var existing = GetExistingNavMeshAsset(surface);
if (existing != null)
{
ReplaceData(existing, oper.bakeData);
}
else
{
surface.navMeshData = oper.bakeData;
CreateNavMeshAsset(surface);
}
if (surface.isActiveAndEnabled)
surface.AddData();
CreateNavMeshAsset(surface);

EditorSceneManager.MarkSceneDirty(surface.gameObject.scene);
}
}
Expand Down