Skip to content

Instantly share code, notes, and snippets.

@khadzhynov
Created May 30, 2020 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save khadzhynov/933134bac5bcb747c4ecd31f815c72a6 to your computer and use it in GitHub Desktop.
Save khadzhynov/933134bac5bcb747c4ecd31f815c72a6 to your computer and use it in GitHub Desktop.
Code snippets for Unity NavMesh on sphere tutorial
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.AI;
[Serializable]
public struct NavMeshChunk
{
public Vector3 EulerRotation;
//---- DRAG HERE YOUR BAKED NAVMESH CHUNK
public NavMeshData Data;
public bool Enabled;
}
public class NavMeshSphere : MonoBehaviour
{
//----- HERE ARE YOUR BAKED NAVMESH CHUNKS
[SerializeField]
private List<NavMeshChunk> _navMeshChunks;
[SerializeField]
private List<NavMeshDataInstance> _instances = new List<NavMeshDataInstance>();
[SerializeField]
private Transform _pivot;
public void OnEnable()
{
RemoveAllNavMeshLoadedData();
LoadNavmeshData();
}
public void RemoveAllNavMeshLoadedData()
{
NavMesh.RemoveAllNavMeshData();
}
public void LoadNavmeshData()
{
foreach(var chunk in _navMeshChunks)
{
if (chunk.Enabled)
{
_instances.Add(
NavMesh.AddNavMeshData(
chunk.Data,
_pivot.transform.position,
Quaternion.Euler(chunk.EulerRotation)));
}
}
}
public void OnDisable()
{
foreach(var instance in _instances)
{
instance.Remove();
}
}
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
[CustomEditor(typeof(NavMeshSphere))]
public class NavMeshSphereEditor : Editor
{
public override void OnInspectorGUI()
{
base.OnInspectorGUI();
if (GUILayout.Button("Load navmesh data"))
{
(target as NavMeshSphere).LoadNavmeshData();
}
if (GUILayout.Button("remove navmesh data"))
{
(target as NavMeshSphere).RemoveAllNavMeshLoadedData();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment