init
This commit is contained in:
commit
341a877b4a
2338 changed files with 1346408 additions and 0 deletions
79
Assets/NavMesh/Examples/Scripts/AgentLinkMover.cs
Normal file
79
Assets/NavMesh/Examples/Scripts/AgentLinkMover.cs
Normal file
|
@ -0,0 +1,79 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using System.Collections;
|
||||
|
||||
public enum OffMeshLinkMoveMethod
|
||||
{
|
||||
Teleport,
|
||||
NormalSpeed,
|
||||
Parabola,
|
||||
Curve
|
||||
}
|
||||
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class AgentLinkMover : MonoBehaviour
|
||||
{
|
||||
public OffMeshLinkMoveMethod m_Method = OffMeshLinkMoveMethod.Parabola;
|
||||
public AnimationCurve m_Curve = new AnimationCurve();
|
||||
|
||||
IEnumerator Start()
|
||||
{
|
||||
NavMeshAgent agent = GetComponent<NavMeshAgent>();
|
||||
agent.autoTraverseOffMeshLink = false;
|
||||
while (true)
|
||||
{
|
||||
if (agent.isOnOffMeshLink)
|
||||
{
|
||||
if (m_Method == OffMeshLinkMoveMethod.NormalSpeed)
|
||||
yield return StartCoroutine(NormalSpeed(agent));
|
||||
else if (m_Method == OffMeshLinkMoveMethod.Parabola)
|
||||
yield return StartCoroutine(Parabola(agent, 2.0f, 0.5f));
|
||||
else if (m_Method == OffMeshLinkMoveMethod.Curve)
|
||||
yield return StartCoroutine(Curve(agent, 0.5f));
|
||||
agent.CompleteOffMeshLink();
|
||||
}
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator NormalSpeed(NavMeshAgent agent)
|
||||
{
|
||||
OffMeshLinkData data = agent.currentOffMeshLinkData;
|
||||
Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
|
||||
while (agent.transform.position != endPos)
|
||||
{
|
||||
agent.transform.position = Vector3.MoveTowards(agent.transform.position, endPos, agent.speed * Time.deltaTime);
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator Parabola(NavMeshAgent agent, float height, float duration)
|
||||
{
|
||||
OffMeshLinkData data = agent.currentOffMeshLinkData;
|
||||
Vector3 startPos = agent.transform.position;
|
||||
Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
|
||||
float normalizedTime = 0.0f;
|
||||
while (normalizedTime < 1.0f)
|
||||
{
|
||||
float yOffset = height * 4.0f * (normalizedTime - normalizedTime * normalizedTime);
|
||||
agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
|
||||
normalizedTime += Time.deltaTime / duration;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
|
||||
IEnumerator Curve(NavMeshAgent agent, float duration)
|
||||
{
|
||||
OffMeshLinkData data = agent.currentOffMeshLinkData;
|
||||
Vector3 startPos = agent.transform.position;
|
||||
Vector3 endPos = data.endPos + Vector3.up * agent.baseOffset;
|
||||
float normalizedTime = 0.0f;
|
||||
while (normalizedTime < 1.0f)
|
||||
{
|
||||
float yOffset = m_Curve.Evaluate(normalizedTime);
|
||||
agent.transform.position = Vector3.Lerp(startPos, endPos, normalizedTime) + yOffset * Vector3.up;
|
||||
normalizedTime += Time.deltaTime / duration;
|
||||
yield return null;
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/AgentLinkMover.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/AgentLinkMover.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 03bc8063c9e95401f9a8a38bc716750d
|
||||
timeCreated: 1460116525
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
25
Assets/NavMesh/Examples/Scripts/ClickToMove.cs
Normal file
25
Assets/NavMesh/Examples/Scripts/ClickToMove.cs
Normal file
|
@ -0,0 +1,25 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
// Use physics raycast hit from mouse click to set agent destination
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class ClickToMove : MonoBehaviour
|
||||
{
|
||||
NavMeshAgent m_Agent;
|
||||
RaycastHit m_HitInfo = new RaycastHit();
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_Agent = GetComponent<NavMeshAgent>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetMouseButtonDown(0) && !Input.GetKey(KeyCode.LeftShift))
|
||||
{
|
||||
var ray = Camera.main.ScreenPointToRay(Input.mousePosition);
|
||||
if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
|
||||
m_Agent.destination = m_HitInfo.point;
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/ClickToMove.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/ClickToMove.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c9472e73a59274b9b8da79d72004bd8b
|
||||
timeCreated: 1430734266
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
13
Assets/NavMesh/Examples/Scripts/DestroyOnTrigger.cs
Normal file
13
Assets/NavMesh/Examples/Scripts/DestroyOnTrigger.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using UnityEngine;
|
||||
|
||||
// Destroy owning GameObject if any collider with a specific tag is trespassing
|
||||
public class DestroyOnTrigger : MonoBehaviour
|
||||
{
|
||||
public string m_Tag = "Player";
|
||||
|
||||
void OnTriggerEnter(Collider other)
|
||||
{
|
||||
if (other.gameObject.tag == m_Tag)
|
||||
Destroy(gameObject);
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/DestroyOnTrigger.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/DestroyOnTrigger.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c786c4b29e72040b881bf13065fb9788
|
||||
timeCreated: 1430999621
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
50
Assets/NavMesh/Examples/Scripts/DungeonManager.cs
Normal file
50
Assets/NavMesh/Examples/Scripts/DungeonManager.cs
Normal file
|
@ -0,0 +1,50 @@
|
|||
using UnityEngine;
|
||||
|
||||
[DefaultExecutionOrder(-200)]
|
||||
public class DungeonManager : MonoBehaviour
|
||||
{
|
||||
public int m_Width = 10;
|
||||
public int m_Height = 10;
|
||||
public float m_Spacing = 4.0f;
|
||||
public GameObject[] m_Tiles = new GameObject[16];
|
||||
|
||||
void Awake()
|
||||
{
|
||||
Random.InitState(23431);
|
||||
var map = new int[m_Width * m_Height];
|
||||
for (int y = 0; y < m_Height; y++)
|
||||
{
|
||||
for (int x = 0; x < m_Width; x++)
|
||||
{
|
||||
bool px = false;
|
||||
bool py = false;
|
||||
if (x > 0)
|
||||
px = (map[(x - 1) + y * m_Width] & 1) != 0;
|
||||
if (y > 0)
|
||||
py = (map[x + (y - 1) * m_Width] & 2) != 0;
|
||||
|
||||
int tile = 0;
|
||||
if (px)
|
||||
tile |= 4;
|
||||
if (py)
|
||||
tile |= 8;
|
||||
if (x + 1 < m_Width && Random.value > 0.5f)
|
||||
tile |= 1;
|
||||
if (y + 1 < m_Height && Random.value > 0.5f)
|
||||
tile |= 2;
|
||||
|
||||
map[x + y * m_Width] = tile;
|
||||
}
|
||||
}
|
||||
|
||||
for (int y = 0; y < m_Height; y++)
|
||||
{
|
||||
for (int x = 0; x < m_Width; x++)
|
||||
{
|
||||
var pos = new Vector3(x * m_Spacing, 0, y * m_Spacing);
|
||||
if (m_Tiles[map[x + y * m_Width]] != null)
|
||||
Instantiate(m_Tiles[map[x + y * m_Width]], pos, Quaternion.identity);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/DungeonManager.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/DungeonManager.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c56d911659e3c4b499e11b47a73a4dbb
|
||||
timeCreated: 1430743428
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
26
Assets/NavMesh/Examples/Scripts/EnableIffSleeping.cs
Normal file
26
Assets/NavMesh/Examples/Scripts/EnableIffSleeping.cs
Normal file
|
@ -0,0 +1,26 @@
|
|||
using UnityEngine;
|
||||
|
||||
// Enables a behaviour when a rigidbody settles movement
|
||||
// otherwise disables the behaviour
|
||||
public class EnableIffSleeping : MonoBehaviour
|
||||
{
|
||||
public Behaviour m_Behaviour;
|
||||
Rigidbody m_Rigidbody;
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_Rigidbody = GetComponent<Rigidbody>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (m_Rigidbody == null || m_Behaviour == null)
|
||||
return;
|
||||
|
||||
if (m_Rigidbody.IsSleeping() && !m_Behaviour.enabled)
|
||||
m_Behaviour.enabled = true;
|
||||
|
||||
if (!m_Rigidbody.IsSleeping() && m_Behaviour.enabled)
|
||||
m_Behaviour.enabled = false;
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/EnableIffSleeping.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/EnableIffSleeping.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c56046daca8eb46f3af030c05f030e37
|
||||
timeCreated: 1431001396
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
58
Assets/NavMesh/Examples/Scripts/FreeCam.cs
Normal file
58
Assets/NavMesh/Examples/Scripts/FreeCam.cs
Normal file
|
@ -0,0 +1,58 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class FreeCam : MonoBehaviour
|
||||
{
|
||||
public enum RotationAxes { MouseXAndY = 0, MouseX = 1, MouseY = 2 }
|
||||
public RotationAxes axes = RotationAxes.MouseXAndY;
|
||||
public float sensitivityX = 15F;
|
||||
public float sensitivityY = 15F;
|
||||
|
||||
public float minimumX = -360F;
|
||||
public float maximumX = 360F;
|
||||
|
||||
public float minimumY = -60F;
|
||||
public float maximumY = 60F;
|
||||
|
||||
public float moveSpeed = 1.0f;
|
||||
|
||||
public bool lockHeight = false;
|
||||
|
||||
float rotationY = 0F;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (axes == RotationAxes.MouseXAndY)
|
||||
{
|
||||
float rotationX = transform.localEulerAngles.y + Input.GetAxis("Mouse X") * sensitivityX;
|
||||
|
||||
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
|
||||
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
|
||||
|
||||
transform.localEulerAngles = new Vector3(-rotationY, rotationX, 0);
|
||||
}
|
||||
else if (axes == RotationAxes.MouseX)
|
||||
{
|
||||
transform.Rotate(0, Input.GetAxis("Mouse X") * sensitivityX, 0);
|
||||
}
|
||||
else
|
||||
{
|
||||
rotationY += Input.GetAxis("Mouse Y") * sensitivityY;
|
||||
rotationY = Mathf.Clamp(rotationY, minimumY, maximumY);
|
||||
|
||||
transform.localEulerAngles = new Vector3(-rotationY, transform.localEulerAngles.y, 0);
|
||||
}
|
||||
|
||||
var xAxisValue = Input.GetAxis("Horizontal");
|
||||
var zAxisValue = Input.GetAxis("Vertical");
|
||||
if (lockHeight)
|
||||
{
|
||||
var dir = transform.TransformDirection(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
|
||||
dir.y = 0.0f;
|
||||
transform.position += dir;
|
||||
}
|
||||
else
|
||||
{
|
||||
transform.Translate(new Vector3(xAxisValue, 0.0f, zAxisValue) * moveSpeed);
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/FreeCam.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/FreeCam.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: b6c30eb361e754b75ac25325c5d024bd
|
||||
timeCreated: 1430921534
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
90
Assets/NavMesh/Examples/Scripts/LocalNavMeshBuilder.cs
Normal file
90
Assets/NavMesh/Examples/Scripts/LocalNavMeshBuilder.cs
Normal file
|
@ -0,0 +1,90 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using NavMeshBuilder = UnityEngine.AI.NavMeshBuilder;
|
||||
|
||||
// Build and update a localized navmesh from the sources marked by NavMeshSourceTag
|
||||
[DefaultExecutionOrder(-102)]
|
||||
public class LocalNavMeshBuilder : MonoBehaviour
|
||||
{
|
||||
// The center of the build
|
||||
public Transform m_Tracked;
|
||||
|
||||
// The size of the build bounds
|
||||
public Vector3 m_Size = new Vector3(80.0f, 20.0f, 80.0f);
|
||||
|
||||
NavMeshData m_NavMesh;
|
||||
AsyncOperation m_Operation;
|
||||
NavMeshDataInstance m_Instance;
|
||||
List<NavMeshBuildSource> m_Sources = new List<NavMeshBuildSource>();
|
||||
|
||||
IEnumerator Start()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
UpdateNavMesh(true);
|
||||
yield return m_Operation;
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
// Construct and add navmesh
|
||||
m_NavMesh = new NavMeshData();
|
||||
m_Instance = NavMesh.AddNavMeshData(m_NavMesh);
|
||||
if (m_Tracked == null)
|
||||
m_Tracked = transform;
|
||||
UpdateNavMesh(false);
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
// Unload navmesh and clear handle
|
||||
m_Instance.Remove();
|
||||
}
|
||||
|
||||
void UpdateNavMesh(bool asyncUpdate = false)
|
||||
{
|
||||
NavMeshSourceTag.Collect(ref m_Sources);
|
||||
var defaultBuildSettings = NavMesh.GetSettingsByID(0);
|
||||
var bounds = QuantizedBounds();
|
||||
|
||||
if (asyncUpdate)
|
||||
m_Operation = NavMeshBuilder.UpdateNavMeshDataAsync(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
|
||||
else
|
||||
NavMeshBuilder.UpdateNavMeshData(m_NavMesh, defaultBuildSettings, m_Sources, bounds);
|
||||
}
|
||||
|
||||
static Vector3 Quantize(Vector3 v, Vector3 quant)
|
||||
{
|
||||
float x = quant.x * Mathf.Floor(v.x / quant.x);
|
||||
float y = quant.y * Mathf.Floor(v.y / quant.y);
|
||||
float z = quant.z * Mathf.Floor(v.z / quant.z);
|
||||
return new Vector3(x, y, z);
|
||||
}
|
||||
|
||||
Bounds QuantizedBounds()
|
||||
{
|
||||
// Quantize the bounds to update only when theres a 10% change in size
|
||||
var center = m_Tracked ? m_Tracked.position : transform.position;
|
||||
return new Bounds(Quantize(center, 0.1f * m_Size), m_Size);
|
||||
}
|
||||
|
||||
void OnDrawGizmosSelected()
|
||||
{
|
||||
if (m_NavMesh)
|
||||
{
|
||||
Gizmos.color = Color.green;
|
||||
Gizmos.DrawWireCube(m_NavMesh.sourceBounds.center, m_NavMesh.sourceBounds.size);
|
||||
}
|
||||
|
||||
Gizmos.color = Color.yellow;
|
||||
var bounds = QuantizedBounds();
|
||||
Gizmos.DrawWireCube(bounds.center, bounds.size);
|
||||
|
||||
Gizmos.color = Color.green;
|
||||
var center = m_Tracked ? m_Tracked.position : transform.position;
|
||||
Gizmos.DrawWireCube(center, m_Size);
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/LocalNavMeshBuilder.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/LocalNavMeshBuilder.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 115dfe7b0938447cbbf9507076f54049
|
||||
timeCreated: 1454852118
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
76
Assets/NavMesh/Examples/Scripts/MeshTool.cs
Normal file
76
Assets/NavMesh/Examples/Scripts/MeshTool.cs
Normal file
|
@ -0,0 +1,76 @@
|
|||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
|
||||
// Manipulation tool for displacing the vertices in a list of meshes
|
||||
public class MeshTool : MonoBehaviour
|
||||
{
|
||||
public enum ExtrudeMethod
|
||||
{
|
||||
Vertical,
|
||||
MeshNormal
|
||||
}
|
||||
|
||||
public List<MeshFilter> m_Filters = new List<MeshFilter>();
|
||||
public float m_Radius = 1.5f;
|
||||
public float m_Power = 2.0f;
|
||||
public ExtrudeMethod m_Method = ExtrudeMethod.Vertical;
|
||||
|
||||
RaycastHit m_HitInfo = new RaycastHit();
|
||||
|
||||
void Start()
|
||||
{
|
||||
Cursor.lockState = CursorLockMode.Locked;
|
||||
Cursor.visible = false;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
var ray = new Ray(Camera.main.transform.position, Camera.main.transform.forward);
|
||||
if (Physics.Raycast(ray.origin, ray.direction, out m_HitInfo))
|
||||
{
|
||||
Debug.DrawRay(m_HitInfo.point, m_HitInfo.normal, Color.red);
|
||||
Vector3 displacement = (m_Method == ExtrudeMethod.Vertical) ? Vector3.up : m_HitInfo.normal;
|
||||
|
||||
if (Input.GetMouseButton(0) || (Input.GetKey(KeyCode.Space) && !Input.GetKey(KeyCode.LeftShift)))
|
||||
ModifyMesh(m_Power * displacement, m_HitInfo.point);
|
||||
if (Input.GetMouseButton(1) || (Input.GetKey(KeyCode.Space) && Input.GetKey(KeyCode.LeftShift)))
|
||||
ModifyMesh(-m_Power * displacement, m_HitInfo.point);
|
||||
}
|
||||
}
|
||||
|
||||
void ModifyMesh(Vector3 displacement, Vector3 center)
|
||||
{
|
||||
foreach (var filter in m_Filters)
|
||||
{
|
||||
Mesh mesh = filter.mesh;
|
||||
Vector3[] vertices = mesh.vertices;
|
||||
|
||||
for (int i = 0; i < vertices.Length; ++i)
|
||||
{
|
||||
Vector3 v = filter.transform.TransformPoint(vertices[i]);
|
||||
vertices[i] = vertices[i] + displacement * Gaussian(v, center, m_Radius);
|
||||
}
|
||||
|
||||
mesh.vertices = vertices;
|
||||
mesh.RecalculateBounds();
|
||||
|
||||
var col = filter.GetComponent<MeshCollider>();
|
||||
if (col != null)
|
||||
{
|
||||
var colliMesh = new Mesh();
|
||||
colliMesh.vertices = mesh.vertices;
|
||||
colliMesh.triangles = mesh.triangles;
|
||||
col.sharedMesh = colliMesh;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static float Gaussian(Vector3 pos, Vector3 mean, float dev)
|
||||
{
|
||||
float x = pos.x - mean.x;
|
||||
float y = pos.y - mean.y;
|
||||
float z = pos.z - mean.z;
|
||||
float n = 1.0f / (2.0f * Mathf.PI * dev * dev);
|
||||
return n * Mathf.Pow(2.718281828f, -(x * x + y * y + z * z) / (2.0f * dev * dev));
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/MeshTool.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/MeshTool.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 9d5a3e609186342b2a976cd32fd7d0f0
|
||||
timeCreated: 1430920188
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
134
Assets/NavMesh/Examples/Scripts/NavMeshPrefabInstance.cs
Normal file
134
Assets/NavMesh/Examples/Scripts/NavMeshPrefabInstance.cs
Normal file
|
@ -0,0 +1,134 @@
|
|||
using UnityEngine;
|
||||
using System.Collections.Generic;
|
||||
using UnityEngine.AI;
|
||||
|
||||
[ExecuteInEditMode]
|
||||
[DefaultExecutionOrder(-102)]
|
||||
public class NavMeshPrefabInstance : MonoBehaviour
|
||||
{
|
||||
[SerializeField]
|
||||
NavMeshData m_NavMesh;
|
||||
public NavMeshData navMeshData
|
||||
{
|
||||
get { return m_NavMesh; }
|
||||
set { m_NavMesh = value; }
|
||||
}
|
||||
|
||||
[SerializeField]
|
||||
bool m_FollowTransform;
|
||||
public bool followTransform
|
||||
{
|
||||
get { return m_FollowTransform; }
|
||||
set { SetFollowTransform(value); }
|
||||
}
|
||||
|
||||
NavMeshDataInstance m_Instance;
|
||||
|
||||
// Position Tracking
|
||||
static readonly List<NavMeshPrefabInstance> s_TrackedInstances = new List<NavMeshPrefabInstance>();
|
||||
public static List<NavMeshPrefabInstance> trackedInstances {get {return s_TrackedInstances; }}
|
||||
Vector3 m_Position;
|
||||
Quaternion m_Rotation;
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
AddInstance();
|
||||
|
||||
if (m_Instance.valid && m_FollowTransform)
|
||||
AddTracking();
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
m_Instance.Remove();
|
||||
RemoveTracking();
|
||||
}
|
||||
|
||||
public void UpdateInstance()
|
||||
{
|
||||
m_Instance.Remove();
|
||||
AddInstance();
|
||||
}
|
||||
|
||||
void AddInstance()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
if (m_Instance.valid)
|
||||
{
|
||||
Debug.LogError("Instance is already added: " + this);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (m_NavMesh)
|
||||
m_Instance = NavMesh.AddNavMeshData(m_NavMesh, transform.position, transform.rotation);
|
||||
|
||||
m_Rotation = transform.rotation;
|
||||
m_Position = transform.position;
|
||||
}
|
||||
|
||||
void AddTracking()
|
||||
{
|
||||
#if UNITY_EDITOR
|
||||
// At runtime we don't want linear lookup
|
||||
if (s_TrackedInstances.Contains(this))
|
||||
{
|
||||
Debug.LogError("Double registration of " + this);
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
if (s_TrackedInstances.Count == 0)
|
||||
NavMesh.onPreUpdate += UpdateTrackedInstances;
|
||||
s_TrackedInstances.Add(this);
|
||||
}
|
||||
|
||||
void RemoveTracking()
|
||||
{
|
||||
s_TrackedInstances.Remove(this);
|
||||
if (s_TrackedInstances.Count == 0)
|
||||
NavMesh.onPreUpdate -= UpdateTrackedInstances;
|
||||
}
|
||||
|
||||
void SetFollowTransform(bool value)
|
||||
{
|
||||
if (m_FollowTransform == value)
|
||||
return;
|
||||
m_FollowTransform = value;
|
||||
if (value)
|
||||
AddTracking();
|
||||
else
|
||||
RemoveTracking();
|
||||
}
|
||||
|
||||
bool HasMoved()
|
||||
{
|
||||
return m_Position != transform.position || m_Rotation != transform.rotation;
|
||||
}
|
||||
|
||||
static void UpdateTrackedInstances()
|
||||
{
|
||||
foreach (var instance in s_TrackedInstances)
|
||||
{
|
||||
if (instance.HasMoved())
|
||||
instance.UpdateInstance();
|
||||
}
|
||||
}
|
||||
|
||||
#if UNITY_EDITOR
|
||||
void OnValidate()
|
||||
{
|
||||
// Only when the instance is valid (OnEnable is called) - we react to changes caused by serialization
|
||||
if (!m_Instance.valid)
|
||||
return;
|
||||
// OnValidate can be called several times - avoid double registration
|
||||
// We afford this linear lookup in the editor only
|
||||
if (!m_FollowTransform)
|
||||
{
|
||||
RemoveTracking();
|
||||
}
|
||||
else if (!s_TrackedInstances.Contains(this))
|
||||
{
|
||||
AddTracking();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 4abfa16db9d114ec0ae6e10e4a566cb4
|
||||
timeCreated: 1454507331
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
79
Assets/NavMesh/Examples/Scripts/NavMeshSourceTag.cs
Normal file
79
Assets/NavMesh/Examples/Scripts/NavMeshSourceTag.cs
Normal file
|
@ -0,0 +1,79 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using System.Collections.Generic;
|
||||
|
||||
// Tagging component for use with the LocalNavMeshBuilder
|
||||
// Supports mesh-filter and terrain - can be extended to physics and/or primitives
|
||||
[DefaultExecutionOrder(-200)]
|
||||
public class NavMeshSourceTag : MonoBehaviour
|
||||
{
|
||||
// Global containers for all active mesh/terrain tags
|
||||
public static List<MeshFilter> m_Meshes = new List<MeshFilter>();
|
||||
public static List<Terrain> m_Terrains = new List<Terrain>();
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
var m = GetComponent<MeshFilter>();
|
||||
if (m != null)
|
||||
{
|
||||
m_Meshes.Add(m);
|
||||
}
|
||||
|
||||
var t = GetComponent<Terrain>();
|
||||
if (t != null)
|
||||
{
|
||||
m_Terrains.Add(t);
|
||||
}
|
||||
}
|
||||
|
||||
void OnDisable()
|
||||
{
|
||||
var m = GetComponent<MeshFilter>();
|
||||
if (m != null)
|
||||
{
|
||||
m_Meshes.Remove(m);
|
||||
}
|
||||
|
||||
var t = GetComponent<Terrain>();
|
||||
if (t != null)
|
||||
{
|
||||
m_Terrains.Remove(t);
|
||||
}
|
||||
}
|
||||
|
||||
// Collect all the navmesh build sources for enabled objects tagged by this component
|
||||
public static void Collect(ref List<NavMeshBuildSource> sources)
|
||||
{
|
||||
sources.Clear();
|
||||
|
||||
for (var i = 0; i < m_Meshes.Count; ++i)
|
||||
{
|
||||
var mf = m_Meshes[i];
|
||||
if (mf == null) continue;
|
||||
|
||||
var m = mf.sharedMesh;
|
||||
if (m == null) continue;
|
||||
|
||||
var s = new NavMeshBuildSource();
|
||||
s.shape = NavMeshBuildSourceShape.Mesh;
|
||||
s.sourceObject = m;
|
||||
s.transform = mf.transform.localToWorldMatrix;
|
||||
s.area = 0;
|
||||
sources.Add(s);
|
||||
}
|
||||
|
||||
for (var i = 0; i < m_Terrains.Count; ++i)
|
||||
{
|
||||
var t = m_Terrains[i];
|
||||
if (t == null) continue;
|
||||
|
||||
var s = new NavMeshBuildSource();
|
||||
s.shape = NavMeshBuildSourceShape.Terrain;
|
||||
s.sourceObject = t.terrainData;
|
||||
// Terrain system only supports translation - so we pass translation only to back-end
|
||||
s.transform = Matrix4x4.TRS(t.transform.position, Quaternion.identity, Vector3.one);
|
||||
s.area = 0;
|
||||
sources.Add(s);
|
||||
}
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/NavMeshSourceTag.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/NavMeshSourceTag.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 6e038bf700cfa444fb16c6ef963be9f9
|
||||
timeCreated: 1430814538
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
21
Assets/NavMesh/Examples/Scripts/Oscillator.cs
Normal file
21
Assets/NavMesh/Examples/Scripts/Oscillator.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
using UnityEngine;
|
||||
|
||||
// Makes a transform oscillate relative to its start position
|
||||
public class Oscillator : MonoBehaviour
|
||||
{
|
||||
public float m_Amplitude = 1.0f;
|
||||
public float m_Period = 1.0f;
|
||||
public Vector3 m_Direction = Vector3.up;
|
||||
Vector3 m_StartPosition;
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_StartPosition = transform.position;
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
var pos = m_StartPosition + m_Direction * m_Amplitude * Mathf.Sin(2.0f * Mathf.PI * Time.time / m_Period);
|
||||
transform.position = pos;
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/Oscillator.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/Oscillator.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c59ac8154f1b24ea7a85cab0f39a4489
|
||||
timeCreated: 1454359265
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
128
Assets/NavMesh/Examples/Scripts/RandomInstancing.cs
Normal file
128
Assets/NavMesh/Examples/Scripts/RandomInstancing.cs
Normal file
|
@ -0,0 +1,128 @@
|
|||
using UnityEngine;
|
||||
|
||||
using System.Collections.Generic;
|
||||
|
||||
// Fill 5x5 tiles around the local position procedurally by instantiating prefabs at random positions/orientations
|
||||
[DefaultExecutionOrder(-200)]
|
||||
public class RandomInstancing : MonoBehaviour
|
||||
{
|
||||
public GameObject m_Prefab;
|
||||
public int m_PoolSize = 250;
|
||||
public int m_InstancesPerTile = 10;
|
||||
public bool m_RandomPosition = true;
|
||||
public bool m_RandomOrientation = true;
|
||||
public float m_Height;
|
||||
|
||||
public int m_BaseHash = 347652783;
|
||||
public float m_Size = 100.0f;
|
||||
|
||||
List<Transform> m_Instances = new List<Transform>();
|
||||
int m_Used;
|
||||
int m_LocX, m_LocZ;
|
||||
|
||||
void Awake()
|
||||
{
|
||||
for (int i = 0; i < m_PoolSize; ++i)
|
||||
{
|
||||
var go = Instantiate(m_Prefab, Vector3.zero, Quaternion.identity) as GameObject;
|
||||
go.SetActive(false);
|
||||
m_Instances.Add(go.transform);
|
||||
}
|
||||
}
|
||||
|
||||
void OnEnable()
|
||||
{
|
||||
m_LocX = ~0;
|
||||
m_LocZ = ~0;
|
||||
UpdateInstances();
|
||||
}
|
||||
|
||||
void OnDestroy()
|
||||
{
|
||||
for (int i = 0; i < m_Instances.Count; ++i)
|
||||
{
|
||||
if (m_Instances[i])
|
||||
Destroy(m_Instances[i].gameObject);
|
||||
}
|
||||
m_Instances.Clear();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
UpdateInstances();
|
||||
}
|
||||
|
||||
void UpdateInstances()
|
||||
{
|
||||
var x = (int)Mathf.Floor(transform.position.x / m_Size);
|
||||
var z = (int)Mathf.Floor(transform.position.z / m_Size);
|
||||
if (x == m_LocX && z == m_LocZ)
|
||||
return;
|
||||
|
||||
m_LocX = x;
|
||||
m_LocZ = z;
|
||||
|
||||
m_Used = 0;
|
||||
for (var i = x - 2; i <= x + 2; ++i)
|
||||
{
|
||||
for (var j = z - 2; j <= z + 2; ++j)
|
||||
{
|
||||
var count = UpdateTileInstances(i, j);
|
||||
if (count != m_InstancesPerTile)
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Deactivate the remaining active elements in the pool.
|
||||
// Here we assume all active elements are contiguous and first in the list.
|
||||
for (int i = m_Used; i < m_PoolSize && m_Instances[i].gameObject.activeSelf; ++i)
|
||||
m_Instances[i].gameObject.SetActive(false);
|
||||
}
|
||||
|
||||
int UpdateTileInstances(int i, int j)
|
||||
{
|
||||
var seed = Hash2(i, j) ^ m_BaseHash;
|
||||
var count = System.Math.Min(m_InstancesPerTile, m_PoolSize - m_Used);
|
||||
for (var end = m_Used + count; m_Used < end; ++m_Used)
|
||||
{
|
||||
float x = 0;
|
||||
float y = 0;
|
||||
|
||||
if (m_RandomPosition)
|
||||
{
|
||||
x = Random(ref seed);
|
||||
y = Random(ref seed);
|
||||
}
|
||||
var pos = new Vector3((i + x) * m_Size, m_Height, (j + y) * m_Size);
|
||||
|
||||
if (m_RandomOrientation)
|
||||
{
|
||||
float r = 360.0f * Random(ref seed);
|
||||
m_Instances[m_Used].rotation = Quaternion.AngleAxis(r, Vector3.up);
|
||||
}
|
||||
m_Instances[m_Used].position = pos;
|
||||
m_Instances[m_Used].gameObject.SetActive(true);
|
||||
}
|
||||
|
||||
if (count < m_InstancesPerTile)
|
||||
Debug.LogWarning("Pool exhausted", this);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
static int Hash2(int i, int j)
|
||||
{
|
||||
return (i * 73856093) ^ (j * 19349663);
|
||||
}
|
||||
|
||||
static float Random(ref int seed)
|
||||
{
|
||||
seed = (seed ^ 123459876);
|
||||
var k = seed / 127773;
|
||||
seed = 16807 * (seed - k * 127773) - 2836 * k;
|
||||
if (seed < 0) seed = seed + 2147483647;
|
||||
float ran0 = seed * 1.0f / 2147483647.0f;
|
||||
seed = (seed ^ 123459876);
|
||||
return ran0;
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/RandomInstancing.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/RandomInstancing.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: 0805752a20e62489680c7c8ba929bccc
|
||||
timeCreated: 1430813999
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
23
Assets/NavMesh/Examples/Scripts/RandomWalk.cs
Normal file
23
Assets/NavMesh/Examples/Scripts/RandomWalk.cs
Normal file
|
@ -0,0 +1,23 @@
|
|||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
|
||||
// Walk to a random position and repeat
|
||||
[RequireComponent(typeof(NavMeshAgent))]
|
||||
public class RandomWalk : MonoBehaviour
|
||||
{
|
||||
public float m_Range = 25.0f;
|
||||
NavMeshAgent m_Agent;
|
||||
|
||||
void Start()
|
||||
{
|
||||
m_Agent = GetComponent<NavMeshAgent>();
|
||||
}
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (m_Agent.pathPending || m_Agent.remainingDistance > 0.1f)
|
||||
return;
|
||||
|
||||
m_Agent.destination = m_Range * Random.insideUnitCircle;
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/RandomWalk.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/RandomWalk.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: dd76b8ee2dbcf4be6998e917c65cd6ed
|
||||
timeCreated: 1431075769
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
13
Assets/NavMesh/Examples/Scripts/SpawnPrefabOnKeyDown.cs
Normal file
13
Assets/NavMesh/Examples/Scripts/SpawnPrefabOnKeyDown.cs
Normal file
|
@ -0,0 +1,13 @@
|
|||
using UnityEngine;
|
||||
|
||||
public class SpawnPrefabOnKeyDown : MonoBehaviour
|
||||
{
|
||||
public GameObject m_Prefab;
|
||||
public KeyCode m_KeyCode;
|
||||
|
||||
void Update()
|
||||
{
|
||||
if (Input.GetKeyDown(m_KeyCode) && m_Prefab != null)
|
||||
Instantiate(m_Prefab, transform.position, transform.rotation);
|
||||
}
|
||||
}
|
12
Assets/NavMesh/Examples/Scripts/SpawnPrefabOnKeyDown.cs.meta
Normal file
12
Assets/NavMesh/Examples/Scripts/SpawnPrefabOnKeyDown.cs.meta
Normal file
|
@ -0,0 +1,12 @@
|
|||
fileFormatVersion: 2
|
||||
guid: c751607f01df8445c86b012985b8b152
|
||||
timeCreated: 1430998893
|
||||
licenseType: Pro
|
||||
MonoImporter:
|
||||
serializedVersion: 2
|
||||
defaultReferences: []
|
||||
executionOrder: 0
|
||||
icon: {instanceID: 0}
|
||||
userData:
|
||||
assetBundleName:
|
||||
assetBundleVariant:
|
Loading…
Add table
Add a link
Reference in a new issue