Besides the comfortable inbuilt components Follow Waypoints, Linear Path and Unity Pathfinding, one particular strength of our path system is its extendability which allows you to easily combine your custom pathfinding solution and/or your custom path structure with behaviours derived from AIMFollowPath
, like AIMFollowWaypoints
. The following diagram should provide you a basic overview of the system.
Figure 1: Basic class diagram showing the software design of the path system.
The integration can easily be done by inheriting from AIMPathConnector
. This abstract base class is intended for transferring your custom path (way)points to the behaviours derived from AIMFollowPath
. Note that it makes no difference whether the points are calculated by a pathfinding solution or if they come from pre-defined path structures. The data flow is ensured by the method GetPoints
, whereby every behaviour of type AIMFollowPath
can have at least one AIMPathConnector
for retrieving the point data.
Of course, there are a few important aspects which needs to be considered during the implementation depending on your concrete scenario. These are described by the examples in the following two sections. First, how to combine your custom path data with the inbuilt behaviours. Second, how to properly integrate your custom pathfinding solution.
For the use of custom path data, you need to provide a data adapter derived from AIMPathConnector
which implements the protected property points
so that it returns custom points in global coordinates which are of Unity's type Vector3
. Note that your script will automatically be a component due to the fact that AIMPathConnector
inherits from MonoBehaviour
. The points in your custom class needs to be provided in a collection of type IList
. This can be, for example, an ordinary list as well as a pure array. The following code snippet demonstrates an example for such an implementation.
If you want to exchange or modify your path at runtime, there are two other important things which have to be considered. First, GetPoints
returns always a copy of the provided points
. So if you want to avoid the memory allocation, you can use the method GetPointsNonAlloc
. instead. Second, if the path data has been modified or exchanged by your path system, then you have to call the delegate PathChanged
. This delegate can be used to register additional functionality, for example, it notifies Follow Path behaviours that the path has updated.
The integration of a custom pathfinding solution is quite similar to the integration of custom point data. Please, have a look at the given class diagram at the top of this page. As you can see, we have also provided a class especially for custom pathfinding called AIMPathfinding
. There are three major aspects to care about when deriving from this class. First, you have to implement the method CalculatePath
. Within this method, your (asynchronous) pathfinding algorithm needs to be started. Second, you have to call the delegate PathChanged
after the pathfinding process has finished, whereby the Update
method is intended to be used for checking this. The delegate is applied for notifying other components using the path that there was an update. Third, like for custom point data, you have to implement the points
property returning the solution of the pathfinding algorithm. When you do these things, you will come up with a script which should be similar to the following example.