Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

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

Discussion in 'Scripting' started by atwaterlol, Mar 19, 2017.

  1. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    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
     
  2. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    It's theoretically possible, but it would be a lot more hassle than learning C# instead.
     
  3. lordofduct

    lordofduct

    Joined:
    Oct 3, 2011
    Posts:
    8,514
    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.
     
    duckkoder, fenderrex and Ryiah like this.
  4. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    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++?
     
  5. Brathnann

    Brathnann

    Joined:
    Aug 12, 2014
    Posts:
    7,186
    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.
     
  6. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    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#.
     
    figment3d and Ryiah like this.
  7. Kiwasi

    Kiwasi

    Joined:
    Dec 5, 2013
    Posts:
    16,860
    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.
     
    forloopcowboy and Ryiah like this.
  8. Boz0r

    Boz0r

    Joined:
    Feb 27, 2014
    Posts:
    419
    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.
     
    Dave-Carlile, Kiwasi and lordofduct like this.
  9. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,957
    That's not to say it's impossible though. You just need the right conditions. :p

    suitcase-money-575x323.jpg
     
    Velctor, Oyedoyin1, ModLunar and 4 others like this.
  10. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    Oh unity pro...... but i am a broke kid....
     
  11. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    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!
     
    ModLunar likes this.
  12. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    Source code license, actually. It's well beyond Unity Pro.
     
    davidrochin, Ryiah and Kiwasi like this.
  13. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Google first result of "unity dll calls".
    https://docs.unity3d.com/Manual/UsingDLL.html

    Google is your friend! :)
     
  14. KelsoMRK

    KelsoMRK

    Joined:
    Jul 18, 2010
    Posts:
    5,539
  15. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    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.)
     
  16. Dave-Carlile

    Dave-Carlile

    Joined:
    Sep 16, 2012
    Posts:
    967
    The more C# I learn the more I continue to hate C++.
     
  17. StarManta

    StarManta

    Joined:
    Oct 23, 2006
    Posts:
    8,773
    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.
     
  18. LaneFox

    LaneFox

    Joined:
    Jun 29, 2011
    Posts:
    7,462
    Well this saved me a lot of time writing stuffs.
     
  19. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    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
     
  20. Eric5h5

    Eric5h5

    Volunteer Moderator Moderator

    Joined:
    Jul 19, 2006
    Posts:
    32,401
    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
     
    Ryiah and Kiwasi like this.
  21. atwaterlol

    atwaterlol

    Joined:
    Aug 27, 2016
    Posts:
    28
    Well, at least now I am certain of which language should I focus on....
     
  22. makeshiftwings

    makeshiftwings

    Joined:
    May 28, 2011
    Posts:
    3,350
    Just to remind you of lordofduct's post above, you CAN code in any language that .NET supports, and that includes C++/CLI, which basically is C++. If all you care about is using "&" instead of "ref" and other clunky C++ syntax, then it will get you what you want. If you want to use a bunch of native DLL's from other places, it won't help though.
     
  23. gmfy

    gmfy

    Joined:
    Nov 22, 2016
    Posts:
    9
  24. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,957
    Just finished giving the asset a quick glance. Basically the asset simply assists with hooking up your native code plugin to the managed code the engine expects for actual scripting. Nothing the asset provides wasn't already able to be done. It's just saving you some time and effort.

    I'm surprised I haven't seen it mentioned elsewhere considering how long it's been available.
     
    Last edited: Feb 26, 2018
  25. Invertex

    Invertex

    Joined:
    Nov 7, 2013
    Posts:
    1,539
    @atwaterlol What are your actual concerns/dislikes about C# over C++? Many people don't understand that C# is capable of nearly everything C++ is, but makes your life a hell of a lot easier by generating the same generic stuff you'd normally be typing out manually over-and-over-and-over and also having to debug yourself in C++. C# will greatly speed up your workflow if you give it the chance. Are you perhaps holding on to that old adage of "C++ is so much faster performing than C#!!!" that really isn't representative of modern C# compilers and is perpetuated by C++ die-hards? You can deal with pointers, memory copies, bitshifts, etc... in C# if you really want to, but most of the time you don't need to be, you can let C# make your life much easier.

    Here's a recent talk about data-oriented programming with C# for very high performance code you might be interested in.

     
  26. Owen-Reynolds

    Owen-Reynolds

    Joined:
    Feb 15, 2012
    Posts:
    1,992
    If you really know both, C++ is easier to code in than C#. For examples C++'s template system (which C# named generics) is more versatile than generics and the new dynamic keyword combined (you're allowed to use T however you like, including == and function calls, giving compile-time errors when the actual type can't do that.) You can add real variables and functions to a C++ interface. C++ doesn't need C#'s new ref-return function since you can do it directly (int& a1=C.age,) which is a nice time-saver. The "using"'s are more flexible. You don't need a special rule for creating a delegate since Typedef works for everything, including function-types.

    Maybe the biggest thing is no need for java-style reference types in C++. Declare Cat c1; to use it like a struct, or Cat *c1=new Cat(); to use it like a class. Pointers aren't that hard to learn (you already set things to null in C# and have to know what a nullRefError is.)

    But the thing is, you use what you have, and in Unity that's C#, and it's fine. And C# does have a few nice things (if(n=3), array-bounds-checks.) My point here is that "C# is better once you know it" is another way of saying you've only used C#. But you _can_ realistically say a C++ programmer can get used to C# as basically the same thing.