Beispiel #1
0
        internal PathProcessor(AstarPath astar, PathReturnQueue returnQueue, int processors, bool multithreaded)
        {
            this.astar       = astar;
            this.returnQueue = returnQueue;

            if (processors < 0)
            {
                throw new System.ArgumentOutOfRangeException("processors");
            }

            if (!multithreaded && processors != 1)
            {
                throw new System.Exception("Only a single non-multithreaded processor is allowed");
            }

            // Set up path queue with the specified number of receivers
            queue        = new ThreadControlQueue(processors);
            pathHandlers = new PathHandler[processors];

            for (int i = 0; i < processors; i++)
            {
                pathHandlers[i] = new PathHandler(i, processors);
            }

            if (multithreaded)
            {
#if UNITY_2017_3_OR_NEWER
                profilingSampler = CustomSampler.Create("Calculating Path");
#endif

                threads = new Thread[processors];

                // Start lots of threads
                for (int i = 0; i < processors; i++)
                {
                    var pathHandler = pathHandlers[i];
                    threads[i] = new Thread(() => CalculatePathsThreaded(pathHandler));
#if !UNITY_SWITCH || UNITY_EDITOR
                    // Note: Setting the thread name seems to crash when deploying for Switch: https://forum.arongranberg.com/t/path-processor-crashing-nintendo-switch-build/6584
                    threads[i].Name = "Pathfinding Thread " + i;
#endif
                    threads[i].IsBackground = true;
                    threads[i].Start();
                }
            }
            else
            {
                // Start coroutine if not using multithreading
                threadCoroutine = CalculatePaths(pathHandlers[0]);
            }
        }
Beispiel #2
0
        internal PathProcessor(AstarPath astar, PathReturnQueue returnQueue, int processors, bool multithreaded)
        {
            this.astar       = astar;
            this.returnQueue = returnQueue;

            if (processors < 0)
            {
                throw new System.ArgumentOutOfRangeException("processors");
            }

            if (!multithreaded && processors != 1)
            {
                throw new System.Exception("Only a single non-multithreaded processor is allowed");
            }

            // Set up path queue with the specified number of receivers
            queue        = new ThreadControlQueue(processors);
            pathHandlers = new PathHandler[processors];

            for (int i = 0; i < processors; i++)
            {
                pathHandlers[i] = new PathHandler(i, processors);
            }

            if (multithreaded)
            {
#if UNITY_2017_3_OR_NEWER
                profilingSampler = CustomSampler.Create("Calculating Path");
#endif

                threads = new Thread[processors];

                // Start lots of threads
                for (int i = 0; i < processors; i++)
                {
                    var pathHandler = pathHandlers[i];
                    threads[i]              = new Thread(() => CalculatePathsThreaded(pathHandler));
                    threads[i].Name         = "Pathfinding Thread " + i;
                    threads[i].IsBackground = true;
                    threads[i].Start();
                }
            }
            else
            {
                // Start coroutine if not using multithreading
                threadCoroutine = CalculatePaths(pathHandlers[0]);
            }
        }
Beispiel #3
0
        public PathProcessor(AstarPath astar, PathReturnQueue returnQueue, int processors, bool multithreaded)
        {
            this.astar       = astar;
            this.returnQueue = returnQueue;

            if (processors < 0)
            {
                throw new System.ArgumentOutOfRangeException("processors");
            }

            if (!multithreaded && processors != 1)
            {
                throw new System.Exception("Only a single non-multithreaded processor is allowed");
            }

            // Set up path queue with the specified number of receivers
            queue       = new ThreadControlQueue(processors);
            threadInfos = new PathThreadInfo[processors];

            for (int i = 0; i < processors; i++)
            {
                threadInfos[i] = new PathThreadInfo(i, astar, new PathHandler(i, processors));
            }

            if (multithreaded)
            {
                threads = new Thread[processors];

                // Start lots of threads
                for (int i = 0; i < processors; i++)
                {
                    var threadIndex = i;
                    var thread      = new Thread(() => CalculatePathsThreaded(threadInfos[threadIndex]));
                    thread.Name         = "Pathfinding Thread " + i;
                    thread.IsBackground = true;
                    threads[i]          = thread;
                    thread.Start();
                }
            }
            else
            {
                // Start coroutine if not using multithreading
                threadCoroutine = CalculatePaths(threadInfos[0]);
            }
        }
