Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use shared C# project in Unity?

I have a problem that's been hunting me for weeks.

I have the following setup :

  • Server (C# project)
  • Shared (Library)
  • Client (Unity project)

Where both the Server and Client use the Shared library.

Right now, I manually build and copy the shared.dll to the Assets folder of the Unity project.

I find it really cumbersome. I'm looking for an automatic way and I did some research and learned that since Unity rebuilds the project file, you can't link a project to it because it will automatically get deleted on rebuild.

My question is : How do I link an external Project to Unity?

like image 943
Alde Avatar asked Oct 19 '25 14:10

Alde


1 Answers

There are two ways that I am aware of.

The first and simplest is to use Microsoft's MSBuildForUnity (which was created after the original post) https://github.com/microsoft/MSBuildForUnity/blob/master/Samples/IntegratedDependencies.Unity/README.md which is build for this very purpose.

The alternative (which was possible at the time of the original post) is to use the undocumented process of creating a subclass of AssetPostprocessor and create the function private static string OnGeneratedSlnSolution(string path, string content) to manually manipulate the sln file that Unity will generate to include your references to your shared library.

This way, each time that Unity recreated the sln file, it will now also include your reference. The down-side is that the content comes in as a String so you may want to parse it through more libraries to make sure that you have a valid result or make your solution very rigid.

like image 67
PaulieTree Avatar answered Oct 21 '25 02:10

PaulieTree