I was wondering if it is possible to create a plugin that works with Unity in VS2010 using C# and I was wondering if anyone can post a example. Thanks in advance.
Yes no problem. all you ahve to do is make it a .NET 3.5, 3.0 or 2.0 project - it defaults to .net 4.x which will not work
Can you please upload an example project as well as the source to a simple plugin. Im assuming your saying I can write the plugin in C# not in C++
Plugins (real ones) are normally developed in C++ as plugins talk to functionality that does not exist in the managed space.
you could do them in C# but if you do them wiht c# you can just do them right inside of unity, no need for a managed assembly as you will end on DLLImport and System.Runtime.InteropServices namespace anyway.
if its just about another .NET assembly (also called plugin but not really one, just additional .net functionality) you can do it in C# inside and outside of unity, its not that doing it in VS gets you any additional functionality with C# its always the same
as for an example: you can just look at any .NET 2.0 project you find that generates a dll.
there are no “real plugins” done with c# to my knowledge, cause there would be no gain over just doing it in unity
also real plugins that access unmanaged libraries, the os etc require pro, independent on if you do them in C++ or C#
Why do you need him to upload anything?
Click “File.”
Click “New Project.”
Select Dynamic Class Library as the template.
Click “Project” → “Properties” and change to .NET Framework 3.5
Build the DLL, there you go champ!
Here’s a over detailed tutorial in how to do it, for future reference. Some settings can throw you off.
This is just one way of doing it, i’m sure there are other (possibly shorter) ways of doing it.
• Open VS 2010
• File → New Project
• Win32 Project (name is something)
• Make sure you change the .net framework to 2, 3, or 3.5 (not 4)
• In Application settings choose, DLL and check Empty Project.
• Create a cpp file call it anything. You can delete the folders, don’t do anything.
• Go To: Project → Properties: Configuration should be all All Configurations
• Project → Properties → Configuration Properties → General: Change Character set to Multi-Byte Character Set.
• Project → Properties → Configuration Properties → General: Whole Program Optimization set to No Whole Program Optimization.
• Project → Properties → Configuration Properties → C/C++ - > Code Generation: Under Run Time Library, change to Multi Threaded dll (/MD)
• Copy paste following in cpp file;
#if _MSC_VER // this is defined when compiling with Visual Studio
#define EXPORT_API __declspec(dllexport) // Visual Studio needs annotating exported functions with this
#else
#define EXPORT_API // XCode does not need annotating exported functions, so define is empty
#endif
// ------------------------------------------------------------------------
// Plugin itself
// Link following functions C-style (required for plugins)
extern “C”
{
//Everything goes in here
const EXPORT_API char* PrintHello()
{
return “Hello\0”;
}
}
• Change Build options to release and Build
• Outputted dll in project folder copy that file into your unity Project under Assets/Plugins (create folder plugins)
• In unity create a new script. Attach it to a game object (Main cam is good for testing)
• In script copy the following;
using UnityEngine;
using System.Runtime.InteropServices; //This gives you access to DLL
public class PluginFBX : MonoBehaviour
{
//DLL stuff
[DllImport (“Test”)] //The string should be the name of the dll file
private static extern string PrintHello();
void Start()
{
Debug.Log(PrintHello());
}
}
• Play in Unity, should print out Hello to the console.
That’s a C++ plugin, not a C# DLL. (And given that it’s not C++/CLR, step 4 is impossible).
C# DLLs for Unity are just class libraries built against the 2.0 CLR (which includes versions 3.0 and 3.5 of the framework) and usually that reference UnityEngine.dll. You’ll find the DLL somewhere inside the ‘Data’ folder adjacent to Unity.exe. Make sure to set ‘Copy Local’ to false.
I was wondering - is is absolutely necessary to use 3.x or 2 to get a DLL to work in Unity? Or to change the runtime library to /MD?
You can use C++ for “Unmanaged” and C# for “Managed” ones. So you can make DLLs with C# more info is here:
http://ericeastwood.com/blog/17/unity-and-dlls-c-managed-and-c-unmanaged
Also here is a brief video of doing so:
I recommend you to read the article first and then watch the video.
You might use managed dlls because most propably your code is in C# (which is a managed language) and you use .Net features. BTW, Managed Language means it has garbage collector or other memory managers or stuff like that.