Skip to content

Unity3D Integration

Bruce Markham edited this page Nov 29, 2021 · 3 revisions

Integrate FastGraph to Unity 3D

Overview

FastGraph is built under several targets to offer the maximum features for each of them.

As a consequence of its wide targeting, it is possible to use FastGraph library under the famous Unity 3D game engine.

Here is a video that showcases how to integrate a library that comes from NuGet.org into a Unity project. The example is the one of NewtonSoft.Json but is also applicable to FastGraph.

We have a tiny Unity project that uses FastGraph's predecessor, QuikGraph, and is already setup to use QuikGraph (2.3.0 .NET Standard 2.0)! (It's same techniques should apply to using FastGraph with Unity.) Note that you can also use library that targets .NET Framework rather that .NET Standard.

The sample only consist in a QuikGraph (i.e. FastGraph) dll put in Plugins folder and using it in a script to log edges from a graph constructed on the fly.

Assembly details: 1

Unity overview: 1

Serialization troubleshooting

FastGraph comes with features related to serialization in FastGraph.Serialization assembly. Among those features there is one that is not always supported by Unity which is the GraphML serialization (other serialization methods work). As for now all Unity versions <= 2020.3 are partially supporting it (feel free to ping FastGraph owners to inform about changes related to this).

Unity 3D has a specific C# environment, this has some consequences and some stuff that normally works or is supported in standard C# application may not be usable under Unity.

It will mainly depend on the project setting relative to the C# scripting runtime (Edit > Project Settings > Player > Configuration > API Compatibility Level). By default Unity projects are setup to use the .NET Standard 2.0 compatibility level which in most cases will certainly fit and work.

1

The GraphML serialization is using some code generation at runtime through DynamicMethod API. This is an API that is quite old in the .NET Framework but not in .NET Standard. As a consequence this method of serialization will not work for projects that are setup to use .NET Standard 2.0 compatibility level.

However it is possible to use GraphML serialization but you will have to make changes in your project settings and make it using API compatibility level .NET 4.x (using FastGraph libraries targeting .NET Framework should not be mandatory the .NET Standard 2.0 builds should be enough).

1

If this project setting change is not possible on your end, then you will not be able to fully use the FastGraph.Serialization features. :-(

A quick check you can do to verify if Unity is supporting this kind of stuff is to try compile a C# script that is declaring a DynamicMethod. If compilation is ok then you should be able to use the feature, otherwise not.