- Home /
How can I detect changes to GraphView, Nodes and Edges?
Hey!
I'm building a dialogue editing system using GraphView, and I'm trying to make it save any changes automatically. To do this I need to detect a bunch of situations, and save in response to them:
Situations I've been able to catch:
- A node has been added (I'm explicitly adding it in code, so that's easy)
- A node has been deleted (Using DetachFromPanelEvent)
Situations I'm unable to catch:
- A node has been moved (dragged).
- An Edge has been added.
- An Edge has been deleted.
Any tips or info on how I could achieve detecting those is greatly appreciated. I really don't want to downgrade to a manual saving system as that risks having a lot of work going unsaved by mistake.
Answer by muckenhoupt · Sep 25, 2020 at 07:54 AM
You've probably figured this out already, but for the sake of other people searching for answers on this forum: The key to all this is the GraphView.graphViewChanged callback. You want to do something like:
MyGraphView() {
graphViewChanged = OnGraphChange;
}
private GraphViewChange OnGraphChange(GraphViewChange change)
{
if (change.edgesToCreate != null) {
foreach (Edge edge in change.edgesToCreate)
{
...
}
}
if (change.elementsToRemove != null)
{
foreach (GraphElement e in change.elementsToRemove)
{
if (e.GetType() == typeof(Edge) {
...
}
}
}
if (change.movedElements != null)
{
foreach (GraphElement e in change.movedElements)
{
if (e.GetType() == typeof(Node))
{
...
}
}
}
return change;
}
I changed my solution to something else, but this will be very helpful in the future. Thank you!
Your answer
Follow this Question
Related Questions
Creating a software for drawing diagrams 4 Answers
Event.current.Use() not consuming event in editor SceneView 1 Answer
Unity VFX Graph won't open after using it as subgraph. 1 Answer
Repurposing the inspector graph module 2 Answers
Method that runs every time unity clears non-serializable properties 0 Answers