Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

External C# DLL and Unity and UnityVS

Discussion in 'Scripting' started by rickblacker, Oct 8, 2014.

  1. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Hi all... I have an external C# DLL that I created in Visual Studio. It's straight up C#, not a MonoBehavior classes. This DLL was created and compiled in a completely different project than my Unity scripting code base.

    On top of that, I have the UnityVS plugin. When I open up Unity to edit one of the Unity C# scripts, Unity VS opens up for script editing which is what I want.

    When I want to incorporate this external DLL into my Unity project via UnityVS, I do what I would normally do in Visual Studio which is to add a reference to it, making sure that Copy Local is chosen. So far so good.

    However, when I try to compile my Unity scripting in Visual Studio/UnityVS I am getting a compile errors saying that it does not know about the classes from my external DLL.

    What exactly do I need to do, to ensure that my external DLL is seen by Visual Studio/Unity VS project?

    Any help is GREATLY appreciated!!!
    Rick
     
  2. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    You should just drop your DLL into your project folder and let Unity handle adding it and setting up your references. Also, don't compile from within Visual Studio... the compilation will be done in Unity.
     
    Pirs01 likes this.
  3. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Hmmm... Ok but what about needing to debug the Unity scripts? I don't need to debug into the DLL, but I would need to be able to debug at least into the unity scripts.

    Also even before I compile it, I'm getting the blue squiggly lines under where I'm trying to reference the class from the DLL.
     
    Last edited: Oct 8, 2014
  4. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    When using Visual Studio with Unity think of Visual as an interface to Unity rather then standalone IDE. You will always have both running. As said Unity should be doing all the compiling. You use Visual for editing code and debugging but Unity builds and runs the code. Visual talks to Unity to show you debugging info.

    To debug in Visual go to: DEBUG -> Attach Unity Debugger -> pick Unity instance from list.
    Now setup your break points and run your app in Unity instance picked from list.
     
    Dustin-Horne likes this.
  5. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Yes, I understand that you don't run a game from visual studio, rather it's for writing code, and yes, I understand that you still have to attach to Unity to debug. Perhaps I'm not stating the problem correctly...

    I have an external C# DLL.
    I use Visual Studio and UnityVS for editing my scripts
    Unity is configured to launch VS when I want to edit a script
    I want to reference in my C# DLL into my Unity scripts so I double click on a script to launch VS
    I reference in the DLL. Referencing.
    However, even thought i have referenced in the DLL the editor is giving me reference error squigglies.
    Doing a clean on the project has no affect
    Doing a rebuild does not have any affect.
     
  6. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    EDIT:

    I really don't know if you mean to call managed UnityEngine code in your DLL or do you want to call native DLL function in your Unity script or what and what either of those have to do with Unity itself, VS or debugging. I think you need to restate your problem.
     
    Last edited: Oct 8, 2014
  7. Cpt Chuckles

    Cpt Chuckles

    Joined:
    Dec 31, 2012
    Posts:
    86
    why aren't you using emacs?
     
  8. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Pirs01....

    Yes, I know what DLLs are, I've been developing software for 18 years. I'm just rather new to Unity and integrating Visual Studio.

    The problem is, the reference to the DLL is not being resolved and I'm not sure why. Not sure if it's the way Unity setups up a Visual Studio project or maybe UnityVS? Not sure, I just know that when I try to reference in the DLL, then try to put in the "using" statement at the top of the mono script, I get the squiggly lines under the using statement saying:

    "The type or namespace 'xxxxx' could not be found( are you missing a using or directive or an assembly reference?)
    where xxxx is the name of my class in my DLL.
     
  9. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    From top of my had: in "using x" the x should be namespace inside the dll and not the name of class inside dll. Also I would guess that this only works with managed DLL-s and perhaps your DLL is native code?
     
  10. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Yes, you are correct in the namespace, and I should have said that xxxx is the namespace in my dll. I mistyped.

    It's a C# DLL.
     
  11. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Should work then I guess...

    Could you reproduce the problem with minimum code: create new dll with some dummy class and method and try using it and if the result is the same post the code here?
     
  12. rickblacker

    rickblacker

    Joined:
    May 1, 2014
    Posts:
    266
    Well... I'm under a time crunch so I took the code out of the DLL and created a new C# script in unity. I may come back to this later.
     
  13. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    I was curious abut this and tried reproducing on my end. Here's what I did:

    1. Created new project - new solution independent of Unity with C# Class Library template and this source file:
    Code (csharp):
    1. namespace TestDLL
    2. {
    3.   public class TestDLLClass
    4.   {
    5.     public static int Add(int a, int b)
    6.     {
    7.       return a + b;
    8.     }
    9.   }
    10. }
    2. Built
    2.5. EDIT: Droped the DLL file into Unity Projekt using Unity not VS
    3. In my Unity project tried calling the Add method:
    Code (csharp):
    1. using UnityEngine;
    2. using System.Collections;
    3. using TestDLL;
    4.  
    5. public class GameLogic : MonoBehaviour
    6. {
    7.    // Use this for initialization
    8.   private void Start()
    9.   {
    10.     Debug.Log("2+2=" + TestDLLClass.Add(2, 2));
    11.   }
    12. }
    Worked just fine.
     
    Last edited: Oct 8, 2014
    Dustin-Horne likes this.
  14. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    This is what you have to do. Unity actually recreates the project and modifies it based on what is in the project folder which is why adding a reference through Visual Studio will not work. Your assembly and any other dependencies it may have in terms of 3rd party assemblies just need to be dropped in the Unity folder. This is what I said earlier. :)
     
    Pirs01 likes this.
  15. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    A screen shot might help.

    Is your C# DLL referencing any other DLLs that you don't have included in your project? If so, you will need to include those in the project.
     
  16. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    that's not the issue. He's not dropping it in his project folder so it isn't getting referenced properly by the unity build process.
     
  17. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Okay, didn't catch that. You pointed that out before so I assumed he had already done that part and was still having trouble.

    @rickblacker
    Dustin is correct. You are not supposed to be referencing the DLL yourself. Just drop it in your project's Asset folder and Unity will make the reference for you the next time it imports.
     
    Dustin-Horne likes this.
  18. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Assuming you did drop it into a Unity folder and let UnityVS set up the reference, if you're still getting an error when trying to compile in VS, look at the Warnings in the VS status window (not just errors). It's likely that you'll have a Warning saying that your DLL is a .NET 4 DLL and it can not be referenced from the current project. This is becuase UnityVS projects are .NET 3.5. You need to set the Project Type of your DLL to .NET 3.5 instead of the default of .NET 4, and recompile it, then drop it into the project again. I don't know why Visual Studio just says it's a "warning" instead of an "error".... it really should be an "error".
     
  19. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I'd also disagree with Dustin that you "shouldn't" compile in Visual Studio. Your project should always be able to compile in Visual Studio; if it doesn't, then something is wrong that will likely come back and bite you in Unity's Mono side as well even if it doesn't show up as a compile error within Unity. In this situation, if your DLL referenced anything .NET 4-specific, you would get a runtime reflection exception when it hit that part in your game... just because Mono doesn't notice the error when compiling doesn't mean it's actually going to work.
     
  20. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    It might be possible to compile in VS but why. Unity has it's own internal compiler different then the one VS will use by default. Also Unity doesn't use the .NET but rather Mono except it doesn't. It uses it's own custom version of Mono. Then there's a bunch of additional steps performed by Unity internally like for example serializing stuff to update the Editor UI and doing the managed game logic code <-> native engine code mappings etc. None of this stuff is documented anywhere. Why in the world would you create all this trouble for yourself. I don't see a reason why ever do that.
     
    Dustin-Horne likes this.
  21. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    I compile in VS all the time, but only for the sake of checking for errors which are not flagged automatically such as inconsistent accessibility errors or for working through all the warnings that need to be addressed. Unity will recompile it again anyway the next time I click over to it. I've never had an issue doing this and there's no special configuration required. Just press F6.
     
  22. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Yeah, unless you are messing with external .NET DLL's, there is no "trouble", you just click compile and it compiles.
     
  23. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Seams to me it's the opposite: just because it compiles fine in VS doesn't necessarily mean the real thing will work.
    I'm not convinced about benefit of F6 click over left mouse button click or Alt-Tab.
    Good tip. Did a quick test on random project and VS generated some warnings like field not used that do not pup-up in Unity compilation. Probably because it deals with such things when optimizing but still good for keeping source code clean.
     
  24. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    I guess I'm spoiled with Resharper because it helps me find those issues without the need to compile. You can simply just run it's code analysis to find problems solution wide. For a quick error check I suppose a build in Visual Studio is just fine, but it's still not going to check everything. A good example is when creating Windows Phone or Windows Store projects where there are actually two builds that occur in the publish process (one against Mono in the editor and a second against the .NET 4.5 framework).
     
  25. Pirs01

    Pirs01

    Joined:
    Sep 30, 2012
    Posts:
    389
    Will definitely check it out. I use the PHPStorm from JetBrains for web development and it's just way beyond anything else out there. Very happy with it.
     
  26. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Well obviously you will also need to make sure it compiles in Unity, which will happen as soon as you tab back over to it.

    If you are so concerned with working efficiency that an extra half second of compile time is cause for concern, you might need to step away from your project for a while and take a deep breath. ;)
     
  27. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,608
    Sounds like me... :eek: I was going to say pressing F6 lets me see the errors/warnings a lot quicker than having to switch back and forth from Unity, plus I get to step through them in VS instead of double clicking them from Unity's console.

    Alt + Tab is a whole 2x the key presses! Of course, I usually have like 30 windows open at once so that would get confusing for me quickly.
     
  28. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    Again, if you're that concerned about efficiency, use Reshaper. It will save you several seconds of compile time.
     
  29. Dustin-Horne

    Dustin-Horne

    Joined:
    Apr 4, 2013
    Posts:
    4,568
    JetBrains makes top notch tools for sure. Reshaper is a must have if you do any serious .NET development, whether it be Unity or otherwise. It's a little pricey for a Visual Studio addon but worth every penny for sure. It will save you oodles and oodles of time.
     
  30. k0mbain

    k0mbain

    Joined:
    Nov 1, 2012
    Posts:
    22
  31. janjanusz

    janjanusz

    Joined:
    Jun 12, 2017
    Posts:
    2
    The easiest way is just to copy the dll into Unity assets. You can configure VS to accept references dll to in unity from somewhere else, but you would just open the pandora's box, ie. when working with collab or running the project on the different PC. Just copy it to your assets folder and forget about it.