123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
- using UnityEngine;
- #if UNITY_EDITOR
- using UnityEditor;
- #endif
- namespace Pathfinding {
- using Pathfinding.Util;
-
-
-
-
-
-
-
-
- [AddComponentMenu("Pathfinding/Link")]
- [HelpURL("http://arongranberg.com/astar/documentation/stable/class_pathfinding_1_1_node_link.php")]
- public class NodeLink : GraphModifier {
-
- public Transform end;
-
-
-
-
-
- public float costFactor = 1.0f;
-
- public bool oneWay = false;
-
- public bool deleteConnection = false;
- public Transform Start {
- get { return transform; }
- }
- public Transform End {
- get { return end; }
- }
- public override void OnPostScan () {
- if (AstarPath.active.isScanning) {
- InternalOnPostScan();
- } else {
- AstarPath.active.AddWorkItem(new AstarWorkItem(force => {
- InternalOnPostScan();
- return true;
- }));
- }
- }
- public void InternalOnPostScan () {
- Apply();
- }
- public override void OnGraphsPostUpdate () {
- if (!AstarPath.active.isScanning) {
- AstarPath.active.AddWorkItem(new AstarWorkItem(force => {
- InternalOnPostScan();
- return true;
- }));
- }
- }
- public virtual void Apply () {
- if (Start == null || End == null || AstarPath.active == null) return;
- GraphNode startNode = AstarPath.active.GetNearest(Start.position).node;
- GraphNode endNode = AstarPath.active.GetNearest(End.position).node;
- if (startNode == null || endNode == null) return;
- if (deleteConnection) {
- startNode.RemoveConnection(endNode);
- if (!oneWay)
- endNode.RemoveConnection(startNode);
- } else {
- uint cost = (uint)System.Math.Round((startNode.position-endNode.position).costMagnitude*costFactor);
- startNode.AddConnection(endNode, cost);
- if (!oneWay)
- endNode.AddConnection(startNode, cost);
- }
- }
- public void OnDrawGizmos () {
- if (Start == null || End == null) return;
- Draw.Gizmos.Bezier(Start.position, End.position, deleteConnection ? Color.red : Color.green);
- }
- #if UNITY_EDITOR
- [UnityEditor.MenuItem("Edit/Pathfinding/Link Pair %&l")]
- public static void LinkObjects () {
- Transform[] tfs = Selection.transforms;
- if (tfs.Length == 2) {
- LinkObjects(tfs[0], tfs[1], false);
- }
- SceneView.RepaintAll();
- }
- [UnityEditor.MenuItem("Edit/Pathfinding/Unlink Pair %&u")]
- public static void UnlinkObjects () {
- Transform[] tfs = Selection.transforms;
- if (tfs.Length == 2) {
- LinkObjects(tfs[0], tfs[1], true);
- }
- SceneView.RepaintAll();
- }
- [UnityEditor.MenuItem("Edit/Pathfinding/Delete Links on Selected %&b")]
- public static void DeleteLinks () {
- Transform[] tfs = Selection.transforms;
- for (int i = 0; i < tfs.Length; i++) {
- NodeLink[] conns = tfs[i].GetComponents<NodeLink>();
- for (int j = 0; j < conns.Length; j++) DestroyImmediate(conns[j]);
- }
- SceneView.RepaintAll();
- }
- public static void LinkObjects (Transform a, Transform b, bool removeConnection) {
- NodeLink connecting = null;
- NodeLink[] conns = a.GetComponents<NodeLink>();
- for (int i = 0; i < conns.Length; i++) {
- if (conns[i].end == b) {
- connecting = conns[i];
- break;
- }
- }
- conns = b.GetComponents<NodeLink>();
- for (int i = 0; i < conns.Length; i++) {
- if (conns[i].end == a) {
- connecting = conns[i];
- break;
- }
- }
- if (removeConnection) {
- if (connecting != null) DestroyImmediate(connecting);
- } else {
- if (connecting == null) {
- connecting = a.gameObject.AddComponent<NodeLink>();
- connecting.end = b;
- } else {
- connecting.deleteConnection = !connecting.deleteConnection;
- }
- }
- }
- #endif
- }
- }
|