Advertisement
Guest User

Untitled

a guest
Sep 11th, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 16.89 KB | None | 0 0
  1. diff --git a/Assets/AstarPathfindingProject/Core/AstarPath.cs b/Assets/AstarPathfindingProject/Core/AstarPath.cs
  2. index d54e6cd..55acf19 100644
  3. --- a/Assets/AstarPathfindingProject/Core/AstarPath.cs
  4. +++ b/Assets/AstarPathfindingProject/Core/AstarPath.cs
  5. @@ -685,7 +685,6 @@ public class AstarPath : MonoBehaviour {
  6.    
  7.     /** Calls OnDrawGizmos on graph generators and also #OnDrawGizmosCallback */
  8.     private void OnDrawGizmos () {
  9. -       AstarProfiler.StartProfile ("OnDrawGizmos");
  10.        
  11.         if (active == null) {
  12.             active = this;
  13. @@ -698,6 +697,11 @@ public class AstarPath : MonoBehaviour {
  14.         //If updating graphs, graph info might be corrupt right now
  15.         if (pathQueue != null && pathQueue.AllReceiversBlocked && workItems.Count > 0) return;
  16.  
  17. +       // Don't draw gizmos while scanning since graph info might not be ready yet
  18. +       if (isScanning) return;
  19. +
  20. +       AstarProfiler.StartProfile ("OnDrawGizmos");
  21. +
  22.         if (showNavGraphs && !manualDebugFloorRoof) {
  23.  
  24.             debugFloor = float.PositiveInfinity;
  25. @@ -881,8 +885,13 @@ public class AstarPath : MonoBehaviour {
  26.      * \see CallThreadSafeCallbacks
  27.      */
  28.     private void Update () {
  29. -       PerformBlockingActions();
  30. -      
  31. +
  32. +       // Execute blocking actions such as graph updates
  33. +       // when not scanning
  34. +       if (!isScanning) {
  35. +           PerformBlockingActions();
  36. +       }
  37. +
  38.         //Process paths
  39.         if (threadEnumerator != null) {
  40.             try {
  41. @@ -2004,7 +2013,7 @@ public class AstarPath : MonoBehaviour {
  42.         };
  43.         ScanLoop (info);
  44.  #endif
  45. -       ScanLoop (null);
  46. +       foreach (var p in ScanLoop ()) {}
  47.     }
  48.    
  49.     /** Scans all graphs. This is a IEnumerable, you can loop through it to get the progress
  50. @@ -2013,10 +2022,10 @@ Debug.Log ("Scanning... " + progress.description + " - " + (progress.progress*10
  51.  } \endcode
  52.       * \see Scan
  53.       */
  54. -   public void ScanLoop (OnScanStatus statusCallback) {
  55. +   public IEnumerable<Progress> ScanLoop () {
  56.        
  57.         if (graphs == null) {
  58. -           return;
  59. +           yield break;
  60.         }
  61.        
  62.         isScanning = true;
  63. @@ -2035,7 +2044,7 @@ Debug.Log ("Scanning... " + progress.description + " - " + (progress.progress*10
  64.        
  65.         astarData.UpdateShortcuts ();
  66.  
  67. -       if (statusCallback != null) statusCallback (new Progress (0.05F,"Pre processing graphs"));
  68. +       yield return new Progress (0.05F,"Pre processing graphs");
  69.        
  70.         if (OnPreScan != null) {
  71.             OnPreScan (this);
  72. @@ -2055,34 +2064,33 @@ Debug.Log ("Scanning... " + progress.description + " - " + (progress.progress*10
  73.             }
  74.         }
  75.  
  76. +       // Loop through all graphs and scan them one by one
  77.         for (int i=0;i<graphs.Length;i++) {
  78.            
  79.             NavGraph graph = graphs[i];
  80.            
  81.             if (graph == null) {
  82. -               if (statusCallback != null) statusCallback (new Progress (AstarMath.MapTo (0.05F,0.7F,(float)(i+0.5F)/(graphs.Length+1)),"Skipping graph "+(i+1)+" of "+graphs.Length+" because it is null"));
  83. +               yield return new Progress (AstarMath.MapTo (0.05F,0.7F,(float)(i+0.5F)/(graphs.Length+1)),"Skipping graph "+(i+1)+" of "+graphs.Length+" because it is null");
  84.                 continue;
  85.             }
  86.            
  87.             if (OnGraphPreScan != null) {
  88. -               if (statusCallback != null) statusCallback (new Progress (AstarMath.MapToRange (0.1F,0.7F,(float)(i)/(graphs.Length)),"Scanning graph "+(i+1)+" of "+graphs.Length+" - Pre processing"));
  89. +               yield return new Progress (Mathf.Lerp (0.1F,0.7F,(float)(i)/(graphs.Length)),"Scanning graph "+(i+1)+" of "+graphs.Length+" - Pre processing");
  90.                 OnGraphPreScan (graph);
  91.             }
  92.  
  93. -           float minp = AstarMath.MapToRange (0.1F,0.7F,(float)(i)/(graphs.Length));
  94. -           float maxp = AstarMath.MapToRange (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length));
  95. -          
  96. -           if (statusCallback != null) statusCallback (new Progress (minp,"Scanning graph "+(i+1)+" of "+graphs.Length));
  97. +           // Just used for progress information
  98. +           // This graph will advance the progress bar from minp to maxp
  99. +           float minp = Mathf.Lerp (0.1F,0.7F,(float)(i)/(graphs.Length));
  100. +           float maxp = Mathf.Lerp (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length));
  101. +
  102. +           var progressDescriptionPrefix = "Scanning graph "+(i+1)+" of "+graphs.Length;
  103. +
  104. +           yield return new Progress (minp,progressDescriptionPrefix);
  105.            
  106. -           OnScanStatus info = null;
  107. -           if (statusCallback != null) {
  108. -               info = delegate (Progress p) {
  109. -                   p.progress = AstarMath.MapToRange (minp, maxp, p.progress);
  110. -                   statusCallback (p);
  111. -               };
  112. +           foreach (var p in graph.ScanInternal ()) {
  113. +               yield return new Progress(AstarMath.MapToRange (minp, maxp, p.progress), progressDescriptionPrefix + " - " + p.description);
  114.             }
  115. -          
  116. -           graph.ScanInternal (info);
  117.  
  118.             // Assign the graph index to every node in the graph
  119.             graph.GetNodes (delegate (GraphNode node) {
  120. @@ -2091,12 +2099,12 @@ Debug.Log ("Scanning... " + progress.description + " - " + (progress.progress*10
  121.             });
  122.            
  123.             if (OnGraphPostScan != null) {
  124. -               if (statusCallback != null) statusCallback (new Progress (AstarMath.MapToRange (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length)),"Scanning graph "+(i+1)+" of "+graphs.Length+" - Post processing"));
  125. +               yield return new Progress (AstarMath.MapToRange (0.1F,0.7F,(float)(i+0.95F)/(graphs.Length)),progressDescriptionPrefix + " - Post processing");
  126.                 OnGraphPostScan (graph);
  127.             }
  128.         }
  129.  
  130. -       if (statusCallback != null) statusCallback (new Progress (0.8F,"Post processing graphs"));
  131. +       yield return new Progress (0.8F,"Post processing graphs");
  132.  
  133.         if (OnPostScan != null) {
  134.             OnPostScan (this);
  135. @@ -2109,16 +2117,19 @@ Debug.Log ("Scanning... " + progress.description + " - " + (progress.progress*10
  136.         } catch (System.Exception e) {
  137.             Debug.LogException (e);
  138.         }
  139. -      
  140. -       isScanning = false;
  141.  
  142. -       if (statusCallback != null) statusCallback (new Progress (0.90F,"Computing areas"));
  143. +       yield return new Progress (0.90F,"Computing areas");
  144.  
  145.         FloodFill ();
  146.  
  147.         VerifyIntegrity ();
  148. -      
  149. -       if (statusCallback != null) statusCallback (new Progress (0.95F,"Late post processing"));
  150. +
  151. +       yield return new Progress (0.95F,"Late post processing");
  152. +
  153. +       // Signal that we have stopped scanning here
  154. +       // Note that no yields can happen after this point
  155. +       // since then other parts of the system can start to interfere
  156. +       isScanning = false;
  157.  
  158.         if (OnLatePostScan != null) {
  159.             OnLatePostScan (this);
  160. diff --git a/Assets/AstarPathfindingProject/Core/astarclasses.cs b/Assets/AstarPathfindingProject/Core/astarclasses.cs
  161. index 6d9f3df..5854189 100644
  162. --- a/Assets/AstarPathfindingProject/Core/astarclasses.cs
  163. +++ b/Assets/AstarPathfindingProject/Core/astarclasses.cs
  164. @@ -333,6 +333,10 @@ namespace Pathfinding {
  165.             progress = p;
  166.             description = d;
  167.         }
  168. +
  169. +       public override string ToString () {
  170. +           return progress.ToString("0.0") + " " + description;
  171. +       }
  172.     }
  173.    
  174.     /** Graphs which can be updated during runtime */
  175. diff --git a/Assets/AstarPathfindingProject/Developer/TestingScripts/ScanButton.cs b/Assets/AstarPathfindingProject/Developer/TestingScripts/ScanButton.cs
  176. index 620cffc..1994d47 100644
  177. --- a/Assets/AstarPathfindingProject/Developer/TestingScripts/ScanButton.cs
  178. +++ b/Assets/AstarPathfindingProject/Developer/TestingScripts/ScanButton.cs
  179. @@ -6,7 +6,15 @@ public class ScanButton : MonoBehaviour {
  180.     // Update is called once per frame
  181.     void OnGUI () {
  182.         if (GUI.Button(new Rect (5,5,100,30), "Scan")) {
  183. -           AstarPath.active.Scan ();
  184. +           StartCoroutine (Scan());
  185. +       }
  186. +   }
  187. +
  188. +   IEnumerator Scan () {
  189. +       //AstarPath.active.Scan ();
  190. +       foreach (var p in AstarPath.active.ScanLoop()) {
  191. +           Debug.Log (p);
  192. +           yield return null;
  193.         }
  194.     }
  195.  }
  196. diff --git a/Assets/AstarPathfindingProject/Editor/AstarPathEditor.cs b/Assets/AstarPathfindingProject/Editor/AstarPathEditor.cs
  197. index c8da287..ac54bb3 100644
  198. --- a/Assets/AstarPathfindingProject/Editor/AstarPathEditor.cs
  199. +++ b/Assets/AstarPathfindingProject/Editor/AstarPathEditor.cs
  200. @@ -2372,10 +2372,15 @@ public class AstarPathEditor : Editor {
  201.         UnityEditor.EditorUtility.DisplayProgressBar ("Scanning","Scanning...",0);
  202.        
  203.         try {
  204. -           OnScanStatus info = delegate (Progress p) {
  205. -               UnityEditor.EditorUtility.DisplayProgressBar ("Scanning",p.description,p.progress);
  206. -           };
  207. -           AstarPath.active.ScanLoop (info);
  208. +           var lastMessageTime = float.NegativeInfinity;
  209. +           foreach (var p in AstarPath.active.ScanLoop ()) {
  210. +               // Displaying the progress bar is pretty slow, so don't do it too often
  211. +               if (Time.realtimeSinceStartup - lastMessageTime > 0.2f) {
  212. +                   // Display a progress bar of the scan
  213. +                   UnityEditor.EditorUtility.DisplayProgressBar ("Scanning",p.description,p.progress);
  214. +                   lastMessageTime = Time.realtimeSinceStartup;
  215. +               }
  216. +           }
  217.            
  218.         } catch (System.Exception e) {
  219.             Debug.LogError ("There was an error generating the graphs:\n"+e.ToString ()+"\n\nIf you think this is a bug, please contact me on arongranberg.com (post a comment)\n");
  220. diff --git a/Assets/AstarPathfindingProject/Generators/Base.cs b/Assets/AstarPathfindingProject/Generators/Base.cs
  221. index f9400ee..c48502d 100644
  222. --- a/Assets/AstarPathfindingProject/Generators/Base.cs
  223. +++ b/Assets/AstarPathfindingProject/Generators/Base.cs
  224. @@ -278,8 +278,9 @@ namespace Pathfinding {
  225.             if (AstarPath.OnGraphPreScan != null) {
  226.                 AstarPath.OnGraphPreScan (this);
  227.             }
  228. -          
  229. -           ScanInternal ();
  230. +
  231. +           var scan = ScanInternal ().GetEnumerator ();
  232. +           while (scan.MoveNext()) {}
  233.            
  234.             if (AstarPath.OnGraphPostScan != null) {
  235.                 AstarPath.OnGraphPostScan (this);
  236. @@ -294,18 +295,13 @@ namespace Pathfinding {
  237.         public void Scan () {
  238.             throw new System.Exception ("This method is deprecated. Please use AstarPath.active.Scan or if you really want this.ScanInternal which has the same functionality as this method had.");
  239.         }
  240. -
  241. -       /** Internal method for scanning graphs */
  242. -       public void ScanInternal () {
  243. -           ScanInternal (null);
  244. -       }
  245.        
  246.         /// <summary>
  247.         /// Scans the graph, called from <see cref="AstarPath.ScanInternal"/>
  248.         /// Override this function to implement custom scanning logic
  249.         /// The statusCallback may be optionally called to show progress info in the editor
  250.         /// </summary>
  251. -       public abstract void ScanInternal (OnScanStatus statusCallback);
  252. +       public abstract IEnumerable<Progress> ScanInternal ();
  253.        
  254.         /* Color to use for gizmos.
  255.          * Returns a color to be used for the specified node with the current debug settings (editor only).
  256. diff --git a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  257. index 462706f..d4325fc 100644
  258. --- a/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  259. +++ b/Assets/AstarPathfindingProject/Generators/GridGenerator.cs
  260. @@ -861,13 +861,13 @@ AstarPath.active.Scan();
  261.             neighbourZOffsets[6] =  1;
  262.             neighbourZOffsets[7] = -1;
  263.         }
  264. -      
  265. -       public override void ScanInternal (OnScanStatus statusCallback) {
  266. +
  267. +       public override IEnumerable<Progress> ScanInternal () {
  268.            
  269.             AstarPath.OnPostScan += new OnScanDelegate (OnPostScan);
  270.  
  271.             if (nodeSize <= 0) {
  272. -               return;
  273. +               yield break;
  274.             }
  275.  
  276.             // Make sure the matrix is up to date
  277. @@ -875,7 +875,7 @@ AstarPath.active.Scan();
  278.            
  279.             if (width > 1024 || depth > 1024) {
  280.                 Debug.LogError ("One of the grid's sides is longer than 1024 nodes");
  281. -               return;
  282. +               yield break;
  283.             }
  284.  
  285.  #if !AstarFree
  286. @@ -894,6 +894,8 @@ AstarPath.active.Scan();
  287.             // Set a global reference to this graph so that nodes can find it
  288.             GridNode.SetGridGraph (graphIndex,this);
  289.  
  290. +           yield return new Progress(0.05f, "Creating nodes");
  291. +
  292.             // Create all nodes
  293.             nodes = new GridNode[width*depth];
  294.             for (int i=0;i<nodes.Length;i++) {
  295. @@ -906,12 +908,14 @@ AstarPath.active.Scan();
  296.                 collision = new GraphCollision ();
  297.             }
  298.             collision.Initialize (matrix,nodeSize);
  299. -          
  300. +
  301.  #if !AstarFree
  302.             textureData.Initialize ();
  303.  #endif
  304.            
  305.             for (int z = 0; z < depth; z ++) {
  306. +               if (z % 5 == 0) yield return new Progress(Mathf.Lerp(0.1f,0.7f,z/(float)depth), "Scanning");
  307. +
  308.                 for (int x = 0; x < width; x++) {
  309.                    
  310.                     var node = nodes[z*width+x];
  311. @@ -931,6 +935,8 @@ AstarPath.active.Scan();
  312.            
  313.            
  314.             for (int z = 0; z < depth; z ++) {
  315. +               if (z % 5 == 0) yield return new Progress(Mathf.Lerp(0.7f,0.9f,z/(float)depth), "Scanning");
  316. +
  317.                 for (int x = 0; x < width; x++) {
  318.                
  319.                     var node = nodes[z*width+x];
  320. @@ -940,6 +946,8 @@ AstarPath.active.Scan();
  321.                 }
  322.             }
  323.  
  324. +           yield return new Progress(0.95f, "Calculating erosion");
  325. +
  326.             // Apply erosion
  327.             ErodeWalkableArea ();
  328.         }
  329. diff --git a/Assets/AstarPathfindingProject/Generators/LayerGridGraphGenerator.cs b/Assets/AstarPathfindingProject/Generators/LayerGridGraphGenerator.cs
  330. index 6d0c72f..8209f86 100644
  331. --- a/Assets/AstarPathfindingProject/Generators/LayerGridGraphGenerator.cs
  332. +++ b/Assets/AstarPathfindingProject/Generators/LayerGridGraphGenerator.cs
  333. @@ -331,8 +331,13 @@ namespace Pathfinding {
  334.                 }
  335.             }
  336.         }
  337. -      
  338. -       public override void ScanInternal (OnScanStatus status) {
  339. +
  340. +       public override IEnumerable<Progress> ScanInternal () {
  341. +           ScanInternal ((p) => {});
  342. +           yield break;
  343. +       }
  344. +
  345. +       public void ScanInternal (OnScanStatus status) {
  346.            
  347.             if (nodeSize <= 0) {
  348.                 return;
  349. diff --git a/Assets/AstarPathfindingProject/Generators/NavMeshGenerator.cs b/Assets/AstarPathfindingProject/Generators/NavMeshGenerator.cs
  350. index 7931932..2b20f97 100644
  351. --- a/Assets/AstarPathfindingProject/Generators/NavMeshGenerator.cs
  352. +++ b/Assets/AstarPathfindingProject/Generators/NavMeshGenerator.cs
  353. @@ -739,10 +739,17 @@ and have a low memory footprint because of their smaller size to describe the sa
  354.             }
  355.            
  356.             sourceMesh = mesh;
  357. -           ScanInternal ();
  358. +
  359. +           var scan = ScanInternal ().GetEnumerator ();
  360. +           while (scan.MoveNext()) {}
  361.         }
  362. -      
  363. -       public override void ScanInternal (OnScanStatus statusCallback) {
  364. +
  365. +       public override IEnumerable<Progress> ScanInternal () {
  366. +           ScanInternal ((p) => {});
  367. +           yield break;
  368. +       }
  369. +
  370. +       public void ScanInternal (OnScanStatus statusCallback) {
  371.            
  372.             if (sourceMesh == null) {
  373.                 return;
  374. diff --git a/Assets/AstarPathfindingProject/Generators/PointGenerator.cs b/Assets/AstarPathfindingProject/Generators/PointGenerator.cs
  375. index 265e764..b71885e 100644
  376. --- a/Assets/AstarPathfindingProject/Generators/PointGenerator.cs
  377. +++ b/Assets/AstarPathfindingProject/Generators/PointGenerator.cs
  378. @@ -544,7 +544,12 @@ namespace Pathfinding {
  379.  #endif
  380.         }
  381.  
  382. -       public override void ScanInternal (OnScanStatus statusCallback) {
  383. +       public override IEnumerable<Progress> ScanInternal () {
  384. +           ScanInternal ((p) => {});
  385. +           yield break;
  386. +       }
  387. +
  388. +       public void ScanInternal (OnScanStatus statusCallback) {
  389.  
  390.             if (root == null) {
  391.                 //If there is no root object, try to find nodes with the specified tag instead
  392. diff --git a/Assets/AstarPathfindingProject/Generators/QuadtreeGraph.cs b/Assets/AstarPathfindingProject/Generators/QuadtreeGraph.cs
  393. index 456262d..a7ff4ec 100644
  394. --- a/Assets/AstarPathfindingProject/Generators/QuadtreeGraph.cs
  395. +++ b/Assets/AstarPathfindingProject/Generators/QuadtreeGraph.cs
  396. @@ -46,8 +46,13 @@ namespace Pathfinding {
  397.             }
  398.             return val ? 1 : 0;
  399.         }
  400. -      
  401. -       public override void ScanInternal (OnScanStatus statusCallback)
  402. +
  403. +       public override IEnumerable<Progress> ScanInternal () {
  404. +           ScanInternal ((p) => {});
  405. +           yield break;
  406. +       }
  407. +
  408. +       public void ScanInternal (OnScanStatus statusCallback)
  409.         {
  410.             Width = 1 << editorWidthLog2;
  411.             Height = 1 << editorHeightLog2;
  412. diff --git a/Assets/AstarPathfindingProject/Generators/RecastGenerator.cs b/Assets/AstarPathfindingProject/Generators/RecastGenerator.cs
  413. index 6d9634c..d352759 100644
  414. --- a/Assets/AstarPathfindingProject/Generators/RecastGenerator.cs
  415. +++ b/Assets/AstarPathfindingProject/Generators/RecastGenerator.cs
  416. @@ -1195,8 +1195,13 @@ But this time, edit the setting named "Forward" to "Z forward" (not -Z as it is
  417.         }
  418.        
  419.  #if !PhotonImplementation
  420. -      
  421. -       public override void ScanInternal (OnScanStatus statusCallback) {
  422. +
  423. +       public override IEnumerable<Progress> ScanInternal () {
  424. +           ScanInternal ((p) => {});
  425. +           yield break;
  426. +       }
  427. +
  428. +       public void ScanInternal (OnScanStatus statusCallback) {
  429.             AstarProfiler.Reset ();
  430.             AstarProfiler.StartProfile ("Base Scan");
  431.             //AstarProfiler.InitializeFastProfile (new string[] {"Rasterize", "Rasterize Inner 1", "Rasterize Inner 2", "Rasterize Inner 3"});
  432. diff --git a/Assets/AstarPathfindingProject/Generators/SimplestGraphPossible.cs b/Assets/AstarPathfindingProject/Generators/SimplestGraphPossible.cs
  433. index a932817..a82978c 100644
  434. --- a/Assets/AstarPathfindingProject/Generators/SimplestGraphPossible.cs
  435. +++ b/Assets/AstarPathfindingProject/Generators/SimplestGraphPossible.cs
  436. @@ -11,9 +11,10 @@ using Pathfinding.Serialization;
  437.  //Inherit our new graph from a base graph type
  438.  [JsonOptIn]
  439.  public class SimpleGraph : NavGraph {
  440. -  
  441. -   public override void ScanInternal (OnScanStatus statusCallback) {
  442. +
  443. +   public override IEnumerable<Progress> ScanInternal () {
  444.         //Here we will place our code for scanning the graph
  445. +       yield break;
  446.     }
  447.    
  448.     public override void GetNodes (GraphNodeDelegateCancelable del) {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement