How to integrate a C# Project in Unity

Hi all,

i’ve got a Universal Windows Project with a more or less complex data structure. It receives sensor data from a server using a RESTful API.

Now i want to create a unity 3d project to visualize some of the data. I am new to unity and don’t now how to integrate my data structure into unity. Are there any recommendations? Can i import my C# project to unity or do i have to create the classes directly in unity? As far as i can see unity uses C# scripts, but can i create a complex data structure with that?

Thanks in advance!

As always there are many ways to complete a task, I will cover one way (base level approach to how I would do it).

First things first, Soc - Separation of Concerns. While you want to build a Unity app to visualize your data-set, it has nothing to do with the upstream application (ie focus only on the task you want to do with Unity). So “Can i import my C# project to unity”, sorta but don’t that directly, “do i have to create the classes directly in unity” nope you will reuse your existing custom code, “Unity C# features … can i create a complex data structure with that” indeed you can, limitation is that of the target platform your app will run on.

Now to the question, “How to integrate a C# Project in Unity”, again briefly touching on SoC.

Top level solution

  • Build the C# DLL(s) from your existing external project.
  • Create a new Unity project, then create two new folders within the Assets folder, Plugins and Scripts.
  • From the built C# project, copy over at least the DLL file (as well as mdb, meta, pdb files for debugging).
  • Now create a new C# Script in Unity (right click in the Project pane within Scripts), this can just be a sample test file to get you going, nothing fancy.
  • Set Visual Studio as the External Script Editor (you can use Mono if you want), Edit → Preferences → External Tools → External Script Editor.
  • Now double click the C# Script created in Unity above, this will open Visual Studio.
  • Once Visual Studio is open, add a reference to the DLL(s) you copied into the {UnityProject}/Assets/Plugins folder. I have often found that Unity already did this step when opening Visual Studio from Unity, even if the VS Solution was already created by Unity before.
  • You can now use the C# DLL(s) you built externally from Unity.
  • Expand on the idea how ever you want, automate copying over DLL(s), etc

Some talk points:

Unity is designed for multi platform, not all of .NET C# features work on Mono (topic of its own), but if you only target Windows you don’t need to be concerned with this so much, just something to keep in mind.

The external DLL(s) you build and use in your Unity project, should be built with SoC in mind. What I mean is that they should be very task specific, for example one DLL will hold all the Classes for your data “Entity Model” structure, while another one DLL handles calling into your existing REST API to present the data in the Unity App. Don’t built bloated DLL(s) that include task that wont be used by Unity at all, for example the logic for how the data is processed and stored in your external service has nothing to do with Unity and doesnt belong there.

Unrelated to your question, but another question I recently talked on may have some helpful tips for your new project, Source Control with Unity: