Skip to content

Commit

Permalink
Merged the commit 'Initial commit for switch support.' from the conso…
Browse files Browse the repository at this point in the history
…les branch
  • Loading branch information
UnityGuillaume committed Oct 2, 2019
1 parent 7486066 commit 8d17a3d
Show file tree
Hide file tree
Showing 10 changed files with 255 additions and 117 deletions.
1 change: 1 addition & 0 deletions Assets/3DGamekit/Art/Shaders/BubbleBurst.shader
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
float4 texcoord1 : TEXCOORD1;
float4 texcoord2 : TEXCOORD2;
float3 normal : NORMAL;
float4 tangent : TANGENT;
UNITY_VERTEX_INPUT_INSTANCE_ID
};

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#define NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF

using System.Collections.Generic;
using System.IO;
using System.Linq;
using UnityEditor.Experimental.SceneManagement;
using UnityEditor.IMGUI.Controls;
using UnityEditor.SceneManagement;
using UnityEditorInternal;
Expand All @@ -29,9 +26,6 @@ class NavMeshSurfaceEditor : Editor
SerializedProperty m_UseGeometry;
SerializedProperty m_VoxelSize;

#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
SerializedProperty m_NavMeshData;
#endif
class Styles
{
public readonly GUIContent m_LayerMask = new GUIContent("Include Layers");
Expand All @@ -45,6 +39,15 @@ class Styles
public readonly GUIContent m_ShowPolyMeshDetail = new GUIContent("Show Poly Mesh Detail");
}

struct AsyncBakeOperation
{
public NavMeshSurface surface;
public NavMeshData bakeData;
public AsyncOperation bakeOperation;
}

static List<AsyncBakeOperation> s_BakeOperations = new List<AsyncBakeOperation>();

static Styles s_Styles;

static bool s_ShowDebugOptions;
Expand Down Expand Up @@ -75,9 +78,6 @@ void OnEnable()
m_UseGeometry = serializedObject.FindProperty("m_UseGeometry");
m_VoxelSize = serializedObject.FindProperty("m_VoxelSize");

#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
m_NavMeshData = serializedObject.FindProperty("m_NavMeshData");
#endif
NavMeshVisualizationSettings.showNavigation++;
}

Expand All @@ -86,6 +86,55 @@ void OnDisable()
NavMeshVisualizationSettings.showNavigation--;
}

static string GetAndEnsureTargetPath(NavMeshSurface surface)
{
// Create directory for the asset if it does not exist yet.
var activeScenePath = surface.gameObject.scene.path;

var targetPath = "Assets";
if (!string.IsNullOrEmpty(activeScenePath))
targetPath = Path.Combine(Path.GetDirectoryName(activeScenePath), Path.GetFileNameWithoutExtension(activeScenePath));
if (!Directory.Exists(targetPath))
Directory.CreateDirectory(targetPath);
return targetPath;
}

static void CreateNavMeshAsset(NavMeshSurface surface)
{
var targetPath = GetAndEnsureTargetPath(surface);

var combinedAssetPath = Path.Combine(targetPath, "NavMesh-" + surface.name + ".asset");
combinedAssetPath = AssetDatabase.GenerateUniqueAssetPath(combinedAssetPath);
AssetDatabase.CreateAsset(surface.navMeshData, combinedAssetPath);
}

static NavMeshData GetNavMeshAssetToDelete(NavMeshSurface navSurface)
{
var prefabType = PrefabUtility.GetPrefabType(navSurface);
if (prefabType == PrefabType.PrefabInstance || prefabType == PrefabType.DisconnectedPrefabInstance)
{
// Don't allow deleting the asset belonging to the prefab parent
var parentSurface = PrefabUtility.GetCorrespondingObjectFromSource(navSurface) as NavMeshSurface;
if (parentSurface && navSurface.navMeshData == parentSurface.navMeshData)
return null;
}
return navSurface.navMeshData;
}

void ClearSurface(NavMeshSurface navSurface)
{
var assetToDelete = GetNavMeshAssetToDelete(navSurface);
navSurface.RemoveData();
navSurface.navMeshData = null;
EditorUtility.SetDirty(navSurface);

if (assetToDelete)
{
AssetDatabase.DeleteAsset(AssetDatabase.GetAssetPath(assetToDelete));
EditorSceneManager.MarkSceneDirty(navSurface.gameObject.scene);
}
}

