Advertisement
Guest User

Untitled

a guest
Apr 8th, 2018
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 11.81 KB | None | 0 0
  1. commit 90c64824b7ed0d40c32e63628e2b624a3f690d79
  2. Author: Aron Granberg <aron.granberg@gmail.com>
  3. Date:   Tue Mar 6 17:01:59 2018 +0100
  4.  
  5.     It is now possible to use pathfinding from editor scripts.
  6.  
  7. diff --git a/Assets/AstarPathfindingProject/Core/AstarPath.cs b/Assets/AstarPathfindingProject/Core/AstarPath.cs
  8. index f472c36d..07b80291 100644
  9. --- a/Assets/AstarPathfindingProject/Core/AstarPath.cs
  10. +++ b/Assets/AstarPathfindingProject/Core/AstarPath.cs
  11. @@ -573,10 +573,11 @@ public class AstarPath : VersionedMonoBehaviour {
  12.     private ushort nextFreePathID = 1;
  13.  
  14.     private AstarPath () {
  15. +       pathReturnQueue = new PathReturnQueue(this);
  16. +
  17.         // Make sure that the pathProcessor is never null
  18. -       pathProcessor = new PathProcessor(this, pathReturnQueue, 0, true);
  19. +       pathProcessor = new PathProcessor(this, pathReturnQueue, 1, false);
  20.  
  21. -       pathReturnQueue = new PathReturnQueue(this);
  22.         workItems = new WorkItemProcessor(this);
  23.         graphUpdates = new GraphUpdateProcessor(this);
  24.  
  25. @@ -1179,6 +1180,9 @@ public class AstarPath : VersionedMonoBehaviour {
  26.     void InitializePathProcessor () {
  27.         int numThreads = CalculateThreadCount(threadCount);
  28.  
  29. +       // Outside of play mode everything is synchronous, so no threads are used.
  30. +       if (!Application.isPlaying) numThreads = 0;
  31. +
  32.  #if AstarFree
  33.         // Trying to prevent simple modding to add support for more than one thread
  34.         if (numThreads > 1) {
  35. @@ -1734,7 +1738,7 @@ public class AstarPath : VersionedMonoBehaviour {
  36.                         throw new System.Exception("Pathfinding Threads seem to have crashed.");
  37.                     }
  38.  
  39. -                   //Wait for threads to calculate paths
  40. +                   // Wait for threads to calculate paths
  41.                     Thread.Sleep(1);
  42.                     active.PerformBlockingActions(true);
  43.                 }
  44. @@ -1745,7 +1749,7 @@ public class AstarPath : VersionedMonoBehaviour {
  45.                         throw new System.Exception("Critical error. Path Queue is empty but the path state is '" + path.PipelineState + "'");
  46.                     }
  47.  
  48. -                   //Calculate some paths
  49. +                   // Calculate some paths
  50.                     active.pathProcessor.TickNonMultithreaded();
  51.                     active.PerformBlockingActions(true);
  52.                 }
  53. @@ -1835,6 +1839,11 @@ public class AstarPath : VersionedMonoBehaviour {
  54.         } else {
  55.             astar.pathProcessor.queue.Push(path);
  56.         }
  57. +
  58. +       // Outside of play mode, all path requests are synchronous
  59. +       if (!Application.isPlaying) {
  60. +           BlockUntilCalculated(path);
  61. +       }
  62.     }
  63.  
  64.     /** Terminates pathfinding threads when the application quits */
  65. diff --git a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  66. index 2dcbb43f..2c30287b 100644
  67. --- a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  68. +++ b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  69. @@ -237,7 +237,9 @@ namespace Pathfinding {
  70.          * \warning This method should not be called directly. It is used by the GraphNode constructor.
  71.          */
  72.         public void InitializeNode (GraphNode node) {
  73. -           if (!queue.AllReceiversBlocked) {
  74. +           // Note: Outside of play mode everything is synchronous, but the pathfinding coroutine may be not called every frame
  75. +           // and therefore might AllReceiversBlocked not be correctly set.
  76. +           if (!queue.AllReceiversBlocked && Application.isPlaying) {
  77.                 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");
  78.             }
  79.  
  80. diff --git a/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs b/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
  81. index b46507d9..0a08e602 100644
  82. --- a/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
  83. +++ b/Assets/AstarPathfindingProject/Developer/DocExamples/MiscSnippets.cs
  84. @@ -560,6 +560,37 @@ public class MiscSnippets3 {
  85.         }
  86.         /** [AstarPath.ScanAsync1] */
  87.     }
  88. +
  89. +   void EditorMode () {
  90. +       /** [AstarEditorMode.Init] */
  91. +       // Make sure the AstarPath object has been loaded
  92. +       AstarPath.FindAstarPath();
  93. +
  94. +       AstarPath.active.Scan();
  95. +       /** [AstarEditorMode.Init] */
  96. +
  97. +       /** [AstarEditorMode.CheckScanned] */
  98. +       // Check if the first grid graph in the scene has any nodes
  99. +       // if it doesn't then it is not scanned.
  100. +       if (AstarPath.active.data.gridGraph.nodes == null) AstarPath.active.Scan();
  101. +       /** [AstarEditorMode.CheckScanned] */
  102. +
  103. +       Transform transform = null;
  104. +       Transform target = null;
  105. +
  106. +       /** [AstarEditorMode.Search] */
  107. +       ABPath path = ABPath.Construct(transform.position, target.position);
  108. +       AstarPath.StartPath(path);
  109. +       // Everything is synchronous so the path is calculated now
  110. +
  111. +       Debug.Log("Found a path with " + path.vectorPath.Count + " points. The error log says: " + path.errorLog);
  112. +
  113. +       // Draw the path in the scene view
  114. +       for (int i = 0; i < path.vectorPath.Count - 1; i++) {
  115. +           Debug.DrawLine(path.vectorPath[i], path.vectorPath[i+1], Color.red);
  116. +       }
  117. +       /** [AstarEditorMode.Search] */
  118. +   }
  119.  }
  120.  
  121.  public class MiscSnippets5 {
  122. diff --git a/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs
  123. new file mode 100644
  124. index 00000000..005b027e
  125. --- /dev/null
  126. +++ b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs
  127. @@ -0,0 +1,32 @@
  128. +using UnityEngine;
  129. +using System.Collections;
  130. +using Pathfinding;
  131. +
  132. +[ExecuteInEditMode]
  133. +public class EditorPathfinding : MonoBehaviour {
  134. +   public Transform target;
  135. +
  136. +   // Use this for initialization
  137. +   void Start () {
  138. +   }
  139. +
  140. +   // Update is called once per frame
  141. +   void Update () {
  142. +       // Make sure the AstarPath object has been loaded
  143. +       AstarPath.FindAstarPath();
  144. +
  145. +       // Make sure the graph is scanned
  146. +       if (AstarPath.active.astarData.gridGraph.nodes == null) AstarPath.active.Scan();
  147. +
  148. +       ABPath path = ABPath.Construct(transform.position, target.position);
  149. +       AstarPath.StartPath(path);
  150. +       // Everything is synchronous so the path is calculated now
  151. +
  152. +       Debug.Log("Found a path with " + path.vectorPath.Count + " points. The error log says: " + path.errorLog);
  153. +
  154. +       // Draw the path in the scene view
  155. +       for (int i = 0; i < path.vectorPath.Count - 1; i++) {
  156. +           Debug.DrawLine(path.vectorPath[i], path.vectorPath[i+1], Color.red);
  157. +       }
  158. +   }
  159. +}
  160. diff --git a/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta
  161. new file mode 100644
  162. index 00000000..cfc5d7f7
  163. --- /dev/null
  164. +++ b/Assets/AstarPathfindingProject/Developer/TestingScripts/EditorPathfinding.cs.meta
  165. @@ -0,0 +1,12 @@
  166. +fileFormatVersion: 2
  167. +guid: 1645ac9407ce44c83993a78427a5cb4d
  168. +timeCreated: 1520346758
  169. +licenseType: Pro
  170. +MonoImporter:
  171. +  serializedVersion: 2
  172. +  defaultReferences: []
  173. +  executionOrder: 0
  174. +  icon: {instanceID: 0}
  175. +  userData:
  176. +  assetBundleName:
  177. +  assetBundleVariant:
  178. diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
  179. index 6419d8a4..586c7f3e 100644
  180. --- a/Assets/AstarPathfindingProject/changelog.cs
  181. +++ b/Assets/AstarPathfindingProject/changelog.cs
  182. @@ -4,6 +4,7 @@
  183.  - 4.1.14 (2018-03-06)
  184.     - Fixed Pathfinding.GridNode.ClosestPointOnNode being completely broken. Thanks Ivan for reporting this.
  185.         This was used internally in some cases when pathfinding on grid graphs. So this fixes a few cases of strange pathfinding results too.
  186. +   - It is now possible to use pathfinding from editor scripts. See \ref editor-mode.
  187.  
  188.  - 4.1.13 (2018-03-06)
  189.     - Fixed LayerGridGraph.GetNode not performing out of bounds checks.
  190. diff --git a/Documentation/ExtraDocPages/editor_mode.cs b/Documentation/ExtraDocPages/editor_mode.cs
  191. new file mode 100644
  192. index 00000000..763ae35e
  193. --- /dev/null
  194. +++ b/Documentation/ExtraDocPages/editor_mode.cs
  195. @@ -0,0 +1,23 @@
  196. +/** \page editor-mode Pathfinding from editor scripts
  197. +
  198. +This page shows how to get pathfinding working outside of play mode from e.g an editor script.
  199. +
  200. +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
  201. +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.
  202. +
  203. +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.
  204. +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).
  205. +
  206. +\snippet MiscSnippets.cs AstarEditorMode.Init
  207. +
  208. +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.
  209. +A good indicator is if the graph has any nodes.
  210. +
  211. +\snippet MiscSnippets.cs AstarEditorMode.CheckScanned
  212. +
  213. +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.
  214. +\snippet MiscSnippets.cs AstarEditorMode.Search
  215. +
  216. +\see \ref calling-pathfinding
  217. +
  218. +*/
  219. \ No newline at end of file
  220. diff --git a/Documentation/ExtraDocPages/structure.cs b/Documentation/ExtraDocPages/structure.cs
  221. index 4bdfe30a..a3f3786b 100644
  222. --- a/Documentation/ExtraDocPages/structure.cs
  223. +++ b/Documentation/ExtraDocPages/structure.cs
  224. @@ -26,6 +26,7 @@
  225.  - \subpage{extending}
  226.  - \subpage{accessing-data}
  227.  - \subpage{multiple-agent-types}
  228. +- \subpage{editor-mode}
  229.  
  230.  \page misc Misc
  231.  \order{-2}
  232.  
  233. commit 8822c88c5601c0418fa917d076018c7ef9199046
  234. Author: Aron Granberg <aron.granberg@gmail.com>
  235. Date:   Wed Mar 14 11:32:05 2018 +0100
  236.  
  237.     Fixed a bug introduced in 4.1.14 which caused scanning recast graphs in the Unity editor to fail with an error sometimes.
  238.  
  239. diff --git a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  240. index 2c30287b..9327c815 100644
  241. --- a/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  242. +++ b/Assets/AstarPathfindingProject/Core/Misc/PathProcessor.cs
  243. @@ -131,7 +131,7 @@ namespace Pathfinding {
  244.         int Lock (bool block) {
  245.             queue.Block();
  246.  
  247. -           if (block && Application.isPlaying) {
  248. +           if (block) {
  249.                 while (!queue.AllReceiversBlocked) {
  250.                     if (IsUsingMultithreading) {
  251.                         Thread.Sleep(1);
  252. @@ -237,9 +237,7 @@ namespace Pathfinding {
  253.          * \warning This method should not be called directly. It is used by the GraphNode constructor.
  254.          */
  255.         public void InitializeNode (GraphNode node) {
  256. -           // Note: Outside of play mode everything is synchronous, but the pathfinding coroutine may be not called every frame
  257. -           // and therefore might AllReceiversBlocked not be correctly set.
  258. -           if (!queue.AllReceiversBlocked && Application.isPlaying) {
  259. +           if (!queue.AllReceiversBlocked) {
  260.                 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");
  261.             }
  262.  
  263. diff --git a/Assets/AstarPathfindingProject/changelog.cs b/Assets/AstarPathfindingProject/changelog.cs
  264. index 3abb29f6..6ea29722 100644
  265. --- a/Assets/AstarPathfindingProject/changelog.cs
  266. +++ b/Assets/AstarPathfindingProject/changelog.cs
  267. @@ -4,6 +4,7 @@
  268.  - 4.1.15
  269.     - Fixed RichAI.desiredVelocity always being zero. Thanks sukrit1234 for finding the bug.
  270.     - Added some video examples to \link Pathfinding.AIPath.pickNextWaypointDist AIPath.pickNextWaypointDist\endlink.
  271. +   - Fixed a bug introduced in 4.1.14 which caused scanning recast graphs in the Unity editor to fail with an error sometimes.
  272.  
  273.  - 4.1.14 (2018-03-06)
  274.     - Fixed Pathfinding.GridNode.ClosestPointOnNode being completely broken. Thanks Ivan for reporting this.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement