Is there any way to program in Unity with C++ (solved!)

Hello all,
I am just wondering if you could program in Unity in c++ instead of c#, because I prefer c++ and use it more frequently than c#. I searched it up in the forums and on the internet, it just says a bunch of stuff about plugins and dll files which really confused me. Please help!

Thanks!
Richard

Itā€™s theoretically possible, but it would be a lot more hassle than learning C# instead.

You can write external software that can be used as a ā€˜pluginā€™ for unity. This is useful if say you have some code in C++ that is fast at doing something (like say OpenCV to calculate objects in a picture).

Under right settings, you can compile C++/CLI to a dll usable in monoā€¦ so loadable in unity. But youā€™ll be writing and compiling your code yourself in VisualStudio and then importing it into unity manually. But this isnā€™t technically C++ā€¦ itā€™s a superset of C++ with its own custom libraries and syntax sugar that compiles to CIL which can be ran in a .net/mono runtime (if you configure the compiler to do so). Not sure if youā€™re familiar with this specific dialect of C++.

Honestly thoughā€¦ C# very easy to pick up, especially if you know C++, why not try using it? Like, itā€™s a lot simpler to use than C++ is.

Otherwiseā€¦ thereā€™s other engines that do have direct support for C++ builtin.

3 Likes

Hey guys,
First of all, thanks for the quick reply! Sorry I didnā€™t check this thread for a few days.

I heard that Unity itself is written in C++ and built a C# wrapper for scripting. What does this ā€œwrapperā€ exactly means? Is it just a reference to the existing c++ functions, or is a duplicate or something? Is it possible for me to use the C++ part instead?

The reason that why I donā€™t really want to completely switch to C# is because I am also doing competitive programming using C++. Plus, I feel like that C++'s structure is somewhat more clear and makes more sense to me.

Yeah, I tried to use Unreal but the interface and structure of Unreal really annoys me and I liked Unity much better

Will Unity ever support C++?

They will most likely never support c++. Note, I donā€™t work for Unity, but Iā€™m pretty confident that this is the case.
Right now, the two main languages are c# and UnityScript. And even UnityScript isnā€™t pushed as much as it use to be.

As mentioned, if you want to use Unity, your best bet is to use c#. Itā€™s not like that should change your c++ use for other things.

CPP is supported as plugins. This means you use your normal workflow of writing cpp code to build a dll with the functions you want (so far nothing out of the ordinary here) and access it in Unity through calls from your C# scripts. This should be trivial for someone that already knows cpp. Maybe we misunderstand your cpp experience? Either way, learning C# will certainly not be detrimental to you in any way.

It means Unity is written in cpp. They wrote a C# front end (wrapper) so that developers can access the public cpp functions in C#.

There is no ā€˜switchingā€™ here. Unity affords you the flexibility to use cpp if you want to, you just have to use C# to access it. How much you write in C# is entirely up to you. You could write everything in cpp and just make simple calls from a very basic C# front end in Unity if you wanted to. Itā€™s a lot better than both alternatives - no cpp or no C#.

2 Likes

Wrapper isnā€™t exactly the right term. But itā€™s pretty close. The Unity engine has two parts, an unmanaged part written in C++ and an managed part written on C#.

Itā€™s not possible for you to access the C++ side directly. You need to use the provided C# hooks. However once you have the hooks, you can forward the data on to be processed in any way you like.

Just be aware that every language interface you cross comes at a cost of development time and runtime performance.

2 Likes

Donā€™t get too hung up on the language. One of the most important skills in software development is choosing the right tool for the job. In this case, itā€™s C#.

Learning more languages is always a good idea, too.

3 Likes

Thatā€™s not to say itā€™s impossible though. You just need the right conditions. :stuck_out_tongue:

3004963--224077--suitcase-money-575x323.jpg

9 Likes

Oh unity proā€¦ but i am a broke kidā€¦

Hey guys/gals,
after a brief discussion with some of my friends, Iā€™ve decided to stick to C# and Unity.
Just a question: is there a tutorial/article somewhere about how can I integrate c++ dlls with Unity? I really want to know how to do it because I feel like that itā€™s gonna be handy in the future.

