- Home /
Programatically creating entire Unity games
This is, in a sense, a follow-up to this previous question: http://answers.unity3d.com/questions/12146/Create-Unity-scenes-programatically.html.
I am a somewhat seasoned web/enterprise developer, and I am very used to write my code "in plain text" and without the necessity of using a visual editor. Learning how to do things in a graphical enviromnent subject to future changes is costly and takes away some of our programming power.
Is it possible to create an entire Unity project without the need of the Editor - that is, programatically?
Of course I understand that, being made in a fully-fledged game engine, an Unity game requires some "boilerplate stuff" to work, such as a specific directory structure, configuration files etc. What I am asking is if we can actually, after setting up all this "framework", do something like this:
UnityGame game = new UnityGame("My Game");
GameScene scene1 = new GameScene();
GameObject object = new GameObject();
// ...do some stuff to the GameObject
// ...such as adding behaviour, other GameObjects
// ...Transforms, scripts etc.
scene1.addObject(object);
game.addScene(scene1);
Or, in other words: can we create a game using Unity as a software developer would use an application development framework (such as ASP.NET, Java's Spring or Hibernate, Ruby's Rails etc.)?
Answer by Loius · Aug 13, 2013 at 02:11 AM
Yes. You need a master MonoBehaviour; its Start will act as your "Main" function. From there you can make and break all you need to without ever going back into Unity's editor (by the by, "Scenes" are not classes of any kind; they're really just files describing an arrangement of assets. you'd just create the objects themselves rather than adding them to any specific scene)
Though I wouldn't recommend it. Custom editors can make the design process trivial and idiot-proof. The project I'm working on is mostly external to Unity but I'm still writing lots of custom editors so the designers can make changes without talking to me all the time. Also, trying to replicate the behaviour of GameObjects without actually using GameObjects is hundreds of headaches just waiting to happen.
Thanks for the answer. Is there any place/article/documentation/whatever where I can get a more "in-depth" knowledge on how to do this? I've searched the "standard" documentation and it wasn't very helpful.
I understand doing everything "by hand" without the Editor is probably a bad idea; actually, my question is more out of "curiosity", in order to get a better understanding of Unity's internals.
$$anonymous$$mmm, I don't know of any articles or anything that are all about "how to not use unity for unity" or whatever this is called.
I can give you some function names and methodology, though; Google can send you towards the right places with these hopefully:
Use Instantiate to create objects; you'll probably want to employ Pooling to keep performance with large numbers of reused objects. The Resources folder may be heavily required. Using BinarySerialization gets you small, updatable files; I binary-serialize most of the data in my project. Writing a little helper library to be able to save any System.Serializable class (which most of your classes should be) to a Resources location can help with that.
Lots of the other stuff is "common sense" (i.e. programmer know-how) and frequent, religious perusal of the Scripting Reference
Answer by cdrandin · Aug 13, 2013 at 12:29 AM
I would say you can because simply the inspector is built off there own parser program for custom editors. The inspector is just middleware from what is actually being sent to the "core" of the program. This is just based of my observation of how Unity has worked with me so far and how the inspector works.