Tips For The Pragmatic Unity Developer

In the video below I’ve presented several productivity tips / hacks for the great Unity3d developer, that cultivates his / her essential qualities (impatience laziness hubris):

 

Sources & References

The three most essential plugins are DOTween, Text Mesh Pro and Console Pro.

Jet Brains IDE for C# preview here.

The “auto-save on play” script is here, The “no more hot reload script” is here, both by the same cool guy.

The scene object enable and disable script is this:

public class TheScene : MonoBehaviour
    {
        public GameObject[] activateOnStartup, deactivateOnStartup;
        
        void Start()
        {
            foreach (GameObject go in activateOnStartup)
            {
                go.SetActive(true);
                if (go.GetComponent<CanvasGroup>() != null)
                    go.GetComponent<CanvasGroup>().alpha = 1;
            }
            foreach (GameObject go in deactivateOnStartup)
            {
                go.SetActive(false);
                if (go.GetComponent<CanvasGroup>() != null)
                    go.GetComponent<CanvasGroup>().alpha = 0;
            }
        }
    }

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Untangling GameObject State in Unity

In this short post and video I try to discuss and clarify a few points about GameObject state in Unity with respect to game, scene and “runtime” scope. It is a bit more complex than one may understand initially, so bear with me a little.

When you start developing scenes in Unity, it won’t take long before you start asking questions like:

How can I get the same GameObject in different scenes?

Why do static properties sometimes get reset across scenes?

Why when I reload a scene I get duplicated objects which are meant to be singletons?

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?

Here are some answers.

Here is the full schema I refer to:

GameObject State in Unity

While MonoBehaviour’s  life-cycle is quite well documented e.g. both directly in Unity docs here and also by third parties e.g. here, how to handle GameObject’s persistence in and across scenes may be more obscure.

So here are written (partial) answers to the questions above:

How can I get the same GameObject in different scenes?
Would be probably better to reword this as “how can I persist and share data across scenes” – and there are many ways to do that :-)

Why do static properties sometimes get reset across scenes?
Only static properties which are GameObjects present in the scene will get reset (typically singletons). Other static properties will be preserved across the virtual machine.

Why when I reload a scene I get duplicated objects which are meant to be singletons?
That is because you marked those objects with DontDestroyOnLoad and created them (also) in other scenes. Create them via code (not in hierarchy) checking before their existence.

How can I comply with the (highly practical) principle “Make the game runnable from every scene” when I have global instances from other scenes?
This is best done just as explained above: create the global objects in every scene via code if they don’t already exist.

 

Unity Execution Order

Unity Execution Order – from Unity documentation.

If you are in need of learning some good patterns in software game development, a really nice book is Game Programming Patterns. Here are also 50 Tips for Working with Unity (Best Practices) which I reread from time to time, understanding progressively more and more of them (but still not all :D).

Thanks to Daniele Giardini for some feedback on the state scheme above.

Follow me on Twitter where I post about game design, game development, Unity3d 2D, HTML5, applied / serious games.

Social Share Toolbar

Game Development with Unity 2D – part 6: Pixel Perfect

Here we have a nice chat with the sinful Daniele Giardini about pixel perfect in 2D games, as a form of holy perfection.

We are continuing our voyage in Unity 2D development; previous trips: Part 0, Part 1, Part 2, Part 3, Part 4, Part 5.

We didn’t find a satisfactory definition of pixel perfect online, so this is Daniele’s: [Read more…]

Social Share Toolbar

Game Development with Unity 2D – part 5: A grid of Roman centuriae

This is the fifth part of a voyage in Unity 2D development, where we document in video the design and development of a simple strategic game reproducing the Battle of the Teutoburg Forest; here we make a more general discussion of grids in games. If you missed them here are Part 0, Part 1, Part 2, Part 3, Part 4.

We’ll dedicate this episode to discussing grids, which are at the core of so many 2D (and 3D) strategy games.  Up to now our battle has been in continuous space, but it would be cool to have a grid so we can more easily create an AI with a strategy. [Read more…]

Social Share Toolbar

Game Development with Unity 2D – part 4: Oplites, Back to Basics!

This is the fourth part of a voyage in Unity 2D development, where we document in video the design and development of a simple strategic game reproducing the Battle of the Teutoburg Forest; here we take a quick detour. If you missed them here are Part 0, Part 1, Part 2, Part 3, Part 5. [Read more…]

Social Share Toolbar

A very basic Unity 2D tutorial in C#

On YouTube there is a popular tutorial for Unity 2D game development for beginners, in 18 videos, starting here:

Unity 2d game tutorial–part 1 of 18

where you create a Galaga-like game.

The code in the videos is in JavaScript, I followed the tutorial re-writing all code in C#. Note: I am not the author of the YouTube tutorial.
It was quite noticeable while writing code how simply using a statically typed language as C# can avoid quite a number of errors in writing the code.

You can download the complete project written in C# here:
https://docs.google.com/uc?id=0B3QdVbIN8dohRmQtS2ZKai16Qms&export=download

Now some note of caution on the tutorial:

– It is not a tutorial on how to write code correctly in C#, or on how to structure object oriented programming for a game made in Unity, or on how to model AI, game mechanics and so on.

– It is not a tutorial on how to write real, commercial 2D games in Unity – more so now that Unity 4.3 is coming with a brand new integrated approach to 2D games.

The tutorial just introduces Unity as a 2d game tool and makes you use the very basic concepts. From what you learn you can use Unity to test basic game mechanics, but from this to creating professional games in Unity there is a long way to go. I’m proceeding with the books linked and with more complex projects: but thank you tutorial, it was a nice first go at it.

You can keep in contact with me on Twitter.

Social Share Toolbar