Compiling a C# project as DLL for Unity3d - c#

As far as I know a C# project can be added to Unity3d in 2 forms. As DLL or uncompiled project.
What are the pros and cons of both? Will they work cross platform (Android/iOS/WinPhone)?
To compile as DLL - do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
How do you add a uncompiled project to Unity3d?

Will they work Cross platform(Android/iOS/WinPhone)?
Yes. You can create a library project in Xamarin Studio/MonoDevelop/or Visual Studio. If you do not include platform-specific functions, it will be then cross platform.
To compile as DLL - Do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
See this tutorial.

Well Well Well too many question in a single post. Try to answer one by one:
Unity Offical Docs provide much help:
Question
As far as I know a C# project can be added to Unity3d in 2 forms. As DLL or uncompiled project.
To compile as DLL - do I just use MonoDevelop or Visual Studio to new class library project and select .NET 3.5 and compile?
Usually, scripts are kept in a project as source files and compiled by
Unity whenever the source changes. However, it is also possible to
compile a script to a dynamically linked library (DLL) using an
external compiler. The resulting DLL can then be added to the project
and the classes it contains can be attached to objects just like
normal scripts.
It is generally much easier to work with scripts than DLLs in Unity.
However, you may have access to third party Mono code which is
supplied in the form of a DLL. When developing your own code, you may
be able to use compilers not supported by Unity (F#, for example) by
compiling the code to a DLL and adding it to your Unity project. Also,
you may want to supply Unity code without the source (for an Asset
Store product, say) and a DLL is an easy way to do this.
More
What are the pros and cons
About dll:
Pros
You can build DLLs separately.
It could be faster to re-build one DLL
Cons
Calling code from DLL is slower
It would be slower to re-build hole project with all DLLs
Function names are visible. It is easier to reverse code that uses
dynamic DLLs
You can find more on here, here and specially on Google, our best friend:)
How do you add a uncompiled project to Unity3d?
Ans: What do you mean by uncompiled project? You mean scripts? then it is usually Copy\Paste inside asset folder simply.

When you open your Unity project in an IDE (for example Visual Studio), it is a solution with two projects, one for game scripts and one for editor scripts. You can add more projects to the solution through your IDE of choice. In order for this to work, you need to set projects target framework to one that is compatible with Unity (which uses a subset of Mono), for example Unity 3.5 .net full Base Class Libraries. Unfortunately .NET PCL is not supported.
You can reference your (or third party) library in any of the projects as usual, just remember it also has to be compatible with Unity.
Wether your code will work on all the platforms actually depends on what you use. On platform specific limitations refer to http://docs.unity3d.com/Manual/PlatformSpecific.html.

Related

Add new DLL reference to C++ project inside Visual Studio

