Before updating to Windows 10 you may need to return your Pro license. More information here

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


more ▼

asked Mar 16, 2010 at 01:39 PM

avatar image

50 20 21 21

(comments are locked)
10|3000 characters needed characters left

1 answer: sort voted first

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).

more ▼

answered Mar 16, 2010 at 02:22 PM

avatar image

duck ♦♦
51.5k 147 418 546

thanks for replying Duck, the prob is i wanted to read in file using filestream but had to use filinfo instead. can you please mail me some examples of using C#?

Mar 18, 2010 at 06:32 AM Ruben

FileStream should work just fine in Unity (except if you're deploying to webplayer, for security reasons) What problem are you having?

Mar 18, 2010 at 10:20 AM duck ♦♦

Here's an example using FileStream to load images taken from the iPhone's camera into Textures in unity! http://forum.unity3d.com/viewtopic.php?t=29129

Mar 18, 2010 at 10:23 AM duck ♦♦

depends what you want to read and write. For small amounts of data saved and loaded locally, there's PlayerPrefs: http://answers.unity3d.com/questions/3218 otherwise you generally save and load by sending and receiving information via a server: http://answers.unity3d.com/questions/3560

Mar 18, 2010 at 10:46 AM duck ♦♦

i used this : System.IO.FileStream fs = new System.IO.FileStream (@"c:\text.txt", System.IO.FileMode.Open, System.IO.FileAccess.Read);

System.IO.streamReader sr = new System.IO.StreamReader(fs);

it gives me 3 errors: 1) field initializer cannot reference non static field. 2) StreamReader has invalid arguments. 3) cannot convert type object to system.io.stream.

how should i use it then?

Mar 18, 2010 at 10:57 AM Ruben
(comments are locked)
10|3000 characters needed characters left
Your answer
toggle preview:

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here



Answers and Comments



asked: Mar 16, 2010 at 01:39 PM

Seen: 17742 times

Last Updated: Mar 16, 2010 at 02:26 PM