How does Unity use C# on Android?

Asked

Viewed 446 times

5

Recently I started to be interested in game development (initially for the Android platform) and that’s why I met the engine Unity. I’ve never programmed in Java, the native language of Android. However, I know the C language#.

When reading more about the mentioned engine, I verified that it is possible to only use two languages "something derived from Javascript" and C#. Based on this, some doubts arose me:

  • This means that it is possible to generate versions of bytecodes from C# to CLR and JVM?
  • I thought it was only possible to do this through Shaman.
  • What about the SDK issue and access to the system Apis?

1 answer

5


Does this mean that it is possible to generate bytecodes versions of C# for CLR and JVM? I thought it was only possible to do this through Xamarim.

The system of scripting Unity is an implementation that uses Mono in its base, just as Xamarin also uses it. Basically, Mono does everything from CLR, Xamarin and Unity are layers on top that have specific functionality to interact with the operating system, with Unity being more game-specific.

Today Unity has its own version of Runtime and the code is converted to C++ via IL2CPP. Unity as a platform is written almost entirely in C++.

A CIL normal that is then converted. Do not even try to modify this code in C++ that was not made for human consumption.

What about the SDK issue and access to the system Apis?

In general it is Unity’s problem to do this and expose only its API for you to use. It can expose in an identical way to Android, or it can expose more in the style of C#, or it can do something hybrid. I don’t know where they went. The important thing is to know how to use the Unity API.

  • I’m a layman, but one thing I can’t understand is what led Unity to adopt C#. When it comes to game development, we always look for performance. With a language dependent on a VM, we already have a bottleneck. With a non-native language, we’ll have two. But, I do not understand why they did not adopt Java, a language with more support and more mature (in my opinion) and at least on Android we would have a barrier less.

  • 3

    You are correct. Unity does not run only on Android, initially did not run on Android. Java does not run on a lot of platform that they were interested in and C# ran (contrary to popular belief). Maybe they liked C better, which is an easy thing to do. Know how to move and like is a much more adopted and more valid criterion in most cases where one tool is not clearly better than another for a task.

  • If I disassemble an . apk made with Xamarin will I have a CIL (and Mono) code instead of a Java generated file? Obfuscation tools for . net work normally and with the same effectiveness?

  • 1

    More or less, I don’t know if these tools don’t cause problems with Mono and I don’t know if Unity doesn’t change Mono a little. And .apk It’s not just CIL. I don’t know details, but I believe I have all the Mono there, and a shovel of things wrapped around it, plus other things specific to Android.

  • Um... I’m not absolutely sure, but I strongly suspect that Unity uses Mono only to validate syntax and tals (that is, C# is only the language chosen to facilitate the implementation of scripts), and that it manages Java code to produce native APK. More information here: https://docs.unity3d.com/560/Documentation/Manual/android-BuildProcess.html (is the documentation of the previous version, but should not have changed this process).

  • 1

    @Luizvieira I think now is ok, today is different than I knew, and I simplified a little what ended up getting wrong. That’s what I’m looking for.

  • Bacana. The edition was great. : ) If I could give +1 +1. rs

Show 2 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.