Beispiel #4
0
 public PathProcessor(AstarPath astar, PathReturnQueue returnQueue, int processors, bool multithreaded)
 {
     this.astar       = astar;
     this.returnQueue = returnQueue;
     if (processors < 0)
     {
         throw new ArgumentOutOfRangeException("processors");
     }
     if (!multithreaded && (processors != 1))
     {
         throw new Exception("Only a single non-multithreaded processor is allowed");
     }
     this.queue       = new ThreadControlQueue(processors);
     this.threadInfos = new PathThreadInfo[processors];
     for (int i = 0; i < processors; i++)
     {
         this.threadInfos[i] = new PathThreadInfo(i, astar, new PathHandler(i, processors));
     }
     if (multithreaded)
     {
         this.threads = new Thread[processors];
         for (int j = 0; j < processors; j++)
         {
		public PathProcessor (AstarPath astar, PathReturnQueue returnQueue, int processors, bool multithreaded) {
			this.astar = astar;
			this.returnQueue = returnQueue;

			if (processors < 0) {
				throw new System.ArgumentOutOfRangeException("processors");
			}

			if (!multithreaded && processors != 1) {
				throw new System.Exception("Only a single non-multithreaded processor is allowed");
			}

			// Set up path queue with the specified number of receivers
			queue = new ThreadControlQueue(processors);
			threadInfos = new PathThreadInfo[processors];

			for (int i = 0; i < processors; i++) {
				threadInfos[i] = new PathThreadInfo(i, astar, new PathHandler(i, processors));
			}

			if (multithreaded) {
				threads = new Thread[processors];

				// Start lots of threads
				for (int i = 0; i < processors; i++) {
					var threadIndex = i;
					var thread = new Thread (() => CalculatePathsThreaded(threadInfos[threadIndex]));
					thread.Name = "Pathfinding Thread " + i;
					thread.IsBackground = true;
					threads[i] = thread;
					thread.Start();
				}
			} else {
				// Start coroutine if not using multithreading
				threadCoroutine = CalculatePaths(threadInfos[0]);
			}
		}
Beispiel #6
0
	/** Does simple error checking.
	 */
	public void VerifyIntegrity () {
		
		if (active != this) {
			throw new System.Exception ("Singleton pattern broken. Make sure you only have one AstarPath object in the scene");
		}
		
		if (astarData == null) {
			throw new System.NullReferenceException ("AstarData is null... Astar not set up correctly?");
		}
		
		if (astarData.graphs == null) {
			astarData.graphs = new NavGraph[0];
		}
		
		if (pathQueue == null && !Application.isPlaying) {
			pathQueue = new ThreadControlQueue(0);
		}
		if (threadInfos == null && !Application.isPlaying) {
			threadInfos = new PathThreadInfo[0];
		}
		
		//Dummy if, the getter does some error checking
		if (IsUsingMultithreading) {
		}
	}
Beispiel #7
0
	/** Sets up all needed variables and scans the graphs.
	 * Calls Initialize, starts the ReturnPaths coroutine and scans all graphs.
	 * Also starts threads if using multithreading
	 * \see #OnAwakeSettings */
	public void Awake () {
		//Very important to set this. Ensures the singleton pattern holds
		active = this;
		
		if (FindObjectsOfType (typeof(AstarPath)).Length > 1) {
			Debug.LogError ("You should NOT have more than one AstarPath component in the scene at any time.\n" +
				"This can cause serious errors since the AstarPath component builds around a singleton pattern.");
		}
		
		//Disable GUILayout to gain some performance, it is not used in the OnGUI call
		useGUILayout = false;
		
		isEditor = Application.isEditor;
		
		if (OnAwakeSettings != null) {
			OnAwakeSettings ();
		}
		
		//To make sure all graph modifiers have been enabled before scan (to avoid script run order issues)
		GraphModifier.FindAllModifiers ();
		RelevantGraphSurface.FindAllGraphSurfaces ();
		
		int numThreads = CalculateThreadCount (threadCount);
		
		// Trying to prevent simple modding to add support for more than one thread
		if ( numThreads > 1 ) {
			threadCount = ThreadCount.One;
			numThreads = 1;
		}
		
		threads = new Thread[numThreads];
		//Thread info, will contain at least one item since the coroutine "thread" is thought of as a real thread in this case
		threadInfos = new PathThreadInfo[System.Math.Max(numThreads,1)];
		
		//Set up path queue with the specified number of receivers
		pathQueue = new ThreadControlQueue(threadInfos.Length);
		
		for (int i=0;i<threadInfos.Length;i++) {
			threadInfos[i] = new PathThreadInfo(i,this,new PathHandler());
		}
		for (int i=0;i<threads.Length;i++) {
			threads[i] = new Thread (new ParameterizedThreadStart (CalculatePathsThreaded));
			threads[i].Name = "Pathfinding Thread " + i;
			threads[i].IsBackground = true;
		}
		
		
		//Start coroutine if not using multithreading
		if (numThreads == 0) {
			threadEnumerator = CalculatePaths (threadInfos[0]);
		} else {
			threadEnumerator = null;
		}
		
		//Start pathfinding threads
		for (int i=0;i<threads.Length;i++) {
			if (logPathResults == PathLog.Heavy)
				Debug.Log ("Starting pathfinding thread "+i);
			threads[i].Start (threadInfos[i]);
		}

		Thread graphUpdateThread = new Thread (new ParameterizedThreadStart(ProcessGraphUpdatesAsync));
		graphUpdateThread.IsBackground = true;
		graphUpdateThread.Start (this);
		
		Initialize ();
		
		
		// Flush work items, possibly added in initialize to load graph data
		FlushWorkItems();
		
		if (scanOnStartup) {
			if (!astarData.cacheStartup || astarData.data_cachedStartup == null) {
				Scan ();
			}
		}
		
	}