浏览代码

Pathway component with Idea (#373)

* Pathway component with Idea

* Pathway component

deletion of error messages

* Pathway component

cleaning Pathway component

* pathway interactive

* Update PathwayGizmos.cs

* cleaning

* Pathway component

 update

* Pathway component

correction

* Phatway update

* Update PathWayNavMeshUI.cs

* Update PathWayNavMeshUI.cs

* Update PathWayNavMeshUI.cs

* Rename variables

add a tooltip

* Rename variables

Add hide pathway option

* Update PathWayNavMeshUI.cs

* rename

add HasNavMeshAt

* Correction DisplayHandles()

incorrect use of EditorGUI.BeginChangeCheck ();

* Update PathwayHandles.cs

* correction

HasNavMeshAt, I forgot the delete case
Gismos just a rewrite

* less calculus more data

* cleaning

* Update PathWayNavMeshUI.cs

Correction

* correction

* update

update in the path only the points moved by the handles

* correction

* small fixes
/devlogs-4-addressable-assets
GitHub 4 年前
当前提交
47599961
共有 10 个文件被更改,包括 400 次插入43 次删除
  1. 63
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs
  2. 2
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs.meta
  3. 75
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayGizmos.cs
  4. 2
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayGizmos.cs.meta
  5. 17
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayHandles.cs
  6. 40
      UOP1_Project/Assets/Scripts/Pathway/Pathway.cs
  7. 105
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathWayNavMeshUI.cs
  8. 11
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathWayNavMeshUI.cs.meta
  9. 117
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayNavMesh.cs
  10. 11
      UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayNavMesh.cs.meta

63
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs


using UnityEngine;
using UnityEditor;
using UnityEditorInternal;
using System.Linq;
[CustomEditor(typeof(Pathway))]
public class PathwayEditor : Editor

private PathwayHandles _pathwayHandles;
private PathWayNavMeshUI _pathWayNavMeshUI;
private enum LIST_MODIFICATION { ADD, SUPP, DRAG, OTHER};
private LIST_MODIFICATION _currentListModification;
private int _indexCurrentModification;
_pathwayHandles.DispalyHandles();
int index = _pathwayHandles.DisplayHandles();
_pathWayNavMeshUI.RealTime(index);
}
public override void OnInspectorGUI()

_reorderableList.DoLayoutList();
_pathWayNavMeshUI.OnInspectorGUI();
Tools.hidden = _pathway.HidePathway;
serializedObject.ApplyModifiedProperties();
}

_reorderableList.drawElementCallback += DrawElement;
_reorderableList.onAddCallback += AddItem;
_reorderableList.onRemoveCallback += RemoveItem;
_reorderableList.onSelectCallback += SelectItem;
_reorderableList.onMouseDragCallback += DragItem;
_pathWayNavMeshUI = new PathWayNavMeshUI(_pathway);
_currentListModification = LIST_MODIFICATION.OTHER;
}
private void OnDisable()

_reorderableList.drawElementCallback -= DrawElement;
_reorderableList.onAddCallback -= AddItem;
_reorderableList.onRemoveCallback -= RemoveItem;
_reorderableList.onSelectCallback -= SelectItem;
_reorderableList.onMouseDragCallback -= DragItem;
_pathway.waypoints = _pathway.Waypoints.Select(x => x.waypoint).ToArray();
}
private void DrawHeader(Rect rect)

private void DrawElement(Rect rect, int index, bool active, bool focused)
{
SerializedProperty item = _reorderableList.serializedProperty.GetArrayElementAtIndex(index);
SerializedProperty item = _reorderableList.serializedProperty.GetArrayElementAtIndex(index).FindPropertyRelative("waypoint");
item.vector3Value = EditorGUI.Vector3Field(rect, Pathway.FIELD_LABEL + index, item.vector3Value);
}

if (index > -1 && list.serializedProperty.arraySize >= 1)
{
list.serializedProperty.InsertArrayElementAtIndex(index + 1);
Vector3 previous = list.serializedProperty.GetArrayElementAtIndex(index).vector3Value;
list.serializedProperty.GetArrayElementAtIndex(index + 1).vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
Vector3 previous = list.serializedProperty.GetArrayElementAtIndex(index).FindPropertyRelative("waypoint").vector3Value;
list.serializedProperty.GetArrayElementAtIndex(index + 1).FindPropertyRelative("waypoint").vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
_indexCurrentModification = index + 1;
list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1).vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
list.serializedProperty.GetArrayElementAtIndex(list.serializedProperty.arraySize - 1).FindPropertyRelative("waypoint").vector3Value = new Vector3(previous.x + 2, previous.y, previous.z + 2);
_indexCurrentModification = list.serializedProperty.arraySize - 1;
_currentListModification = LIST_MODIFICATION.ADD;
list.index++;
}

{
list.index--;
}
_indexCurrentModification = index - 1;
_currentListModification = LIST_MODIFICATION.SUPP;
private void SelectItem(ReorderableList list)
private void DragItem(ReorderableList list)
InternalEditorUtility.RepaintAllViews();
_indexCurrentModification = list.index;
_currentListModification = LIST_MODIFICATION.DRAG;
switch (_currentListModification)
{
case LIST_MODIFICATION.ADD:
_pathWayNavMeshUI.UpdatePathAt(_indexCurrentModification);
break;
case LIST_MODIFICATION.SUPP:
if (list.serializedProperty.arraySize > 1)
{
_pathWayNavMeshUI.UpdatePathAt((list.serializedProperty.arraySize + _indexCurrentModification) % list.serializedProperty.arraySize);
}
break;
case LIST_MODIFICATION.DRAG:
_pathWayNavMeshUI.UpdatePathAt(list.index);
_pathWayNavMeshUI.UpdatePathAt(_indexCurrentModification);
break;
default:
break;
}
_currentListModification = LIST_MODIFICATION.OTHER;
}
private void DoUndo()

if (_reorderableList.index >= _reorderableList.serializedProperty.arraySize)
if (_reorderableList.index >= _reorderableList.serializedProperty.arraySize )
_pathWayNavMeshUI.GeneratePath();
}
}

2
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayEditor.cs.meta


fileFormatVersion: 2
guid: 69db5c2486715ce408863f0c9c620ca8
guid: c708a9ee9f8e6ce4fbbeca9f47bd420b
MonoImporter:
externalObjects: {}
serializedVersion: 2

75
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayGizmos.cs