Thanks!

1 Like

Source code license, actually. Itā€™s well beyond Unity Pro.

3 Likes

Google first result of ā€œunity dll callsā€.
https://docs.unity3d.com/Manual/UsingDLL.html

Google is your friend! :slight_smile:

To be fair - that page is for Managed Plugins. The next one down is for Native :slight_smile:
https://docs.unity3d.com/Manual/NativePlugins.html

It seems obvious to me that the Unity team would have directly supported C++ if they could have. The fact they havenā€™t means they canā€™t. I assume its about translating to objective-C, browser script?) and so on.

Unity is written first for experts - transparency is materials, shaders and alpha channels; and beginners can learn that. You get Qs about transparent jpegs because browsers have an easy but limited ā€œcolor X is transparentā€ rule, which Unity wants no part of. Even before Unity came around, game programmers used C++. As a game-making company that switched to engines(?), Unity knows this. Just like they support Max and blender, they would have directly supported the preferred language of game developers if were possible.

I feel for you about C++: Vector is the best array-wrapper ever, pointers to ints are easy and useful, you should be able to declare a class on the heap or stack at will, template functions should be checked only against the types you actually use. The more C# you learn, the more youā€™ll like C++. Even when you find a new C# feature which seems pretty neat, and youā€™re trying to like C#, you later find C++ had it first and their version is better. Arrg. At least itā€™s not Java (C# is based on Java, with some C++ added back in.)

1 Like

The more C# I learn the more I continue to hate C++.

12 Likes

I could not possibly disagree with every single sentence in @Owen-Reynolds 's post any harder than I do. Many of them are opinions on the languages that areā€¦ letā€™s say, warped, from my perspective, and those Iā€™ll just ignore, except to say that nearly every single step of developing in C++ requires 12 source files in just the right folders plus a paragraph of boilerplate code pasted in, and in a practical sense the headaches and bugs that ensue from that process negate any pluses the language might have.

Most of the rest of the post is just factually wrong.

Unity has gotten more expert-friendly as time has gone by, but until around Unity ~3 it was absolutely a beginnersā€™ game engine - albeit one that had some power in it if you knew where to look. If it were an expert-first game engine, for example, GameObject.Find wouldnā€™t even exist, and neither would UnityScript as a language - a language explicitly designed to grab web developers without game dev experience. And even using C#, Unity is far and away the easiest C# development environment Iā€™ve ever used: You create a script and it gives you a small templated script files, and then you fill in some code and alt-tab back to the editor and it compiles it. Itā€™s not only an efficient workflow, itā€™s absolutely trivial for beginners to pick up development.

Unity is NOT a game engine designed for experts; itā€™s been adapted by experts because itā€™s fast and easy (with some power accessible under the hood), and even experts enjoy not having to wrestle with their programs.

Many developers who prefer C++ over C# are also the sort to roll their own game engines, and itā€™s tough to sell a game engine to that person. Thereā€™s a much bigger market if you target people that hate the hurdles that C++ forces you to jump through, and it helps to grow the game developer community as a whole, as well.

I have never seen a question about transparent JPEGs here, but even if I saw one, masking out a color in a shader is absolutely trivial.

OTEE always knew they were going to release a game engine publicly. It seems that early on they intended to also release games built on that engine (not unlike what Epic/Unreal does) and that has fallen by the wayside, but Unity was always a game engine.

5 Likes

Well this saved me a lot of time writing stuffs.

4 Likes

In conclusion:
I decided to continue to use C# with Unity while crossing my fingers and hoping that Unity will support C++ā€¦
Thanks for all the great help guys!
Richard

They can, but they donā€™t want to. There was a serious look at using C++ as a scripting language when Apple was going to prohibit things like Mono on iOS (years ago), but Apple backed off on that idea, so it was dropped.

What? Nothing is ever translated to Objective-C. What itā€™s about, is that Unity doesnā€™t want to.

They wonā€™t. They have zero interest in having anything other than C# as a scripting language.

ā€“Eric

2 Likes