Before staring this blog, I wrote one article regarding Unity, CATIA and the COM model. While it did show up some similarities between the three, it didn’t quite explain the whole idea behind COM’s object modeler. I mean I talk about interfaces, about components and lots of technical stuff that only show how similar he three are, but if a noob reads that article, I’m pretty sure he/she wouldn’t understand too much.
Well, this article tries to set things right.
The analogy I’m going to use has absolutely no connection to programming. You won’t hear words as “components”, “objects”. Instead, prepare yourself to enter the world of … refrigerators!
So, on our left we have our generic refrigerator. The only functionality this lump of plastic and metal has is that it keeps food cold. If you think of it, this is the primary function of the fridge.
Now, let’s assume you’re one of those people that love fridge magnets. But not these kind of fridge magnets! No. You’re a programmer trying to learn COM, so, obviously, you’re interested in fridge magnets that add value to your beloved fridge.
Let’s say that one day you find an obsure shop that sells weird magnets. You go in, looking for something … different. And there it is: The perfect fridge magnet! An LCD clock!
Now, as a smart programmer that you are, you simply put two and two together:
And it doesn’t have to stop there! You have a cheap pocket calculator? Glue a magnet on it’s back and voila! Your fridge gets another new functionality: it can now calculate stuff! And how about a barometer? That will turn your fridge into an indoor weather station. Not like the weather inside will actually change, but who cares? What matters now is that from a generic fridge you now have a super-multi-functional fridge. And you did that just by sticking extra gadgets to it!
Now, back to Unity and COM.
In terms of object modeling, that’s how COM works. Same for Unity.
In the image on our left, we have the FPS controller, that comes with Unity’s Island demo. I underlined for you two of the components that add functionality to our object:
- FPS Walker – responsible for actually moving the player when the WASD keys are pressed. Also handles jumping.
- Mouse Look – responsible for rotating the camera
Well, in our case, the fridge is the FPS Controller prefab. As for the fridge magnets, well, you’ve guessed it: they’re the FPS Walker and the Mouse Look scripts. And just like our fridge magnets, they’re adding new functionality to our prefab. And just like fridge magnets can be sticked to any metal surface (well, feromagnetic surfaces, if you’re a science maniac), Unity scripts can be associated to other componets as well. For example, you’ll notice that the Mouse Look script is also used on the Main Camera game object.
And this is another advantage of using a COM-like approach in Unity: you can re-use scripts. All you need to do is to drag them from the resource tree onto your object.
The only thing the fridge magnets (let’s call them components from now on) have in common is the fridge. In our case, the FPS Walker script and the Mouse Look share the object that owns them, the FPS Controller prefab. Now, this has an interesting effect, and I’ll give more details.
MouseLook mouseLookInstance = gameObject.GetComponent<MouseLook>();
the GetComponent() function will return me the MouseLook instance that is associated to the same FPS Controller prefab that also owns the FPSWalker! And that’s quite allright, because we’re asking the game object to give us the MouseLook component.
Having a headache? That’s ok, a new way to create objects is a tough lesson. Still, what you should remember is that (1) you can stick existing (or new) functionality to a Unity game object by simply dragging a component (a script) to your object and (2) for a given game object, you can easily access all its components, by calling GetComponent().