I’m coming to you guys/gals looking for some feedback on similar experiences, tips, tricks, places to start, examples, etc… on how to move my teams current project to more of a code driven general purpose C/C++ style approach. The presentation at http://unity3d.com/support/resources/unite-presentations/techniques-for-making-reusable-code by Mike Mittleman leads me to believe that someone else has taken this approach. At the basic level I would like to define a main entry point, instantiate some manager objects, and load a level to programmatically add our game objects to the heirarchy vs. dragging and dropping the “objects” from the project window to the heirarchy window then resolving the asset references. I really appreciate what Unity gives me in terms of a WYSIWYG editor/RAD tool however, my dev team and I feel really constrained in expressing complex ideas for games that have more than one scene/level. I’m a 6.5 year professional C/C++, TCL, etc… coder for an EDA company here in Portland OR so I’m not afraid to get in there and start writing tons of code. I’m just finding it difficult to figure out where to start. The documentation, presentations, tutorials, etc… all seem geared towards using the editor as the main entry point in developing a game. Not to be pretentious or condescending I feel what I’m trying to do is more advanced or at least not the typical way of doing things. I have really been scouring the documentation, resources, and forums trying to find examples on what other developers have done to no avail. Any and all help is greatly appreciated.
A few questions to help get the convo started:
Is there a single function I define/overload such that gives me a single entry point into Unity? I am looking for the equivalent of main(). The only entry points I can see using are Awake and/or Start.
Does there have to be at least 1 object in the heirarchy to get the whole process going? Or, does Unity just instantiate objects that have certain callbacks defined as it “compiles” the code.
Could someone take the time out of their day to help me by giving/showing me the effective hello world C# code example without having to drag and drop an object into the Unity heirarchy via the editor?
Thanks for taking the time to read this and I look forward to positive and constructive replies. Feel free to ask me questions to help clarify things for you!
No
But if you only have a single active component in your whole scene you can make the Awake callback of that component your entry point
At least a single game object must be there to have a component doing that. Stuff outside of components can only be controlled by components.
Components are the “base layer” between the unity engine and your own functionality.
From what I remember there is an article on the unity wiki from a user who primarily developed outside and wanted to avoid using the editor.
But as it was already told back then to him, I will also ask you: Why do you want to use Unity if your primary interest is to avoid and break 80% of what makes Unity unique (highly modular, reusable code and easy fast configuration of larger functionality through component combination), why don’t you just use another technology that works more like you want?
Because you only cut your possibilities by avoiding unity, you won’t expand it. You at worst even cut the possibility for Webdeployment. (iphone deployment is more or less definitely gone)
Yes, they are Awake/start and you create them on each game object you want to add on a script.
you can create a game without any game Object on the scene, but you should know that any script has to be attached to a game Object in order to get executed.
[/quote]
It is not that I/we want to “avoid” using Unity’s editor. In fact the Unity editor would be up 100% of the time when myself and my team are writing code. We see great value for prototyping, debugging, and visualizing some of the functionality we are creating which to seems like the ideal use of the editor. We can make a tweak or two by dropping an object into the heirarchy and then apply the change back to the code. But, for general development it seems like the wrong place to be.
Maybe what isn’t clear to me is what teams do to manage medium to large games. Would you make a Unity scene for each level? Do they drag and drop every game object element on the screen? Even Mike Mittlemans presentation seemed to suggest that they use Unity in one hand and then do things outside of dragging and dropping assets into Unity on the other.
This dragging and dropping assets into the heirarchy windows doesn’t seem like the scalable way of making a game, or at least our game in my opinion. Creating code to load level data which then creates the game objects, components, etc… on demand seems to be the more managable way to go.
Am I completely off base here in the way I’m thinking about this?
I removed the “non-editor based scripting” from the title of the post. I think that was mis-leading from what I’m trying to find out. The Unity Editor was one of the primary reasons to purchase.
I am just trying to find the most effective way of using it to my advantage in bringing my teams idea to life.
you are right on it but also off on some things I guess due to where you come from as Unity is not as the rest.
There is nothing that prevents you to create things dynamically through code. Thats how spawning of dynamic things is commonly always done.
But there is a massive amount of stuff in a game that is static. From the world itself, over light sources, movement paths, special interaction objects (treasure chests, doors, …) over even basic must have objects like spawn points for AI entities and the player.
Those things make real sense to be done visually, defining and refining them programatically is really not the way any designer will work with.
you always should see the Unity Editor as the end where your modeller and designers work.
Coders won’t work in there.
But they need to know how it works so they can offer the functionality in a flexible and efficient way to the designers that will use it in the editor, as they otherwise will follow their “common patterns” which will likely lead to a suboptimal way of doing it in relation to Unity driven development.
The code development itself is either done in the Unity Scripteditor or alternatively Visual Studio if you use C#
I would recommend to check the documentation on Prefabs as well as the Network Example to see code driven spawning in Unity in action, especially how you can support designers and they you in doing it in a fast yet highly flexible way. They hopefully show you how components help you to connect coders with designers.
To load levels you would just use Application.LoadLevel if we talk of distinct levels
There is nothing else needed, that will actually load a scene you included in the build and call Awake on all components in the scene, including a fictive “main” component.
If you want to work within a single scene thats possible too, but that will require a significant higher amount of code (LoadLevelAdditive as well as sophisticated object handling so you know when you have to destroy which. Potentially even AssetBundles are needed)
I must admit, when I first started using Unity I found it annoying that there was no way to attach a script to the scene itself rather than creating a dummy object. It would probably be worth adopting a convention where the object that represents the scene is always given the same name (“SceneSupervisor” or something) and that this is consistently followed by all programmers. Similarly, some simple naming/folder conventions for scripts can go a long way. Unity is very flexible, and like all flexible tools, it sometimes takes a bit of unwelcome effort to specialise it to your task.
You need to use the overridable functions in the MonoBehaviour class. I’ve never done this, but if I were going to create a main game loop then I would write.
public class GameLoop : MonoBehaviour{
void Main(){
while(true){
//Game goes here
}
}
void Start(){
Main();
}
}
Create one dummy object in your scene and place that script on it. Then you can run your game from that main loop.
The engine wasn’t really intended to work this way, but I can’t think of why this wouldn’t rok.
Also if you want to load objects in code then you should take a look at the Resources class
This isn’t exactly the normal Unity workflow, but different strokes for different folks.