- A* version: 4.2.17
- Unity version: 6000.2.0f1
I am using the free version as I wanted to test it first but now I realize it is quite old compared to the pro.
Here is my problem: I am trying to generate a PointGraph and all node and connection from editor script.
I am scene I have a GameObject “AStar” whith a Pathfinder component and no graphs at this time.
The through a custom editor I run some code to create a PointGraph and some nodes and connections like this
private void CreatePointGraph()
{
AstarPath aPath = AstarPath.active;
if (aPath == null)
{
Debug.LogError("Cannot find active AStarPath");
return;
}
AstarData data = aPath.data;
m_PointGraph = data.FindGraphOfType(typeof(PointGraph)) as PointGraph;
if (m_PointGraph == null)
{
m_PointGraph = data.AddGraph(typeof(PointGraph)) as PointGraph;
}
}
Then I create nodes and connections
Nodes is a list of object I already have which creates a graph I have for my procedural world (it is like a road network formed of points) and I want to create a PointGraph from it.
private void PopulatePointGraph()
{
AstarPath aPath = AstarPath.active;
if (aPath == null)
{
Debug.LogError("Cannot find active AStarPath");
return;
}
aPath.AddWorkItem(() =>
{
for (int i = 0; i < Nodes.Length - 1; i++)
{
var node1 = Nodes[i];
var anode1 = graph.AddNode((Int3)node1.Position);
PointNodes[i] = anode1;
var node2 = Nodes[i + 1];
var anode2 = graph.AddNode((Int3)node2.Position);
PointNodes[i + 1] = anode2;
var cost = (uint)Mathf.Round(LengthToNextNode[i]);
anode1.AddConnection(anode2, cost);
anode2.AddConnection(anode1, cost);
}
});
// not sure what is the best way to save/flush/update the graph.
aPath.FlushWorkItems();
aPath.FlushGraphUpdates();
aPath.Scan();
var data = AstarPath.active.data;
data.SetData(data.SerializeGraphs());
EditorUtility.SetDirty(AstarPath.active);
}
And then I am trying to compute a path (not called directly after but when I click on a button)
public void ComputeTestPath()
{
AstarPath aPath = AstarPath.active;
if (aPath == null)
{
Debug.LogError("Cannot find active AStarPath");
return;
}
var start = way.Nodes[0];
var end = way.Nodes[way.Nodes.Length - 1];
var info = AstarPath.active.GetNearest(start.Position);
Debug.Log(info + " " + info.node + " " + info.position);
var path = ABPath.Construct(start.Position, end.Position, (Path p) =>
{
Debug.Log("Path completed: " + p.vectorPath.Count);
foreach(var vp in p.vectorPath)
{
Debug.Log(vp);
}
});
AstarPath.StartPath(path);
}
and neither GetNearest and Construct/StartPath seems to work.
I know I am not trying to make something conventionel but wanted to know if there is a way to do it or not ?
Note that the PointGraph don’t have any root set.
Not sure I am ultra clear, I hope you can help me
Another point: I read this post (Procedural graph generation in edit mode), look a bit similar to me, tryied the solution if found but it does not help me (sorry as a new user I cannot post link)