31
\$\begingroup\$

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

\$\endgroup\$
1

3 Answers 3

27
\$\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\$
6
  • 1
    \$\begingroup\$ Has this changed for Unity 5 where the free version contains all of the engine features of the pro version? \$\endgroup\$ Commented Mar 4, 2015 at 12:33
  • 1
    \$\begingroup\$ @GeekyMonkey Updated the article and answer. \$\endgroup\$ Commented 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\$ Commented 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\$ Commented 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\$ Commented Nov 26, 2015 at 18:35
3
\$\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\$
1
  • \$\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\$ Commented Feb 22, 2016 at 17:01
3
\$\begingroup\$

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.

\$\endgroup\$

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.