Unity3D: the editor/code duality

So as most of you know I have been doing Flash for a long time and recently I have been doing Unity3d (well mostly NGUI you could say). It has been mostly fun and mostly rigid.

Rigid???

Yeah I would say rigid. Unity3d imposes you a way to work that is mostly to use the editor and not programming. They really really want you to use the editor. And coming from Flash this feels really weird. Probably because after 6 years of using Flash you kinda learn that the most you can do in code, the better and easier it is. Also, if you constantly switch between the editor and the code, it gets confusing, always switching paradigms.

GameObject for president!

GameObject is the root of everything you do in Unity, but one of the most annoying thing is that you can’t extend GameObject… Come on, let me do it, just the tip ;) . Here is what I would do if I could; I would make myself APIs for 2D. Moving something in x would be

gameObject.x = 30;

instead of

gameObject.tranform.localPosition = new Vector3d (30,  gameObject.tranform.localPosition.y, gameObject.tranform.localPosition.z);

Man have you seen the size of that thing just to move something in x???? Let me make it better for myself, I don’t care about 3D, I wouldn’t have a z value I would call it depth and it would make the code way more readable.

Encapsulation, what encapsulation

The other thing that bugs me is that to interact through code with a gameObject hierarchy you created, you basically have to know how it is built and you have to get your pieces using Find or GetComponent. Find statements are the ugliest ones as they use a string to get you what you want and is really error prone.

So you add your Script to your gameObject (proof that the editor is taking precedence over code) but to have interactions between multiple gameObject you have to do it through their scripts which you have to know their types, but have no idea if they exist and are linked or not. You’ll know at runtime when the error pops up.

Everything on a GameObject

Basically my point here is that you can’t do anything if it ain’t on a gameObject. Some core functions just won’t work if it ain’t. Like the WWW function(used to load stuff from the web) (really cryptic name if you ask me). It won’t work if it ain’t called from a MonoBehavior and MonoBehaviors can’t be instantiated, it needs to be added to a GameObject. So what about Models, code that only keep the states of an application or that loads data to hold it. Models have nothing to do with GameObjects, they should be allowed to use the WWW function. But no, if you want to, you need to create a GameObject, add to it a loading script, and wait for it to pass you back the data. Doesn’t that sound devious to you?

Let it define itself

I like Unity3d; it allows you to build for Android / iOs very easily, but the way it is so rigid really annoys me. I think that a project that is so young like Unity3D should not force the users down some path, it should let them find incredible and unthought ways of using it. I want to do 2D / UI with it and right now it is pretty annoying to do so.

, , , , , ,

  1. #1 by jack - June 19th, 2013 at 16:09

    well in my projects i usually have only one gameobject as mainloop and all other important model / controller stuff is not component added to gamebobject unless it’s direct controller for particular gameobject and i mostly use prefabs atm

    gameobject (except the mainloop) for me is view only

    ofcourse u can’t extend gameobject, but u can extend component and i don’t make lists of gameobject, cause it’s ridiculous to call GetComponent all the time, i make lists of the components and there u can extend the functionality to x, y, depth of course

    etc. this article is complete misunderstanging of unity at general … and btw i was working in flash very long time and yes it wasn’t easy to switch to unity but i love that, cause flash sucks :D

  2. #2 by zedia.net - June 19th, 2013 at 18:13

    @jack Well I wrote this so that someone could prove me wrong. I want to learn the right way to do things.

    Anyway, here is a thing, if you instantiate a prefab, or if you Resource.load it, you get a reference to the gameObject. Now you have to GetComponent the script attached to it, but you don’t know its name or if it exists at all. That is really weird to me.

  3. #3 by Mark Mandel - June 20th, 2013 at 21:26

    Preface: I’ve not worked with Unity3d.

    From what I’ve read (http://en.wikipedia.org/wiki/Entity_component_system) Unity3d is based on an Entity System Component architecture. This is quite different from standard OO practices, and is quite likely what is causing you so much consternation.

    I would suggest starting at: http://entity-systems.wikidot.com/

    Which gives a good overview (and code samples if I remember correctly) on ES architectures and why people use them for gaming.

    I hope that helps!

  4. #4 by jack - June 24th, 2013 at 06:49

    @ zedia.net
    you are right, you don’t know it, but i am sure it’s possible to deal with this situation

    you can design the app the way you know what component is on what gameobject like using tags

    or you can parse all gameobject by function which will separate them to groups by components

    as i wrote on serious stuff i am using prefabs with one component only and for me prefab means the particular component and it’s gameobject is only shell; i use extending like monobehaviour > entity > movingentity > enemy > soldier and it works well for me

  5. #5 by sebbe selvig - June 25th, 2013 at 03:56

    I think the component way of Unity is really great, but it takes some time to wrap your head around. Of cause you can manipulate the components directly from components attached to other GameObjects, but that will soon give you headaches!

    Bear in mind that Unity primary focus is 3D, so most intensive interaction would be 3D collision events and keyboard/controller input. Other loosely coupled events can be done through SendMessage, SendMessageUpwards and BroadcastMessage. The functions takes the function name as a string an will call it on applicable components attached to the GameObjects. It’s slower than direct function calls, but it’s not meant to be used for logic that is called every frame.

    To work around the “problem” you point out, you could look into the decorator pattern.

    var obj2d = new Object2D(gameObject);
    obj2d.x = 30;

    The above would have an setter like this:
    float x {
    set {
    contextGO.tranform.localPosition = new Vector3d (30, contextGO.tranform.localPosition.y, contextGO.tranform.localPosition.z);
    }
    get { return contextGO.tranform.localPosition.x; }
    }

    where contextGO is the gameObject you pass in the constructor.

    Also you could use the cool extension methods available in the C# sharp language that enables you to attach new methods to existing classes like so:

    public static class MyExtensions {
    public static void MyExtendedMethod(this GameObject gameObject) {
    // Do something
    }
    }

    But it definitely would be cool to be able to specify what class unity should use as the class for GameObjects.

  6. #6 by Mario R - January 7th, 2014 at 17:16

    Hey, I’ve been looking through your shader for masking in ngui, I wonder if you have come accross the need to have an ngui panel clipping a 3d mesh, I’ve been trying to do so for a few days and looks like it’s beyond me, any advice on how to achieve this?

(will not be published)
Subscribe to comments feed
  1. No trackbacks yet.