Using scene on big projects ?
Hi, I'm a "veteran" Flash dev working on the sequel of Swords & Souls. (here's the game if you have the time => http://goo.gl/NHIq8h)
But I'm new to Unity and I'm struggling about how to structure the project regarding Scenes.
I have no actual "level design" since all levels are data based content which are loaded on the go.
I have lot of different Screens (map, fight, characters sheet, inventory, town, each building in that town, different mini games, etc.)
I also have a lot of common data (mostly player related) which must be kept across all the game session.
In Flash, I had a blank screen, and everything happened in the code, not on the scene, so I'd like to keep that workflow as much as Unity allows it.
Any insight/feedback of your experience about structuring scenes on a big projects would be welcome!
Thanks for your time
Answer by pixelpowa · Jan 18, 2016 at 06:38 PM
"I had a blank screen, and everything happened in the code, not on the scene, so I'd like to keep that workflow as much as Unity allows it."
This is totally possible with Unity, but it requires some work.
First, you'll need an empty scene with a Camera in it. Then you'll need to attach a C# script to the camera, and that's basically your entry point for the rest of your code. You can update your generic game objects in the Camera's Update() method. For example, lets say you have a class called Baddie:
class Baddie
{
float x = 0;
float y = 0;
public void Update()
{
// baddie AI code
}
}
You'd call the Baddie's Update() method in the Camera's Update() method.
Rendering is a lot trickier, and I don't have very good example code at the moment. There are probably multiple (better) approaches, but this is how I did it:
Basically, if you want to render textures manually, you'll need to create a Mesh object that contains the vertices and UVs of the textures you want to draw. Then, in the Camera's OnPostRender() method, you can call Graphics.DrawMeshNow(). IIRC, you'll also need a SpriteRenderer attached to the Camera and call SetPass before DrawMeshNow. The SpriteRenderer will also need to have the relevant Texture object set as its mainTexture. Basically, this is how it might look:
void OnPostRender()
{
spriteRenderer.material.mainTexture = myBaddieTexture;
spriteRenderer.material.SetPass(0);
// update your mesh's vertices and UV coordinates.
Graphics.DrawMeshNow(mesh, Vector3.zero, Quaternion.identity);
}
You can also improve performance by exporting all of your graphics onto the same texture and including all of your sprite coordinates in the same Mesh. (I'm using TexturePacker to help with this.) Depending on your game's art, this may or may not be feasible, but the less drawing calls you make the better.
Hope this helps point you in the right direction. I used this approach to port my game from XNA/Monogame to Unity, and I had to do very little rewriting at all.
Answer by SoulGameStudio · Jan 18, 2016 at 08:06 PM
Thank you very much for your detailed answer. I should have been more accurate => I'm not a 100% code purist, I have no worries pre-defining lots of Prefabs and other needed assets. So I don't think I'd have troubles with rendering since I use Prefabs for all my in game content (projectile, enemy, hero, etc.) stored in the Resource file, so I can add them to the scene properly whenever I need.
What I wonder tho is, let's say I choose not to use multiple Scenes, does it mean I should use giant Prefabs containing each Screens (town, interiors, map, etc.)? And load/unload them as the player navigate? Same question for HUD. Stuff like that. Mostly I wonder if scenes are "must-have" in big projects like that.
@SoulGameStudio Ah I see. I have limited experience using Unity's scene editor, but I think that's a fine approach. You might even be able to nest them inside empty GameObjects and then enable/disable each menu's parent GameObject as you need to, which might make the transitions faster. Here's a tutorial vid on this concept.
Again, though, I'm far from an expert on Unity. I only stumbled on this topic because I was researching ways to handle large 3D levels in Unity, haha.
Okay! Thanks for your insight and for the tuto, watching now. The idea is quite tempting actually.