- Home /
developing a C# library with visual studio to use with Unity
Hi, I am trying to develop a C# library through visual studio so I can use it inside unity, I have found a few sites that give instructions as to how to create a dll and access it through Unity but I find most instructions online are too vague. can anyone post some instructions on how to do so? I'm trying to do this through visual studio 2017
Answer by michi_b · Nov 21, 2018 at 07:49 PM
First, start the visual studio installer, click "Modify" for Visual Studio 2017 and in the opening window, make sure that ".NET desktop development" in the top right corner is checked. Check it and click "Modify" if it wasn't installed (I think it's checked per default), otherwise just exit.
Next, open Visual Studio and go to File->new->Project. Find the subcategory "Installed/Visual C#/Windows Desktop" on the left side, then choose "Class Library (.NET Framework)". Also make sure that you choose "Framework: .NET Framework 3.5" in the bottom left corner of the window. Name your project anything you want, give it the location you want, hit "OK".
Visual Studio now has already created an empty class "Class1" in the project's root namespace for you. Now you can already hit F7 or click "Build->Build Solution" to create the dll file. Open the file explorer and go to "(the solution folder you chose)/(the class library project name you chose)/bin/Debug/". There should be the dll file named like your project. You can already drop this into an Unity Assets folder. Then you should be able to access (the pretty useless) (your root namespace name).Class1 from the scripts in your Unity project.
These are only the basics, you probably also want to reference the UnityEngine.dll from your class library, to be able to use the Monobehaviour class in that library etc. You can also create debug symbols for Unity to be able to debug into the class library when debugging your Unity scripts. But this is all sufficiently well explained in the Manual I think.
thank you for the well-detailed reply, I shall try this immediately, I need to allow unity to access functions compiled in this dll, is there a particular way to call a function from the dll?
If the containing class and the method you want to call are public, you should be able to call it from your scripts in the Unity project just like any other method that you call from other C# scripts in your Unity project.
just make sure that your classes and methods in your dll are explicitly public, not internal