Game architecture


I'm currently thinking about the Architecture of my new Unity-Game. I'm struggling with 2 options:

1) Create GameObjects with the Editor for Entities like Player, Enemy, Items and then attach some Scripts to this Objects.

2) Have a custom Master-Class (like Game) that holds all Game-specific data, like Players, Enemies, Gamestate, etc. And then create and attach the GameObjects to this custom-objects.

I think the second approach is a must if I want to create Objects during Runtime (e.g. random enemies). Is it even possible to create GameObjects from scratch (with GameObject(...)), I just found solutions with cloning/coping existing Objects.

Has somebody any suggestions or options I have missed. (technical answers are welcome, I have some programming background ^^)

regards knoggly

Both are viable options, and it really comes down to your game and it's complexity.

With option one, you as the designer have full control over everything you do, however you also end up having to do all of the work in a repetitive and tedious manner. This is probably the safest way to work, but if you have a background in programming, you can create a better way.

With option two, you can have a "Factory" class, it could be a single factory that generates everything, or you could have a separate factory for each object type you need to spawn. You can hold a reference to each prefab you need to spawn in these factories, and simply use Instantiate() to generate your enemies, players, etc. The problem with this approach is your code needs to know where to place these, otherwise it will end up defaulting their spawn point to the origin, and that can get a little predictable.

To counter this, you can easily make a little Editor tool to place spawn points around a level, and programatically select a spawn point for each Instantiation you perform. It sounds like a long way around, but over the course of your entire game production, it could save you a lot of time.

As for your last question, creating GameObjects from scratch, it is possible. Here's a quick example.

// Create the gameobject
GameObject newObject = new GameObject();
// Add the required components
// Set it's position in the world
newObject.transform.position = new Vector3(50, 1, 50);

Obviously you could do a lot more with it, but I would probably recommend cloning prefabs with Instantiate, as writing a tonne of code to make what is essentially a prefab every time is a bit of a waste. On the flip side, if you are randomizing the abilities of enemies on the fly before spawning them, then this way could be a great option to take.

I think it strongly depends from the design of your game...

EG: our game is very complex, but for level design issues, we HAD to use the unity bult-in editor to build all the levels (differently we'd have to serialize a graphic package scene or other stuff, and this is a no-no for fast development), but we do use the Resource folder, loading the objects dynamically from scripts when needed, for particles, and other stuff..

The best bet with unity is working strongly with prefabs, instanced in the scene, and have the dynamic ones placed in the Resources folder.


thanks for your Answers, I think (surprise, surprise) I will use a combination of both methods :). Create Stubs/Templates for all visual objects, place them somewhere offscreen and then "clone-them-in" on use. I think some data management aside in custom c#-objects is also neccessary to store states across Scene-changes.

regards knoggly