Search Unity

Resolved Referencing the internal custom VS services from Unity

Discussion in 'Scripting' started by AdmirLearn, Dec 23, 2023.

  1. AdmirLearn

    AdmirLearn

    Joined:
    Dec 23, 2023
    Posts:
    5
    Hello everyone,

    First of all, I apologize for this thread because it's not game development related but more application development.

    I'm new to Unity and last few hours I'm trying to find a way to reference my existing .NET MVC web project using Unity and reuse my service methods in Unity. Services that I'm trying to reuse are pure backend services that filter, fetch and create objects from/to my local database which is SQL Server.

    I'm wondering is there way to do it from Unity or am I supposed to make connection to database from Unity and implement them in Unity as well?

    I saw in few threads that there are ways to include .dll files through Assets folder or [DllImport] but in my case I don't have any .dll, but only .cs and .csproj files because it's all locally developed.

    I would appreciate any kind of help!
    Kind regards
     
  2. CodeSmile

    CodeSmile

    Joined:
    Apr 10, 2014
    Posts:
    5,909
    Unity is built around Mono, not Microsoft's .NET Framework - Mono is modelled after .NET Framework but the similarity is only on the API side, in terms of completeness there are differences as well as behaviour in some cases. Unity also adopted the .NET Standard but again, .NET Standard is incompatible with .NET Framework.

    I don't know for certain but I would be surprised if Unity's .NET has native support for MS SQL. The threads that mention copying DLL files likely refer to adding the MS SQL DLLs to your Unity project so that you get to use the classes in those Assemblies. A similar thing is sometimes done to access the Windows.Drawing namespace.

    However, the downside is that as soon as you take a DLL from Microsoft's .NET Framework and put it in your Unity project, that project will only work under Windows (editor and build).

    If that limited set of platforms is not an option you need to adapt the existing code by removing all .NET Framework references that are not supported by Unity (Mono or .NET Standard).
     
    AdmirLearn likes this.
  3. AdmirLearn

    AdmirLearn

    Joined:
    Dec 23, 2023
    Posts:
    5
    Yes, I thought that would be the case but I hoped that I'm wrong and there is some lib or annotation or api that could help us.

    So, if I understood you well, the easiest way and perhaps the only one is to make connection and try to fetch data from the SQL using SQLConnection class. Right?

    Thank you.
     
  4. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    Generally speaking, you don't want your Unity app (or any other client app) to access a database directly, for a lot of reasons, security and practicality being the two most important ones (you don't want your users to install an SQL Server instance just to use your app right?)

    Since your other project is an ASP.NET MVC one, why don't you just expose some web API endpoints that your Unity app can access easily via simple HTTP requests?

    Now if you really have to access your SQL Server database from Unity, I think Mono supports
    System.Data.SqlClient.SqlConnection
    class which allows you to open a connection to a SQL Server database and issue commands/queries, something like this:

    Code (CSharp):
    1. // This is why it's a bad idea to directly access a database from a client
    2. var connectionString = "Server=MyServer; Database=DB; User ID=sa; Password=password;";
    3.  
    4. using (var conn = new System.Data.SqlClient.SqlConnection(connectionString))
    5. {
    6.     conn.Open();
    7.     using (var cmd = conn.CreateCommand())
    8.     {
    9.         var sql = "SELECT * FROM table";
    10.         cmd.CommandText = sql;
    11.  
    12.         using (var reader = cmd.ExecuteReader())
    13.         {
    14.             while (reader.Read())
    15.             {
    16.                 var firstName = (string) reader["FirstName"];
    17.                 var lastName = (string) reader["LastName"];
    18.  
    19.                 Debug.Log($"Name: {firstName} {lastName}");
    20.             }
    21.         }
    22.     }
    23. }
    Alternatively, you need to manually grab DLLs from nuget https://www.nuget.org/packages/Microsoft.Data.SqlClient (nuget packages are just zip files with DLLs inside), copy them to your Unity Assets folder, and use
    Microsoft.Data.SqlClient.SqlConnection
    class to access your DB. This step would also be necessary if you want to create a shared library (DLL) between your MVC and Unity app that does DB access.
     
    AdmirLearn and CodeSmile like this.
  5. AdmirLearn

    AdmirLearn

    Joined:
    Dec 23, 2023
    Posts:
    5
    I consider this database access as a serious flaw of the application of course. That's one the reasons why am I trying to achieve dependency injection in Unity and to reference my existing project. I wasn't sure if there is more elegant solution than the endpoint exposure.

    I will definitely try to make this with endpoint and see what's gonna happen.

    Thank you so much for your help!
     
    Nad_B likes this.
  6. Nad_B

    Nad_B

    Joined:
    Aug 1, 2021
    Posts:
    731
    Exposing a Web API to your website/server IS the elegant solution :)

    Also referencing an external library (assembly/DLL) is not dependency injection. It's just a convenient way to "add more code" to your application without having to manually copy/paste that said code, so the flaw persists, just not anymore in your Unity's app assembly (DLL) but your MVC assembly (DLL), which will also be shipped with your Unity app...
     
    Last edited: Dec 24, 2023
    AdmirLearn likes this.