Unity: Dense Auto Increment Your Build Number Across Scenes

I was looking for a way to have a build number in Unity so that:

  1. I want a “build” number to be dense, at least as dense as commits
  2. it should auto increment on run
  3. it should auto increment from whatever scene you are launching your game
  4. it should be possible to decide to show it or not in every scene
  5. it should totally take care of itself – so when I get feedback on a new build I can always ask the tester which build is she using, and I don’t go “damn it I forgot to increase the build number!!”

The idea is simply to have an editor script that changes a prefab (so that you can place it in every scene, but has a global state) each time you run. Here is my code, which you can fit to your needs (this also does the life-saving “save scene before run” which you may pick too):

// Author: Pietro Polsinelli - http://designAGame.eu
// Twitter https://twitter.com/ppolsinelli
// All free as in free beer

using TMPro;
using UnityEditor;
using UnityEditor.SceneManagement;
using UnityEngine;
using UnityEngine.SceneManagement;

namespace OL
{
    [InitializeOnLoad]
    public class AutoSaveOnRun
    {
        static AutoSaveOnRun()
        {
            //Thanks https://twitter.com/andrewlukasik for the "+=" fix.
            EditorApplication.playmodeStateChanged += () =>
            {
                if (EditorApplication.isPlayingOrWillChangePlaymode && !EditorApplication.isPlaying)
                {
                    GameObject build = AssetDatabase.LoadAssetAtPath("Assets/__Scripts/Tools/Components/Build/Build.prefab",
                        typeof(GameObject)) as GameObject;

                    if (build != null)
                    {
                        TextMeshProUGUI bn = build.GetComponent<TextMeshProUGUI>();
                        if (string.IsNullOrEmpty(bn.text))
                            bn.text = "1";
                        else
                        {
                            bn.text = (int.Parse(bn.text)+1).ToString();
                        }
                    }
                    
                    EditorSceneManager.SaveScene(SceneManager.GetActiveScene());
                    AssetDatabase.SaveAssets();
                }
            };
        }
    }
}

P.S. More approaches

In order to change the build on compile and not on save, I now use this code in the (also very useful) stop-play-on-compile class:

// Copyright Cape Guy Ltd. 2015. http://capeguy.co.uk.
// Provided under the terms of the MIT license -
// http://opensource.org/licenses/MIT. Cape Guy accepts
// no responsibility for any damages, financial or otherwise,
// incurred as a result of using this code.

// Modified by Pietro Polsinelli. 2017. https://twitter.com/ppolsinelli

public class ExitPlayModeOnScriptCompile
    {
        static bool increasedBuildForThisCompile;

        // Static initialiser called by Unity Editor whenever scripts are loaded (editor or play mode)
        static ExitPlayModeOnScriptCompile()
        {
            Unused(_instance);
            _instance = new ExitPlayModeOnScriptCompile();
        }

        ExitPlayModeOnScriptCompile()
        {
            EditorApplication.update += OnEditorUpdate;
        }

        ~ExitPlayModeOnScriptCompile()
        {
            EditorApplication.update -= OnEditorUpdate;
            // Silence the unused variable warning with an if.
            _instance = null;
        }

        // Called each time the editor updates.
        static void OnEditorUpdate()
        {
            if (EditorApplication.isCompiling &amp;&amp; !increasedBuildForThisCompile)
            {
                increasedBuildForThisCompile = true;

                GameObject build = AssetDatabase.LoadAssetAtPath("Assets/__Scripts/Tools/Components/Build/Build.prefab",
                    typeof(GameObject)) as GameObject;

                if (build != null)
                {
                    TextMeshProUGUI bn = build.GetComponent&lt;TextMeshProUGUI&gt;();
                    if (string.IsNullOrEmpty(bn.text))
                        bn.text = "1";
                    else
                    {
                        bn.text = (int.Parse(bn.text) + 1).ToString();
                    }
                }
            }

            if (!EditorApplication.isCompiling)            
            {
                increasedBuildForThisCompile = false;
            }

            if (EditorApplication.isPlaying &amp;&amp; EditorApplication.isCompiling)
            {
               Debug.Log("Exiting play mode due to script compilation.");
                EditorApplication.isPlaying = false;
            }
        }

        // Used to silence the 'is assigned by its value is never used' warning for _instance.
        static void Unused&lt;T&gt;(T unusedVariable)
        {
        }

        static ExitPlayModeOnScriptCompile _instance = null;
    }
}

Yet another approach is to identify the build number with a platform build (I don’t use that because I want density): https://gist.github.com/andrew-raphael-lukasik/36a30f0955d7cdc758e394dc4e7266bf.

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

 

Social Share Toolbar

Character Trait Model: How Unhappy Is Unhappy

Modelling character traits can be tricky: an example problem has been presented by Jon Ingold in this GDC talk. I discuss the problem below and present a sample model that solves it. I provide an implementation in a C# class.

For character trait one may think of say the character happiness, or a relationship-with-X trait.

The problem is presented from minute 36 of the talk: the first idea that comes to mind in modelling a character trait is by using a number. Greater the number, better the state of the trait. This doesn’t work very well.

How Unhappy is Unhappy

From Jon Ingold GDC talk.

Then Ingold quickly jumps to a proposed solution, which consists in tracking two numbers, positive and negative experiences:

Unhappy vector

From Jon Ingold GDC talk.

Apart from the fact that changes are modelled more appropriately using two variables, what was exactly the problem with using one number?

Here is how I understood the problem: suppose you want to model the happiness of a character in a gameplay. You say that the variable HappinessLevel determines HappinessState according to these values:

HappinessLevel >= 5 = VERY_HAPPY
0 < HappinessLevel < 5 = HAPPY
-5 < HappinessLevel <= 0 = SAD
HappinessLevel <= -5 = SUICIDAL

The character goes through many episodes in two different game-plays: in one the character has 2 positive episodes, and 8 negative ones, and so goes SUICIDAL: 80% of the episodes were negative.

In another gameplay, the character has 20 positive episodes and 25 negative ones. Character is still SUICIDAL, but actually only  55% of episodes were negative! Something clearly does not work :-(

Taking the hint from the talk above, I’ve implemented a generic class model for Character Trait that considers the whole set of the episodes. The set of states and their level can be injected; moreover you can have a “decay %” so that for each new episode, all previous ones have a decay, so older the episode less relevant it gets :-) (by default decay is 1 so its turned off).

You find the class and a test (for Unity) in this zip. It can clearly be refined ad infinitum in function of specific needs, e.g. having episodes that are both positive and negative and so on.

A couple tests:

First test no decay test 1 output
Second test with decay Log with decay

Thanks to Daniele Giardini for campaigning for LINQ removal from the code.

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

Social Share Toolbar

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

A note of the wild use of “if(object)” in Unity’s C# code

Looking at other people’s C# code for Unity, I kept seeing this bizarre usage of if:

if (object) { …}

where object was a real object, not a bool instance. This is a JavaScript kind of usage, where there is a big (perceived) mess about object and value types, so I wondered how did C# “automagically” cast to bool? You cannot do that in classical typed OOP languages like Java.

Turns out there is nothing about this in C# itself, its MonoBehaviour that implements implicit boolean casting

public static implicit operator bool($classname$ me){
return me != null;
}

Actually you will have to navigate the hierarchy up to Unity’s Object to find it. See also here.

But if you are learning C# with Unity, be careful as “This can be really confusing because that behavior doesn’t carry over when you start using standard .net libraries and other 3rd party code that is agnostic to Unity.” – here.

And neither it will when writing your classes, when not extending MonoBehaviour, so its not a healthy habit to catch.

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

Social Share Toolbar

A note on slimy static Unity3d gameObject instances

A quick note on the state of C# static variable instances in Unity3d.

So I just discovered that static properties of classes extending MonoBehaviour on level reload may get reloaded or not, depending on circumstances.

The problem is this: you may need in your game development to have a singleton manager class instance which is also a game object. [Read more…]

Social Share Toolbar