Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- commit 90c64824b7ed0d40c32e63628e2b624a3f690d79
- Author: Aron Granberg <aron.granberg@gmail.com>
- Date: Tue Mar 6 17:01:59 2018 +0100
- It is now possible to use pathfinding from editor scripts.
- diff --git a/Assets/AstarPathfindingProject/Core/AstarPath.cs b/Assets/AstarPathfindingProject/Core/AstarPath.cs
- index f472c36d..07b80291 100644
- --- a/Assets/AstarPathfindingProject/Core/AstarPath.cs
- +++ b/Assets/AstarPathfindingProject/Core/AstarPath.cs
- @@ -573,10 +573,11 @@ public class AstarPath : VersionedMonoBehaviour {
- private ushort nextFreePathID = 1;
- private AstarPath () {
- + pathReturnQueue = new PathReturnQueue(this);
- +
- // Make sure that the pathProcessor is never null
- - pathProcessor = new PathProcessor(this, pathReturnQueue, 0, true);
- + pathProcessor = new PathProcessor(this, pathReturnQueue, 1, false);
- - pathReturnQueue = new PathReturnQueue(this);
- workItems = new WorkItemProcessor(this);
- graphUpdates = new GraphUpdateProcessor(this);
- @@ -1179,6 +1180,9 @@ public class AstarPath : VersionedMonoBehaviour {
- void InitializePathProcessor () {
- int numThreads = CalculateThreadCount(threadCount);
- + // Outside of play mode everything is synchronous, so no threads are used.
- + if (!Application.isPlaying) numThreads = 0;
- +
- #if AstarFree
- // Trying to prevent simple modding to add support for more than one thread
- if (numThreads > 1) {
- @@ -1734,7 +1738,7 @@ public class AstarPath : VersionedMonoBehaviour {
- throw new System.Exception("Pathfinding Threads seem to have crashed.");
- }
- - //Wait for threads to calculate paths
- + // Wait for threads to calculate paths
- Thread.Sleep(1);
- active.PerformBlockingActions(true);
- }
- @@ -1745,7 +1749,7 @@ public class AstarPath : VersionedMonoBehaviour {
- throw new System.Exception("Critical error. Path Queue is empty but the path state is '" + path.PipelineState + "'");
- }
- - //Calculate some paths
- + // Calculate some paths
- active.pathProcessor.TickNonMultithreaded();
- active.PerformBlockingActions(true);
- }
- @@ -1835,6 +1839,11 @@ public class AstarPath : VersionedMonoBehaviour {
- } else {
- astar.pathProcessor.queue.Push(path);
- }
- +
- + // Outside of play mode, all path requests are synchronous
- + if (!Application.isPlaying) {
- + BlockUntilCalculated(path);
- + }
- }
- /** Terminates pathfinding threads when the application quits */
- diff --git a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- index 2dcbb43f..2c30287b 100644
- --- a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- +++ b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- @@ -237,7 +237,9 @@ namespace Pathfinding {
- * \warning This method should not be called directly. It is used by the GraphNode constructor.
- */
- public void InitializeNode (GraphNode node) {
- - if (!queue.AllReceiversBlocked) {
- + // Note: Outside of play mode everything is synchronous, but the pathfinding coroutine may be not called every frame
- + // and therefore might AllReceiversBlocked not be correctly set.
- + if (!queue.AllReceiversBlocked && Application.isPlaying) {
- throw new System.Exception("Trying to initialize a node when it is not safe to initialize any nodes. Must be done during a graph update. See http://arongranberg.com/astar/docs/graph-updates.php#direct");
- }
- diff --git a/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs b/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
- index b46507d9..0a08e602 100644
- --- a/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
- +++ b/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
- @@ -560,6 +560,37 @@ public class MiscSnippets3 {
- }
- /** [AstarPath.ScanAsync1] */
- }
- +
- + void EditorMode () {
- + /** [AstarEditorMode.Init] */
- + // Make sure the AstarPath object has been loaded
- + AstarPath.FindAstarPath();
- +
- + AstarPath.active.Scan();
- + /** [AstarEditorMode.Init] */
- +
- + /** [AstarEditorMode.CheckScanned] */
- + // Check if the first grid graph in the scene has any nodes
- + // if it doesn't then it is not scanned.
- + if (AstarPath.active.data.gridGraph.nodes == null) AstarPath.active.Scan();
- + /** [AstarEditorMode.CheckScanned] */
- +
- + Transform transform = null;
- + Transform target = null;
- +
- + /** [AstarEditorMode.Search] */
- + ABPath path = ABPath.Construct(transform.position, target.position);
- + AstarPath.StartPath(path);
- + // Everything is synchronous so the path is calculated now
- +
- + Debug.Log("Found a path with " + path.vectorPath.Count + " points. The error log says: " + path.errorLog);
- +
- + // Draw the path in the scene view
- + for (int i = 0; i < path.vectorPath.Count - 1; i++) {
- + Debug.DrawLine(path.vectorPath[i], path.vectorPath[i+1], Color.red);
- + }
- + /** [AstarEditorMode.Search] */
- + }
- }
- public class MiscSnippets5 {
- diff --git a/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs
- new file mode 100644
- index 00000000..005b027e
- --- /dev/null
- +++ b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs
- @@ -0,0 +1,32 @@
- +using UnityEngine;
- +using System.Collections;
- +using Pathfinding;
- +
- +[ExecuteInEditMode]
- +public class EditorPathfinding : MonoBehaviour {
- + public Transform target;
- +
- + // Use this for initialization
- + void Start () {
- + }
- +
- + // Update is called once per frame
- + void Update () {
- + // Make sure the AstarPath object has been loaded
- + AstarPath.FindAstarPath();
- +
- + // Make sure the graph is scanned
- + if (AstarPath.active.astarData.gridGraph.nodes == null) AstarPath.active.Scan();
- +
- + ABPath path = ABPath.Construct(transform.position, target.position);
- + AstarPath.StartPath(path);
- + // Everything is synchronous so the path is calculated now
- +
- + Debug.Log("Found a path with " + path.vectorPath.Count + " points. The error log says: " + path.errorLog);
- +
- + // Draw the path in the scene view
- + for (int i = 0; i < path.vectorPath.Count - 1; i++) {
- + Debug.DrawLine(path.vectorPath[i], path.vectorPath[i+1], Color.red);
- + }
- + }
- +}
- diff --git a/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta
- new file mode 100644
- index 00000000..cfc5d7f7
- --- /dev/null
- +++ b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta
- @@ -0,0 +1,12 @@
- +fileFormatVersion: 2
- +guid: 1645ac9407ce44c83993a78427a5cb4d
- +timeCreated: 1520346758
- +licenseType: Pro
- +MonoImporter:
- + serializedVersion: 2
- + defaultReferences: []
- + executionOrder: 0
- + icon: {instanceID: 0}
- + userData:
- + assetBundleName:
- + assetBundleVariant:
- diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
- index 6419d8a4..586c7f3e 100644
- --- a/Assets/AstarPathfindingProject/changelog.cs
- +++ b/Assets/AstarPathfindingProject/changelog.cs
- @@ -4,6 +4,7 @@
- - 4.1.14 (2018-03-06)
- - Fixed Pathfinding.GridNode.ClosestPointOnNode being completely broken. Thanks Ivan for reporting this.
- This was used internally in some cases when pathfinding on grid graphs. So this fixes a few cases of strange pathfinding results too.
- + - It is now possible to use pathfinding from editor scripts. See \ref editor-mode.
- - 4.1.13 (2018-03-06)
- - Fixed LayerGridGraph.GetNode not performing out of bounds checks.
- diff --git a/Documentation/ExtraDocPages/editor_mode.cs b/Documentation/ExtraDocPages/editor_mode.cs
- new file mode 100644
- index 00000000..763ae35e
- --- /dev/null
- +++ b/Documentation/ExtraDocPages/editor_mode.cs
- @@ -0,0 +1,23 @@
- +/** \page editor-mode Pathfinding from editor scripts
- +
- +This page shows how to get pathfinding working outside of play mode from e.g an editor script.
- +
- +Pathfinding in editor mode, i.e when the game is not running at all in the Unity editor works in the same way as in play mode with the only major difference
- +being that path requests are synchronous, i.e they will be calculated immediately. Normally path requests are asynchronous and may take several frames to calculate.
- +
- +To get it working you must however first initialize the pathfinding system because in editor mode graphs may not have been deserialized and the graphs may not be scanned.
- +The AstarPath.FindAstarPath method will make sure that the AstarPath.active property is set and that all graphs have been deserialized (they are stored as a byte array internally).
- +
- +\snippet MiscSnippets.cs AstarEditorMode.Init
- +
- +If you think a graph might have been scanned, but you are not sure (for example you might run a particular editor script several times) then you can check first.
- +A good indicator is if the graph has any nodes.
- +
- +\snippet MiscSnippets.cs AstarEditorMode.CheckScanned
- +
- +After this you can request paths as usual. The example below uses the AstarPath component directly, but using a Seeker component should work just as well.
- +\snippet MiscSnippets.cs AstarEditorMode.Search
- +
- +\see \ref calling-pathfinding
- +
- +*/
- \ No newline at end of file
- diff --git a/Documentation/ExtraDocPages/structure.cs b/Documentation/ExtraDocPages/structure.cs
- index 4bdfe30a..a3f3786b 100644
- --- a/Documentation/ExtraDocPages/structure.cs
- +++ b/Documentation/ExtraDocPages/structure.cs
- @@ -26,6 +26,7 @@
- - \subpage{extending}
- - \subpage{accessing-data}
- - \subpage{multiple-agent-types}
- +- \subpage{editor-mode}
- \page misc Misc
- \order{-2}
- commit 8822c88c5601c0418fa917d076018c7ef9199046
- Author: Aron Granberg <aron.granberg@gmail.com>
- Date: Wed Mar 14 11:32:05 2018 +0100
- Fixed a bug introduced in 4.1.14 which caused scanning recast graphs in the Unity editor to fail with an error sometimes.
- diff --git a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- index 2c30287b..9327c815 100644
- --- a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- +++ b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
- @@ -131,7 +131,7 @@ namespace Pathfinding {
- int Lock (bool block) {
- queue.Block();
- - if (block && Application.isPlaying) {
- + if (block) {
- while (!queue.AllReceiversBlocked) {
- if (IsUsingMultithreading) {
- Thread.Sleep(1);
- @@ -237,9 +237,7 @@ namespace Pathfinding {
- * \warning This method should not be called directly. It is used by the GraphNode constructor.
- */
- public void InitializeNode (GraphNode node) {
- - // Note: Outside of play mode everything is synchronous, but the pathfinding coroutine may be not called every frame
- - // and therefore might AllReceiversBlocked not be correctly set.
- - if (!queue.AllReceiversBlocked && Application.isPlaying) {
- + if (!queue.AllReceiversBlocked) {
- throw new System.Exception("Trying to initialize a node when it is not safe to initialize any nodes. Must be done during a graph update. See http://arongranberg.com/astar/docs/graph-updates.php#direct");
- }
- diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
- index 3abb29f6..6ea29722 100644
- --- a/Assets/AstarPathfindingProject/changelog.cs
- +++ b/Assets/AstarPathfindingProject/changelog.cs
- @@ -4,6 +4,7 @@
- - 4.1.15
- - Fixed RichAI.desiredVelocity always being zero. Thanks sukrit1234 for finding the bug.
- - Added some video examples to \link Pathfinding.AIPath.pickNextWaypointDist AIPath.pickNextWaypointDist\endlink.
- + - Fixed a bug introduced in 4.1.14 which caused scanning recast graphs in the Unity editor to fail with an error sometimes.
- - 4.1.14 (2018-03-06)
- - Fixed Pathfinding.GridNode.ClosestPointOnNode being completely broken. Thanks Ivan for reporting this.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement