How does C# in Unity compare with using regular C# .Net for Windows or ASP development?

hi i'm currently doing my MCPD in C# and kind of struggling to come to terms with C# in Unity3D. is there someone that can assist me as i'm not sure where to start.

i think i may be to used to .NET and not pure C#.

please help

thanx

The C# in unity is regular C#, just like you'd learn when studying for your MCPD. If you're used to .Net, you'll be right at home going straight into C# in Unity. Of course, the more specific areas of .Net relating to Windows Forms & ASP applications don't really carry through to Unity, but all the general-purpose programming techniques you'll learn are very relevant and useful.

The only significant differences are:

  • While you can use Visual Studio for editing and scanning for compile-time errors, you have to actually build & run in the Unity IDE.

  • Unity is built on Mono which is an open-source implementation of .Net, so there are slight differences relating to that.

  • The current version of Unity (v2.6) uses Mono 1.2.5 (which is on par with .NET 2.0)

  • Unity doesn't currently have a debugger, although it does have a sort of pause-and-inspect mode. This means you often have to resort to tracing out statements to the console in Unity's editor using Debug.Log(). This situation is being improved - apparently we'll have real debugging in the recently announced version 3 of Unity. Yay!

  • Unity isn't thread-safe, so you can't use multithreaded code to directly interact with Unity objects.

So, when using C# in Unity, you have access to almost the whole .Net API (as supported by the version of Mono in Unity) - Which includes many things that people are sometimes surprised are available, such as FileIO, Collections, Delegates, Try/Catch, Sockets, StringBuilder, etc.

In addition you have access to the Unity Scripting API which you use for manipulating Unity-specific objects such as GameObjects, Components, Vectors, Quaternions, Physics Objects, Animations, as well as for other Unity-specific tasks like reading input from the Mouse, Keyboard and other controllers.

Now, something which may throw you as a beginner is that most of the examples in Unity's scripting documentation are in Unity's Javascript. UT are working on rectifying this, but in the meantime you should know that it's actually trivial to convert most Unity JS scripts to C#. Have a look in the Unity Wiki for C# scripts for an idea of what they're supposed to look like, and you'll often find pages where the same script is provided in both JS and C#.

It may also be of interest to note that pretty much all of the above also applies if you're using Unity's Javascript to code, rather than C#. You still get access to all the .Net classes - the only exception being those which the Javascript syntax itself doesn't support (such as generics).