init
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 7d9da26ac20749b18818b4a4cefe65ea
|
||||
timeCreated: 1761774471
|
||||
@@ -0,0 +1,110 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using UnityEngine;
|
||||
using UnityEngine.AI;
|
||||
using UnityEngine.Rendering;
|
||||
using UnityEngine.Serialization;
|
||||
|
||||
namespace RPGCore.SceneModules.PathVisualizerSceneModule
|
||||
{
|
||||
public class PathVisualizer : MonoBehaviour
|
||||
{
|
||||
private Func<IEnumerable<Vector3>> _pathGetter;
|
||||
private LineRenderer _lineRenderer;
|
||||
|
||||
private float _autoUpdateDelta;
|
||||
|
||||
public bool autoUpdate;
|
||||
public float autoUpdateTime;
|
||||
public float verticalOffset;
|
||||
|
||||
public float lineSize
|
||||
{
|
||||
get => _lineRenderer.startWidth;
|
||||
set => _lineRenderer.startWidth = _lineRenderer.endWidth = value;
|
||||
}
|
||||
|
||||
public Material material
|
||||
{
|
||||
get => _lineRenderer.material;
|
||||
set => _lineRenderer.material = value;
|
||||
}
|
||||
|
||||
public void SetPath(Vector3 start, Vector3 end, int navMeshAreaMask)
|
||||
{
|
||||
var path = new NavMeshPath();
|
||||
NavMesh.CalculatePath(start, end, navMeshAreaMask, path);
|
||||
_pathGetter = () => path.corners;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
public void SetPath(NavMeshAgent agent, Vector3 destination)
|
||||
{
|
||||
var path = new NavMeshPath();
|
||||
agent.CalculatePath(destination, path);
|
||||
_pathGetter = () => path.corners;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
public void SetPath(NavMeshPath path)
|
||||
{
|
||||
_pathGetter = () => path.corners;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
public void SetPath(IEnumerable<Transform> customPath)
|
||||
{
|
||||
_pathGetter = () => customPath.Select(t => t.position);
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
public void SetPath(IEnumerable<Vector3> customPath)
|
||||
{
|
||||
_pathGetter = () => customPath;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
public void SetPath(Func<IEnumerable<Vector3>> pathGetter)
|
||||
{
|
||||
_pathGetter = pathGetter;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
private void Awake()
|
||||
{
|
||||
_lineRenderer = gameObject.AddComponent<LineRenderer>();
|
||||
_lineRenderer.enabled = true;
|
||||
_lineRenderer.shadowCastingMode = ShadowCastingMode.Off;
|
||||
_lineRenderer.numCapVertices = 3;
|
||||
_lineRenderer.numCornerVertices = 3;
|
||||
}
|
||||
|
||||
private void FixedUpdate()
|
||||
{
|
||||
if (!autoUpdate) return;
|
||||
_autoUpdateDelta += Time.fixedDeltaTime;
|
||||
if (_autoUpdateDelta < autoUpdateTime) return;
|
||||
_autoUpdateDelta = 0;
|
||||
UpdatePath();
|
||||
}
|
||||
|
||||
private void UpdatePath()
|
||||
{
|
||||
if (_pathGetter == null) return;
|
||||
var path = _pathGetter().Select(pos => pos + Vector3.up * verticalOffset).ToArray();
|
||||
_lineRenderer.positionCount = path.Length;
|
||||
_lineRenderer.SetPositions(path);
|
||||
}
|
||||
|
||||
public void Show()
|
||||
{
|
||||
_lineRenderer.enabled = true;
|
||||
}
|
||||
|
||||
public void Hide()
|
||||
{
|
||||
_lineRenderer.enabled = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6fdd2eabc020430e881b60cda5926a0a
|
||||
timeCreated: 1761830900
|
||||
@@ -0,0 +1,34 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using RPGCore.Core;
|
||||
using UnityEngine;
|
||||
|
||||
namespace RPGCore.SceneModules.PathVisualizerSceneModule
|
||||
{
|
||||
[Serializable]
|
||||
public class PathVisualizerModule : SceneModule
|
||||
{
|
||||
// Configuration
|
||||
[SerializeField] private float _defaultAutoUpdateTime = 0.1f;
|
||||
[SerializeField] private float _defaultLineSize = 0.05f;
|
||||
[SerializeField] private float _defaultVerticalOffset = 0.1f;
|
||||
[SerializeField] private Material _defaultMaterial;
|
||||
|
||||
// References
|
||||
internal List<PathVisualizer> pathVisualizers = new();
|
||||
|
||||
public PathVisualizer Create()
|
||||
{
|
||||
var gameObject = new GameObject("PathVisualizer");
|
||||
gameObject.transform.parent = this.gameObject.transform;
|
||||
|
||||
var pathVisualizer = gameObject.AddComponent<PathVisualizer>();
|
||||
pathVisualizers.Add(pathVisualizer);
|
||||
pathVisualizer.autoUpdateTime = _defaultAutoUpdateTime;
|
||||
pathVisualizer.lineSize = _defaultLineSize;
|
||||
pathVisualizer.verticalOffset = _defaultVerticalOffset;
|
||||
pathVisualizer.material = _defaultMaterial;
|
||||
return pathVisualizer;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
fileFormatVersion: 2
|
||||
guid: 6eeee4c859ea44119fd2347e9b778b72
|
||||
timeCreated: 1761830760
|
||||
Reference in New Issue
Block a user