Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Calling a c# DLL from unity. The up to date definitive answer.

Discussion in 'Editor & General Support' started by punkouter2019, Apr 20, 2014.

  1. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Ive read a couple things and I am confused..

    1. Is it possible in Pro on indie?? I keep reading something about Dlls or plugins only avail in pro

    2. Can I create v4.5 Dlls in c# or 3.5 ?

    3 What is the simplest workflow for getting c# dll callable from Unity? I want to use it to call Azure mobile Services..

    4. Does anything change with unity v5?
     
  2. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    1. Yes on both. Pro is only required for native (C++) dll's.

    2. Only .NET 3.5 or lower.

    3. You just drop the dll in a folder anywhere in your project and call it as if it were any other C# file.

    4. No. Same Mono runtime, same limitations.
     
  3. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    1. Ok. I am only making c# dlls

    2. So do you happen to know how that effects using Azure Mobile Services ? I guess I need to see it that are compatible with v3.5

    3. That sounds. Is it easy to attach the Dll to a debugger so when it is called you can debug through vs2013?

    Another thought.. Why not be able to write code for a c# dll like you normally do in vs2013.. I mean why does the process of creating the c# dll need to be so separate from unity and is there a way in the future to bring this all together?
     
  4. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    I'm not sure if Azure Mobile Services will work, I've never tried it. You can't automatically debug through Visual Studio because your DLL is not running within a regular .NET app; it's being run through Mono within the Unity player. So generally, you can only debug by using the MonoDevelop IDE that's shipped with Unity. You can do all your coding in Visual Studio though. In Unity settings, go to External Apps and change the IDE from MonoDevelop to Visual Studio. Also, there is a product called UnityVS ( http://unityvs.com/ ) that will allow you to debug from Visual Studio.
     
  5. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Yes. I heard of that.. I guess I need to figure out how badly I miss debugging

    So anyone know when mono will be 4.5.1?
     
  6. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    It's a long story, but currently the answer is "never". Xamarin, the company that owns Mono, is demanding lots of money to allow Unity to upgrade to a newer version, and Unity doesn't want to pay, and for the last six years have been telling us that "nobody really needs anything higher than .NET 2". Their current plan is to try to make their own compiler that will convert C# into C++ and try to move everyone away from .NET. Personally I think that plan is utter crap, and I'm sure they will give up on it once they realize how stupid it is, but for now we're stuck.
     
  7. Fubeca

    Fubeca

    Joined:
    Jul 21, 2013
    Posts:
    41
    If nobody needed anything higher than .NET 2, then there wouldn't be a .NET 4.5. :)

    MS and Xamarin got into bed together, and MS wants people using Unity for their unified platform. (Well, they want devs making stuff for their platform, and the best way for that to happen is to have easy to use engines to make stuff with.) Maybe that will help finally come to an arrangement. I want TPL. :)
     
  8. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Well if MS buys Xamarin then it makes sense to want Unity to use 4.5 right?

    In my case I want to mess with Azure mobile services and I think that is using the newer version of .NET I worry.

    Or is it possible that someday Unity will build in functionality to call webservices, REST services , Azure mobile services so I can avoid making the DLL in the first place?
     
  9. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    It's already possible to use the WWW class to call various web services.
     
  10. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Does it make sense to add as part of the build process for a c# DLL to copy it form the.bin to the /asset folder of your unity project? Or is there a better way?

    And where exactly does the c# dll need to be. in ANY folder inside the asset folder??
     
  11. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    looking the WWW class.. I guess the only scenario I would you this if I had high scores would be to get them from a REST HTTP service?.. But then it would be in JSON right? So Id need to parse through that... So it seems just writing a DLL to use mobile services would be a better idea
     
  12. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    .NET assembles can be placed anywhere within the Assets folder. And sure, it's fine to customize your vcproj to spit the DLL out into the Assets folder automatically.

    If you're looking to retrieve high scores from a server, then WWW is fine for that. They don't have to be in JSON - if you're implementing the server then you can make it spit out whatever format you like - though if it is in JSON, there are libraries you can use in Unity to parse that without a problem. For more elaborate stuff I believe Windows Communication Foundation works fine.
     
  13. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Also I wouldn't hold your breath hoping that Microsoft will buy Xamarin. That rumor has been flying around for years, and it's never progressed beyond a rumor.
     
  14. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
  15. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Can I create the DLL project in the same solution as the one that is opened when I double click a script and visual studio opens.? Seems nicer than having two separate solutions ?
     
  16. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    You can, but Unity has an annoying tendency to regenerate that solution from time to time, so be prepared to have to add your project back to it a few times.
     
  17. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    So tell me if this makes more sense.. Create the Class lib using .net 3.5 but also have a unit test project in the solution to test it out... rather than constantly having to

    1. make a change
    2. compile
    3. paste the dll in the Asset folder

    for every little change...

    Also that will give you debugging break points atleast while you are testing the dll..

    this make sense ?
     
  18. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
  19. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Unless there's a very strong reason that you need the DLL to be a DLL all throughout development, I would just put all the C# files in their own folder in Unity and work like that until you're getting towards the end of the project, then take them out and make it a separate DLL.
     
  20. superpig

    superpig

    Drink more water! Unity Technologies

    Joined:
    Jan 16, 2011
    Posts:
    4,613
    Provided that your classlib doesn't have references to UnityEngine.dll or UnityEditor.dll, that should work.

    Though as I said earlier, your step 3 is unnecessary; you can just configure the vcproj to output the DLL into the Assets folder.
     
  21. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Not sure what you mean there.. In my case I am using the DLL to call Azure Mobile services so it is not something I can do with unity..wHAT DO YOU MEAN PUT THEM IN THIER OWN FOLDER IN UNITY? hOW WOULD I BE OPENING THE PROJECT? oops. sorry hit the cap key
     
  22. punkouter2019

    punkouter2019

    Joined:
    Jul 28, 2013
    Posts:
    170
    Right the DLL is just for doing functionality that is not unity related..

    While on the subject I thought of another scenario..

    Since I can Build a WINDOWS PHONE 8 visual studio .sln at any point.. Would it make sense to do that.. And with the .sln unity creates I can start adding to it (A new class lib for the .sln).. And then I assume as I rebuild the WINDOWS PHONE 8 .sln if it does not overwrite my changes then maybe that is a even better workflow ? I hope that made sense.

    though if every time you build a platform specific .sln it erases everything that was there then that won't work