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

[Video Tutorial] How to use Visual Studio for all your Unity development

Discussion in 'Scripting' started by fholm, Jan 21, 2012.

  1. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Edit: Clarification, this explains how to create a custom C# Visual Studio project, link it to unity, compile a DLL of it, use the scripts inside the DLL in Unity and how to debug it. I know you can use visual studio as your "unity editor", but this is not the same thing.

    Edit #2: Some people have reported that they have to specify either .NET 3.0 or 3.5 to be able to reference UnityEngine.dll, I'm not sure why this is as you can clearly see in the video that it's possible to link it against 2.0 (could have to do with different unity versions, I'm not sure).

    This is a video tutorial (about 9 minutes) that explains how to use Visual Studio for both coding and building your unity code, it also shows how to debug the custom assembly that visual studio generates from inside Unity. I would advice you to watch it in 720p on YouTube to see all the text, etc. properly.



    Here's the text snippet I paste into the Post-Build Event in visual studio in the video (watch the video and you will understand).

    Code (csharp):
    1.  
    2. echo f | xcopy "$(TargetPath)" "C:\MyProject\MyProject.Unity\Assets\Assemblies\" /Y
    3. echo f | xcopy "$(TargetDir)$(TargetName).pdb" "C:\MyProject\MyProject.Unity\Assets\Assemblies\" /Y
    4. "C:\Program Files (x86)\Unity\Editor\Data\Mono\lib\mono\2.0\pdb2mdb.exe" "C:\MyProject\MyProject.Unity\Assets\Assemblies\$(T  argetFileName)"
    5.  
    A few benefits of using this method

    • You can use compile time constants (defines in the project)
    • You can use an obfuscator on your DLL very easily
    • Unity will not mess with your project files and force you to reload them
    • You get the awesome intellisense of VS2010 :)

    Enjoy!
     
    Last edited: Jan 21, 2012
  2. Marrrk

    Marrrk

    Joined:
    Mar 21, 2011
    Posts:
    1,032
    Thank you! Exactly what I will need in the future. Now I dont need to experiment with it myself :)
     
  3. tmanallen

    tmanallen

    Joined:
    Nov 8, 2009
    Posts:
    395
    Thanks for this is, it is very help.
     
  4. imtrobin

    imtrobin

    Joined:
    Nov 30, 2009
    Posts:
    1,548
    Thanks, are you able to add resource into the assembly dll?
     
  5. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    Hi fholm

    I found this really helpful, but I have a problem when I add

    public IEnumerator SomeMethod()
    {
    // anything
    }

    I get an error indicating that pdb2mdb.exe exited with code -1 (the dll and pdb files are still copied over but not the dll.mdb)

    It does not matter what code is inside the method.

    When I remove the method the project builds and the the mdb file is created in the assemblies folder.

    Any ideas?
     
  6. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,052
    Probably a bug in pdb2mdb.exe that has to do with iterator blocks.
     
  7. George Foot

    George Foot

    Joined:
    Feb 22, 2012
    Posts:
    399
    At work about a year ago we were playing with this, and found that the pdb2mdb which shipped with Unity was broken. We installed the latest Mono and used pdb2mdb from there instead, just to generate the mdb files, and it seemed to work. So you could try that.

    However, that was a while ago, and it looks like Unity is already using a newer version now, as it works out of the box for everything I've tried recently (including IEnumerator functions). YMMV.
     
  8. angrypenguin

    angrypenguin

    Joined:
    Dec 29, 2011
    Posts:
    15,617
    Cheers fholm!
     
  9. lofwyre

    lofwyre

    Joined:
    Apr 19, 2007
    Posts:
    174
    Hi thanks for the input, should I be able to find a reference in the Microsoft.Build.CommonTypes.xsd for IEnumerator? I don't see one. I'm using latest Unity 3.5.

    Thanks again
     
  10. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    Thanks fholm for the tips. I'm kind of using the same method since a while, but even if I got the "official" pdb2mdb I still can't get it to work with IEnumerators :/
     
  11. numberkruncher

    numberkruncher

    Joined:
    Feb 18, 2012
    Posts:
    953
    Thanks for the tutorial! You mentioned that there were several issues with using .NET 3.5 for the target framework.

    Obviously I can understand that errors/exceptions will occur when functionality that is unavailable to the Unity-supported mono version are used. However, are there any other undesirable side effects to this?

    When creating an editor DLL for the asset store, is it better practice to develop using the technique that you describe, but then to build final releases using MonoDevelop?
     
  12. gfoot

    gfoot

    Joined:
    Jan 5, 2011
    Posts:
    550
    There are some compiler differences too - the generated code for automatic events, for example, is different, and I recall having some problems with that in the past. I don't remember whether this was connected with AOT compilation, but it may have been.
     
  13. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    I just want to warn anyone out there who might be thinking of using MonoDevelop to compile DLLs in UnityScript. It does not work correctly through MonoDevelop. Even though it's possible to compile a DLL, you cannot set a compile target and therefore will always get a .Net 4.0 DLL which may cause errors in Unity. See my post here for details. (Note: The particular error I was having has a workaround, but I still don't know whether using .Net 4.0 dlls in Unity is a good idea for other reasons.)

    If you're looking into Unity for the first time and you think you will need advanced features like this, stick with C# and stay away from UnityScript at all costs. Trust me, it will save you so many headaches. Just my 2 cents from 2+ years of coding with US.

    Edit: Well, I came up with a workaround that uses the same internal compiler Unity does and outputs a .NET 2.0 library files without the annoying errors. See this post for details.
     
    Last edited: Aug 25, 2012
  14. ChristofS

    ChristofS

    Joined:
    Jan 22, 2013
    Posts:
    9
    Hi, thanks for your valuable tutorial.

    I have a further question: I'd like to use the debug version of my dlls for development and the release version for deployment (for the reason of performance and size). Is it possible to handle this in unity through different directories? Or do I have to exchange the dlls before I build the final release?.
     
  15. Demigiant

    Demigiant

    Joined:
    Jan 27, 2011
    Posts:
    3,239
    You will have to overwrite the DLLs. You can't keep 2 instances of the same DLL in the same Unity project, because they will conflict with each other.

    An easy way to handle this is to just have your Visual Studio solution compile to your Unity project assets, so when you change from DEBUG to RELEASE, you just have to recompile the solution and it's done.
     
  16. mattbenic

    mattbenic

    Joined:
    Nov 24, 2010
    Posts:
    38
    Thanks @fholm for the walkthrough.

    At one point you mention the issue with Unity hanging onto your dlls. Using xcopy, I'm still struggling with that. In particular with dlls that are loaded dynamically (as opposed to being copied directly into the project). When the postbuild runs, after a run of the game that has used the dll, I get the following:

    Code (csharp):
    1.  
    2. 3> MyLibrary -> C:\Users\Share\git2\DllProjects\MyLibrary\bin\Release\MyLibrary.dll
    3. 3>  C:\Users\Share\git2\DllProjects\MyLibrary\bin\Release\MyLibrary.dll
    4. 3>  File creation error - The requested operation cannot be performed on a file with a user-mapped section open
    5.  
    Have you perhaps run into this and found a workaround?
     
  17. Dave_Voyles

    Dave_Voyles

    Joined:
    Feb 7, 2014
    Posts:
    32
    If you are looking for Visual Studio, shoot me an email.

    I'm an evangelist at Microsoft, working with startups and indie developers, so I can likely set you up with a copy of Visual Studio through our BizSpark program. We've also got tons of tutorials and content for Unity devs too. Here's one I did on using Prime[31]'s Azure Leaderboards.

    Here's some info on BizSpark.

    Perks of the program:

    • http://wootstudio.ca/startups/bizspark.aspx
    • Windows 8 / Windows Phone developer accounts
    • Visual Studio Ultimate 2013
    • Windows 8
    • Office 365
    • $150 / month Azure credits
    • Website hosting, virtual machines, databases, and mobile services
    • 90 Days of pluralsight + DigitalTutors training (Awesome for Unity)
    • Free marketing support with Microsoft
    • UnityVS, for debugging Unity games within Visual Studio
     
    guavaman likes this.
  18. guavaman

    guavaman

    Joined:
    Nov 20, 2009
    Posts:
    5,609
    +1. I got into BizSpark last year and it's been great. There's no reason to be using MonoDevelop anymore. And now UnityVS is free too.