Bounds GetBounds()
{
var navSurface = (NavMeshSurface)target;
Expand Down Expand Up @@ -135,6 +184,8 @@ public override void OnInspectorGUI()

EditorGUILayout.Space();

EditorGUILayout.Space();

m_OverrideVoxelSize.isExpanded = EditorGUILayout.Foldout(m_OverrideVoxelSize.isExpanded, "Advanced");
if (m_OverrideVoxelSize.isExpanded)
{
Expand Down Expand Up @@ -182,7 +233,7 @@ public override void OnInspectorGUI()
if (!m_OverrideTileSize.hasMultipleDifferentValues)
{
if (m_OverrideTileSize.boolValue)
EditorGUILayout.HelpBox("Tile size controls the how local the changes to the world are (rebuild or carve). Small tile size allows more local changes, while potentially generating more data overall.", MessageType.None);
EditorGUILayout.HelpBox("Tile size controls the how local the changes to the world are (rebuild or carve). Small tile size allows more local changes, while potentially generating more data in overal.", MessageType.None);
}
EditorGUI.indentLevel--;
}
Expand Down Expand Up @@ -236,46 +287,45 @@ public override void OnInspectorGUI()
if (hadError)
EditorGUILayout.Space();

#if NAVMESHCOMPONENTS_SHOW_NAVMESHDATA_REF
var nmdRect = EditorGUILayout.GetControlRect(true, EditorGUIUtility.singleLineHeight);

EditorGUI.BeginProperty(nmdRect, GUIContent.none, m_NavMeshData);
var rectLabel = EditorGUI.PrefixLabel(nmdRect, GUIUtility.GetControlID(FocusType.Passive), new GUIContent(m_NavMeshData.displayName));
EditorGUI.EndProperty();

using (new EditorGUI.DisabledScope(true))
{
EditorGUI.BeginProperty(nmdRect, GUIContent.none, m_NavMeshData);
EditorGUI.ObjectField(rectLabel, m_NavMeshData, GUIContent.none);
EditorGUI.EndProperty();
}
#endif
using (new EditorGUI.DisabledScope(Application.isPlaying || m_AgentTypeID.intValue == -1))
{
GUILayout.BeginHorizontal();
GUILayout.Space(EditorGUIUtility.labelWidth);
if (GUILayout.Button("Clear"))
{
NavMeshAssetManager.instance.ClearSurfaces(targets);
foreach (NavMeshSurface s in targets)
ClearSurface(s);
SceneView.RepaintAll();
}

if (GUILayout.Button("Bake"))
{
NavMeshAssetManager.instance.StartBakingSurfaces(targets);
// Remove first to avoid double registration of the callback
EditorApplication.update -= UpdateAsyncBuildOperations;
EditorApplication.update += UpdateAsyncBuildOperations;

foreach (NavMeshSurface surf in targets)
{
var oper = new AsyncBakeOperation();

oper.bakeData = InitializeBakeData(surf);
oper.bakeOperation = surf.UpdateNavMesh(oper.bakeData);
oper.surface = surf;

s_BakeOperations.Add(oper);
}
}

GUILayout.EndHorizontal();
}

// Show progress for the selected targets
var bakeOperations = NavMeshAssetManager.instance.GetBakeOperations();
for (int i = bakeOperations.Count - 1; i >= 0; --i)
for (int i = s_BakeOperations.Count - 1; i >= 0; --i)
{
if (!targets.Contains(bakeOperations[i].surface))
if (!targets.Contains(s_BakeOperations[i].surface))
continue;

var oper = bakeOperations[i].bakeOperation;
var oper = s_BakeOperations[i].bakeOperation;
if (oper == null)
continue;

Expand All @@ -290,9 +340,9 @@ public override void OnInspectorGUI()

if (GUILayout.Button("Cancel", EditorStyles.miniButton))
{
var bakeData = bakeOperations[i].bakeData;
var bakeData = s_BakeOperations[i].bakeData;
UnityEngine.AI.NavMeshBuilder.Cancel(bakeData);
bakeOperations.RemoveAt(i);
s_BakeOperations.RemoveAt(i);
}

EditorGUI.ProgressBar(EditorGUILayout.GetControlRect(), p, "Baking: " + (int)(100 * p) + "%");
Expand All @@ -303,6 +353,41 @@ public override void OnInspectorGUI()
}
}

static NavMeshData InitializeBakeData(NavMeshSurface surface)
{
var emptySources = new List<NavMeshBuildSource>();
var emptyBounds = new Bounds();
return UnityEngine.AI.NavMeshBuilder.BuildNavMeshData(surface.GetBuildSettings(), emptySources, emptyBounds
, surface.transform.position, surface.transform.rotation);
}

static void UpdateAsyncBuildOperations()
{
foreach (var oper in s_BakeOperations)
{
if (oper.surface == null || oper.bakeOperation == null)
continue;

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;
if (surface.isActiveAndEnabled)
surface.AddData();
CreateNavMeshAsset(surface);
EditorSceneManager.MarkSceneDirty(surface.gameObject.scene);
}
}
s_BakeOperations.RemoveAll(o => o.bakeOperation == null || o.bakeOperation.isDone);
if (s_BakeOperations.Count == 0)
EditorApplication.update -= UpdateAsyncBuildOperations;
}

[DrawGizmo(GizmoType.Selected | GizmoType.Active | GizmoType.Pickable)]
static void RenderBoxGizmoSelected(NavMeshSurface navSurface, GizmoType gizmoType)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,3 @@
{
"name": "NavMeshComponents",
"references": [],
"optionalUnityReferences": [],
"includePlatforms": [],
"excludePlatforms": [],
"allowUnsafeCode": false
}
{
"name": "NavMeshComponents"
}

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 8d17a3d

Please sign in to comment.