23
\$\begingroup\$

The title kind of says it all. Is it possible to replace C# with C++ on a game using Unity?

\$\endgroup\$
23
\$\begingroup\$

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->Plugins

For Unity 4 Pro and any Unity 5:

  • Just copy the DLLs into UnityProject->Plugins

Unmanaged means C++ and Managed means C#

\$\endgroup\$
  • 1
    \$\begingroup\$ Has this changed for Unity 5 where the free version contains all of the engine features of the pro version? \$\endgroup\$ – GeekyMonkey Mar 4 '15 at 12:33
  • 1
    \$\begingroup\$ @GeekyMonkey Updated the article and answer. \$\endgroup\$ – MLM Mar 4 '15 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 Jun 24 '15 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 Jul 4 '15 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 Nov 26 '15 at 18:35
1
\$\begingroup\$

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.

\$\endgroup\$
  • \$\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 Feb 22 '16 at 17:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.