I want to know more about how Unity works, but how?

I’m sure I’m not the first person to ask this question, but I couldn’t find any good topic about it, so I’m asking here.

I’ve been using Unity for a few weeks now and am loving it as it makes doing a lot of things very simple. However, there are a few things where I’ll try to do something and Unity doesn’t exactly behave as expected, or I end up doubting the efficiency of my implementation because I don’t know what the Unity API functions are really doing when I call them.

For example, right now I’m making a small tower defence game as a learning exercise and want to get a list of n monsters within range x of a tower ordered by distance. This kind of thing seems like something that might be built into Unity, but I couldn’t find anything quite that specific. However, there ARE functions that let us get a list of objects by type, or get objects within a sphere around a point. Doing this is easy and I have no problem writing my own code to then sort the list of objects as I need. However, I don’t know how efficient that is because I don’t know how Unity is getting the objects in the scene.

I don’t need to know the really low level stuff, but it would be nice to know if Unity already performs spatial partitioning on objects in the scene to speed up finding objects within a range etc. or if I need to actually partition the scene and manage lists of objects myself. This is the kind of annoying code that I would rather not be writing so it would be great to know if it’s something that’s not necessary.

Another example is collision detection. Unity probably has very optimised collision detection routines in native code, but if I wanted to make a 2d bullet hell shooter and all my collisions are 2d points and boxes or circles, am I better off just doing it myself and taking the speed penalty of it being in a script? I’m asking because I think even the box collider is OBB, not AABB, so perhaps there is some large performance penalty there that I don’t know about.

A friend and I also had a bit of trouble using the Networking in Unity, which might have been a little easier if we knew more about how it was working and what Unity was sending.

Sorry for the long post. If there is any good resource for finding out a little more about the internal workings of Unity, it would be fantastic and enlightening to have a look at it.

You can take a look at Unity’s DLL such as UnityEngine.dll. Open them with monodevelop (using the Library explorer) to see the CIL or an approximate of the corresponding C# code.

This helps me sometimes. For example you can see that GetComponent() is as efficient as GetComponents() because the first ones calls the second one and returns the first occurence.

Of course, most part of the DLL is only a wrapper around native call to the internal engine. So you may not go very deep.

Your way ahead of me in skill. So this answer might be stupid to you but you can learn everything that Unity has implemented in it in the Reference Manual on the Unity site. Read through that, which you might already have, and you’ll learn everything Unity has to offer.