I'm going to add a DLL which is made inside C# .net to a C++ project.
According to instructions inside some references like this, I should add its reference as below:
Properties --> Common Properties --> Framework and References -->Add new Reference
In windows which titled Add Reference I should add DLL to my C++ project.
However this windows does not show any DLL files and have no options to browse DLL files.
Does anyone know how can I bring my custom DLL into this window?
P.S. Whole this story is about the requirement that I want to connect SQL Server inside a C++ project and I don't want to use C++ libraries. Instead I want to implement SQL related stuff inside a C# class library project and import it inside C++ project using C# project DLL. If you have any ideas about this I would be grateful if you share your idea or solution.
You cannot call managed (i.e. c# methods) from a native (i.e. c++) project without jumping thru some hoops. You have a few options:
Make your project a c++/cli project, this will allow calling managed code directly. This would probably be the easiest, it should not be much more complex than turning on support for "common language runtime support" in the advanced project properties.
Use a c++/cli adapter project between your native and managed code.
Use some thirdparty software to generate native exported functions by some black magic.
Rewrite your application in c#
For a c++/cli and c# projects you should have a "References" entry in your project that you can use to add assemblies to. I'm not sure if nuget works however, so you might need to managed the references manually.

How do you install a Windows Runtime Component built in C++/CX into a C# project?

I have a library called foo, which is written in C++/CX. I chose a Windows Runtime Component because I want it to be projected into C#, C++ and JavaScript. Also, I want to be able to distribute the library, and I don't want to require/allow the consumer to load my project, along with the source files, in the same solution as their project.
The instructions on MSDN only demonstrate how to include the Windows Runtime Component project in a solution with the consuming C# project. I know there is a way to only distribute the binary, but I don't know how.
This question has been asked a thousand times, but the answer always has the two projects in the same solution, is incomplete or a workaround.
I'm using Visual Studio 2013.4 on Windows 8.1.
One way to do it is to create a VSIX package of your component. See Walkthrough: Creating an SDK using C++ that shows exactly how to do this with a WinRT component consumed by a C# project.
I researched and found the answer... It is not documented well, it is not intuitive, but it's worth the trouble when you see how well a Windows Runtime Component works.
Compiling the C++/CX library:
Make sure you compile the library for all permutations of Debug and Release, in Win32, ARM, x86 and x64.
Instructions for consuming in C#:
Right-click on References in the Solution Explorer, and add a reference to the Microsoft Visual C++ Runtime Package v12.0. Then add a reference to the binary you created with the appropriate configuration for your project (i.e. Debug/ARM). This step is tricky, because the file filter prompts you for the .dll, but you need to set the filter to *.* and select the .winmd file. Then you unload the project, find the <Reference> tag for the library you just added. <Reference> will have a <HintPath> tag below it, and under <Reference> you will also need to add <IsWinMDFile>true</IsWinMDFile> and an <Implementation> tag pair loaded with the name of the .dll that was sitting in the same folder as the .winmd file.
For more detailed instructions and information, I highly recommend visiting Mike Taulty's Blog

How to use wixlibs as file references with support for different platforms?

I just am trying to replicate my distributed C# project structure to WIX setup projects. Now there is the following problem:
LIB: a C# library solution that builds AnyCPU .NET dlls from several C# projects
APP: the dlls from LIB are referenced (as file references) by this main application solution. Additionally there are platform dependent libs included in this application solution, therefore it is important to being able to create setups for the two specific target platforms x86 and x64.
Now I started to create a wixsetup project within the APP solution (which works fine). Then I proceeded with creating a wixlib within the LIB solution that references the LIB .NET dlls into the wixlib.
Now the problem:
The wixlib references the AnyCPU .NET dlls within a DirectoryRef which seems to be platform specificly tagged when creating the wixlib. Therefore I have to go back to the LIB solution, build the project with one platform target, copy the built files (via SVN externals mechanisms) to the APP solution, build this project with the exact same target platform as the wixlib was created with and repeat this procedure for creating the other platform.
It may seem that this is kind of complicated, but doable. Due to the fact that I omitted several other library solutions for which the same problem applies and the fact that all those libraries are used in multiple application solutions and - finally - everything has to run on our build server automatically as well, it is clear that this will not work.
I know of the following solution, though:
Double the .NET dll references within wixlib to assign them to different DirectoryRef INSTALLDIR and INSTALLDIR32 e. g. and to implement those different directory references in the wixsetup.
But this would complicate things as well and is not my preferred solution therefore - if there is an alternative.
If there is no smart alternative, just tell me and I will do things as described in the last paragraph.

How to share code between projects of different types in visual studio?

I know there is someone who has post some questions like this. But my problem is a little different.
My program has two versions, one is for PC and the other one is for Windows Phone. In my case, they both use a same algorithm. I want to share the codes between two projects.
First, I tried to create a project containing these codes, then add them to my projects as a reference. But here is the problem, if i create a Windows Form Application project, I can't reference it in a WP project, and vice versa.
Second, I tried to add these codes to my projects as a link. But I have lots of files to share, I don't want add them one by one. And these shared files will mass my project directory.
What should I do?
You need to create a "Portable class Library" project and put your common code in there. it will create a dll. Reference it in other projects and it will work fine.
Using the Portable Class Library project, you can build portable
assemblies that work without modification on the .NET Framework,
Silverlight, Windows Phone 7, or Xbox 360 platforms. Without the
Portable Class Library project, you must target a single platform and
then manually rework the class library for other platforms. The
Portable Class Library project supports a subset of assemblies from
these platforms, and provides a Visual Studio template that makes it
possible to build assemblies that run without modification on these
platforms. - MSDN
sounds to me like you need to make a Class Library project. Create that and you can put in whatever code you want, then compile it to a dll, and reference it in any of your other projects.

Making a C# project DLL and EXE

I'm working on a project where I need to have both an executable so that the user can run a configuration interface and a DLL that can be embedded in other projects to use some of the other features. Is there a way to make Visual Studio produce both an executable and a DLL (as opposed to switching it manually every time)?
I agree with TJMonk15, but i think this should be explained explicitly. You should have two projects, one project that is a DLL, and one that is a normal project. The DLL project should have all your re-usable code. The normal project should be the application you are building, which will reference your re-usable DLL. This way you can build a framework in the DLL project that can be used for any of your future projects.
A good example of this is when you are making a game. Your game engine would be the DLL, and the game you are making would be the executable project. The executable project will contain all the non-reusable traits such as game GUIs and content.
Why wouldn't you put most of the code in one project (With an ouput of type Library) and then write an executable that referenes the DLL?

Categories