The title kind of says it all. Is it possible to replace C# with C++ on a game using Unity?
3 Answers
It is possible to use C++ with the Free version of Unity, although it is easier to work with if you have a Unity Pro license. All you have to do is wrap it up in a DLL and follow the instructions below on where to place it.
I wrote an article that covers this topic: Unity and DLLs: C# (managed) and C++ (unmanaged)
For Unity 4 Free:
- Add unmanaged code to the Unity Project Root:
UnityProject- Add managed code to the Plugins folder:
UnityProject->Plugins- When you build a project, copy the unmanaged code to
BuildRoot->Data->PluginsFor Unity 4 Pro and any Unity 5:
- Just copy the DLLs into
UnityProject->Plugins
Unmanaged means C++ and Managed means C#
-
1\$\begingroup\$ Has this changed for Unity 5 where the free version contains all of the engine features of the pro version? \$\endgroup\$GeekyMonkey– GeekyMonkey2015-03-04 12:33:27 +00:00Commented Mar 4, 2015 at 12:33
-
1\$\begingroup\$ @GeekyMonkey Updated the article and answer. \$\endgroup\$MLM– MLM2015-03-04 18:55:51 +00:00Commented Mar 4, 2015 at 18:55
-
\$\begingroup\$ It makes no sense to try to replace C# with C++ in the Unity3D context. This answer may be helpful if interfacing with native code was asked for. \$\endgroup\$aggsol– aggsol2015-06-24 10:42:17 +00:00Commented Jun 24, 2015 at 10:42
-
1\$\begingroup\$ With the new IL2C++ technology, there aren't many reasons to use C++ instead of C#. Right now, it's limited to mobile platforms but expect the tech to soon be available on PC. \$\endgroup\$JPtheK9– JPtheK92015-07-04 08:03:49 +00:00Commented Jul 4, 2015 at 8:03
-
1\$\begingroup\$ @user2023370 I updated that part to make the language clearer "Unity 4 and below", thanks for the comment :) \$\endgroup\$MLM– MLM2015-11-26 18:35:41 +00:00Commented Nov 26, 2015 at 18:35
It is possible though inconvenient. You'd have to write managed C++ to achieve it. And yes, there is such thing as managed C++. Managed doesn't specifically mean C# and unmanaged C++. To achieve it you'll need to import UnityEngine DLL file. When you're finished you put it in the (Unity Project Name)/Plugins folder. Here would be the code you'd use: In the C++ file:
public ref class CPPUNITY {
public:
void Start() {
Debug::Log("C++ printed message");
}
};
In the C# file:
using UnityEngine;
public class FileName {
void Start() {
CPPUNITY.Start();
}
}
That exact code wouldn't work but thats a base.
-
\$\begingroup\$ Does the mono runtime used in Unity finally support mixed mode assemblies? We haven't had luck some years ago on 4 and assemblies from VS 2012 so this would be really great! \$\endgroup\$Oliver– Oliver2016-02-22 17:01:27 +00:00Commented Feb 22, 2016 at 17:01
Yes, you can use https://github.com/jacksondunstan/UnityNativeScripting
This is basically a code generator which allows you to write C++ code which calls generated C# bindings. Your C++ is then compiled to a binary "Unity plugin" which can be used in your game. Of course, the README of the above repository goes into a lot more detail.