using UnityEngine;
using UnityEditor;
using System.Collections.Generic;
public class PathwayGizmos
public class PathwayGizmos
{
{
if (!pathway.ToggledNavMeshDisplay)
{
DrawHandlesPath(pathway);
}
else
{
DrawNavMeshPath(pathway);
}
DrawHandlesPath(pathway);
DrawHitPoints(pathway);
private static void DrawElements(Pathway pathway, List<Vector3> path, int index)
private static void DrawLabel(Pathway pathway,Vector3 path, int index)
{
GUIStyle style = new GUIStyle();
Vector3 textHeight = Vector3.up;

Handles.Label(path[index] + textHeight, index.ToString(), style);
Handles.Label(path + textHeight, index.ToString(), style);
Handles.color = pathway.LineColor;
DrawElements(pathway, pathway.Waypoints, 0);
}
DrawLabel(pathway, pathway.Waypoints[0].waypoint, 0);
for (int i = 0; i < pathway.Waypoints.Count; i++)
{
if (i != 0 && pathway.Waypoints.Count > 1)
if (pathway.Waypoints.Count > 1)
DrawElements(pathway, pathway.Waypoints, i);
using (new Handles.DrawingScope(pathway.LineColor))
for (int i = 1; i < pathway.Waypoints.Count; i++)
{
DrawLabel(pathway, pathway.Waypoints[i].waypoint, i);
Handles.DrawDottedLine(pathway.Waypoints[i - 1].waypoint, pathway.Waypoints[i].waypoint, 2);
}
if (pathway.Waypoints.Count > 2)
Handles.DrawDottedLine(pathway.Waypoints[i - 1], pathway.Waypoints[i], 2);
Handles.DrawDottedLine(pathway.Waypoints[0].waypoint, pathway.Waypoints[pathway.Waypoints.Count - 1].waypoint, 2);
}
}
}
private static void DrawNavMeshPath(Pathway pathway)
{
Handles.color = pathway.LineColor;
for (int i = 0 ; i < pathway.Path.Count - 1; i++)
{
Handles.DrawLine(pathway.Path[i], pathway.Path[i + 1]);
if (i < pathway.Waypoints.Count) {
DrawLabel(pathway, pathway.Waypoints[i].waypoint, i);
}
if (pathway.Waypoints.Count > 2)
private static void DrawHitPoints(Pathway pathway)
{
if (pathway.DisplayProbes)
using (new Handles.DrawingScope(pathway.LineColor))
float sphereRadius = pathway.ProbeRadius;
for (int i = 0; i < pathway.Hits.Count; i++)
Handles.DrawDottedLine(pathway.Waypoints[0], pathway.Waypoints[pathway.Waypoints.Count - 1], 2);
if (pathway.Hits[i])
{
Gizmos.color = new Color(0, 255, 0, 0.5f);
Gizmos.DrawSphere(pathway.Waypoints[i].waypoint, sphereRadius);
}
else
{
Gizmos.color = new Color(255, 0, 0, 0.5f);
Gizmos.DrawSphere(pathway.Waypoints[i].waypoint, sphereRadius);
}
}

2
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayGizmos.cs.meta


fileFormatVersion: 2
guid: baca1f7fba1a3e04aa3968a4cf90f58b
guid: 9c32d0b2ea4c7d549b9bf0a0b6e4ba2e
MonoImporter:
externalObjects: {}
serializedVersion: 2

17
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayHandles.cs


public class PathwayHandles
{
private Pathway _pathway;
private Vector3 _tmp;
public PathwayHandles(Pathway pathway)
{

public void DispalyHandles()
public int DisplayHandles()
EditorGUI.BeginChangeCheck();
_pathway.Waypoints[i] = Handles.PositionHandle(_pathway.Waypoints[i], Quaternion.identity);
EditorGUI.BeginChangeCheck();
_tmp = Handles.PositionHandle(_pathway.Waypoints[i].waypoint, Quaternion.identity);
if (EditorGUI.EndChangeCheck())
{
_pathway.Waypoints[i].waypoint = _tmp;
return i;
}
return -1;
}

40
UOP1_Project/Assets/Scripts/Pathway/Pathway.cs


public class Pathway : MonoBehaviour
{
[HideInInspector]
public List<Vector3> Waypoints;
public Vector3[] waypoints;
public bool HidePathway;
private int _textSize = 10;
private int _textSize = 20;
[SerializeField, Range(0, 100)]
[Tooltip("This function may reduce the frame rate if a large probe radius is specified. To avoid frame rate issues," +
" it is recommended that you specify a max distance of twice the agent height.")]
private float _probeRadius = 3;
[HideInInspector]
public bool DisplayProbes;
[HideInInspector]
public bool ToggledNavMeshDisplay;
private List<Vector3> _path;
private List<bool> _hits;
public const string FIELD_LABEL = "Point ";
public const string TITLE_LABEL = "Waypoints";

public int TextSize { get => _textSize; }
public float ProbeRadius { get => _probeRadius; }
public List<Vector3> Path { get => _path; set => _path = value; }
public List<bool> Hits { get => _hits; set => _hits = value; }
public bool RealTimeEnabled;
[HideInInspector]
public List<WaypointData> Waypoints;
#if UNITY_EDITOR
[System.Serializable]
public class WaypointData
{
public Vector3 waypoint;
public List<Vector3> corners;
}
#endif

105
UOP1_Project/Assets/Scripts/Editor/Pathway/PathWayNavMeshUI.cs


using UnityEngine;
using UnityEditorInternal;
public class PathWayNavMeshUI
{
private Pathway _pathway;
private PathwayNavMesh _pathwayNavMesh;
public PathWayNavMeshUI(Pathway pathway)
{
_pathway = pathway;
_pathwayNavMesh = new PathwayNavMesh(pathway);
RestorePath();
}
public void OnInspectorGUI()
{
if (!_pathway.ToggledNavMeshDisplay)
{
if (GUILayout.Button("NavMesh Path"))
{
_pathway.ToggledNavMeshDisplay = true;
GeneratePath();
InternalEditorUtility.RepaintAllViews();
}
}
else
{
if (GUILayout.Button("Handles Path"))
{
_pathway.ToggledNavMeshDisplay = false;
InternalEditorUtility.RepaintAllViews();
}
}
}
public void UpdatePath() {
if (!_pathway.DisplayProbes)
{
_pathwayNavMesh.UpdatePath();
}
}
public void UpdatePathAt(int index)
{
if (index >= 0)
{
_pathway.DisplayProbes = !_pathwayNavMesh.HasNavMeshAt(index);
if (!_pathway.DisplayProbes && _pathway.ToggledNavMeshDisplay)
{
_pathway.DisplayProbes = !_pathwayNavMesh.UpdateCornersAt(index);
}
}
}
public void RealTime(int index)
{
if (_pathway.RealTimeEnabled)
{
UpdatePathAt(index);
if (_pathway.ToggledNavMeshDisplay)
{
UpdatePath();
}
}
}
private void RestorePath()
{
bool existsPath = true;
_pathway.Hits.Clear();
_pathway.DisplayProbes = false;
if (_pathway.Waypoints.Count > 1)
{
for (int i = 0; i < _pathway.Waypoints.Count; i++)
{
existsPath &= _pathwayNavMesh.HasNavMeshAt(i);
existsPath &= _pathwayNavMesh.UpdateCornersAt(i);
}
if (existsPath)
{
_pathwayNavMesh.UpdatePath();
}
}
_pathway.DisplayProbes = !existsPath;
}
public void GeneratePath()
{
if (_pathway.ToggledNavMeshDisplay)
{
RestorePath();
_pathway.ToggledNavMeshDisplay = !_pathway.DisplayProbes;
}
}
}

11
UOP1_Project/Assets/Scripts/Editor/Pathway/PathWayNavMeshUI.cs.meta


fileFormatVersion: 2
guid: 1114de2a61dc66e489f951c4ed04991f
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:

117
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayNavMesh.cs


using UnityEngine;
using UnityEngine.AI;
using System.Collections.Generic;
using System.Linq;
public class PathwayNavMesh
{
private Pathway _pathway;
public PathwayNavMesh(Pathway pathway)
{
_pathway = pathway;
_pathway.Hits = new List<bool>();
}
public bool HasNavMeshAt(int index)
{
NavMeshHit hit;
bool hasHit = true;
if (_pathway.Waypoints.Count >= _pathway.Hits.Count)
{
hasHit = NavMesh.SamplePosition(_pathway.Waypoints[index].waypoint, out hit, _pathway.ProbeRadius, NavMesh.AllAreas);
if (index > _pathway.Hits.Count - 1)
{
index = _pathway.Hits.Count;
_pathway.Hits.Add(hasHit);
}
else
{
_pathway.Hits[index] = hasHit;
}
if (hasHit)
{
_pathway.Waypoints[index].waypoint = hit.position;
}
}
else
{
_pathway.Hits.RemoveAt(index);
}
return hasHit;
}
private List<Vector3> GetPathCorners(int startIndex, int endIndex)
{
NavMeshPath navMeshPath = new NavMeshPath();
if (NavMesh.CalculatePath(_pathway.Waypoints[startIndex].waypoint, _pathway.Waypoints[endIndex].waypoint, NavMesh.AllAreas, navMeshPath))
{
return navMeshPath.corners.ToList();
}
else
return null;
}
private bool CopyCorners(int startIndex, int endIndex)
{
List<Vector3> result;
if ((result = GetPathCorners(startIndex, endIndex)) != null)
{
_pathway.Waypoints[startIndex].corners = result;
}
return result != null;
}
public bool UpdateCornersAt(int index)
{
bool canUpdate = true;
if (_pathway.Waypoints.Count > 1 && index <_pathway.Waypoints.Count)
{
if (index == 0)
{
canUpdate = CopyCorners(index, index + 1);
canUpdate &= CopyCorners(_pathway.Waypoints.Count - 1, index);
}
else if (index == _pathway.Waypoints.Count - 1)
{
canUpdate = CopyCorners(index - 1, index);
canUpdate &= CopyCorners(index, 0);
}
else
{
canUpdate = CopyCorners(index - 1, index);
canUpdate &= CopyCorners(index, index + 1);
}
}
return canUpdate;
}
public void UpdatePath()
{
if (_pathway.Waypoints.Count > 1)
{
_pathway.Path = _pathway.Waypoints.Aggregate(new List<Vector3>(), (acc, wpd) =>
{
wpd.corners.ForEach(c => acc.Add(c));
return acc;
});
}
else
{
_pathway.Path.Clear();
}
}
}

11
UOP1_Project/Assets/Scripts/Editor/Pathway/PathwayNavMesh.cs.meta


fileFormatVersion: 2
guid: 89ec3dfe690498548a5787eb410bb855
MonoImporter:
externalObjects: {}
serializedVersion: 2
defaultReferences: []
executionOrder: 0
icon: {instanceID: 0}
userData:
assetBundleName:
assetBundleVariant:
正在加载...
取消
保存