Display Object

Turn visual content on when you want to, not how your lead artist left it last in the editor.

The beauty of a story is how not every piece of it is just thrown on the table at once. Things evolve, things appear and thing disappear. To tell a story, create a game or execute an application you will have assets and pieces that need to only make an appearance when they are needed. Most times this means setting up your scenes so certain things are "off" until they are needed. This often leads to volatile situations where, after editing, someone inevitably leaves the wrong thing on or off.

DisplayObject was designed to help alleviate the pains of these situations and it does so very simply. DisplayObject just turns things off at runtime before anything else (if they were left active in the editor). That's it. It manages proper availability and removes the burden of artists and engineers to ensure a scene is "set up properly" before use.

A real world situation

Let's take a look at a common scenario. Our game has 3 GUI panels that are needed in different squences and combinations: a pause screen, a hud, and a level start. These panels could not be wrangled into a State Machine because there are two combinations that can occur where discreet states would fail: hud with pause and hud with level start.

While working on the GUI for this someone is more than likely going to forget to hide things and leave this for everyone to see at some point: A nice, messy, GUI sandwich.

However, if you add DisplayObject to each GUI panel our GUI sandwich possibilites vanish and we even get some clever editor tools for dealing with this stack while editing in the form of a suite of buttons for rapidly adjusting visibility.

Now that we are working with DisplayObjects we can simply set the activity of these panels with a call to DisplayObject's SetActive method. We have complete control of when things are shown to our player 100% of the time since we know these objects will always be disabled when our project runs.

using System.Collections;
using UnityEngine;
using Pixelplacement;

public class GUISystem : MonoBehaviour
{
	public DisplayObject _pausePanel;
	public DisplayObject _levelStartPanel;
	public DisplayObject _hudPanel;
	private bool _paused;

	IEnumerator Start()
	{
		_hudPanel.SetActive (true);
		_levelStartPanel.SetActive (true);

		yield return new WaitForSeconds (3);

		_levelStartPanel.SetActive (false);
	}

	void Update ()
	{
		if (Input.GetKeyDown (KeyCode.P))
		{
			_paused = !_paused;
			if (_paused)
			{
				Time.timeScale = 0;
				_pausePanel.SetActive (true);
			} else {
				Time.timeScale = 1;
				_pausePanel.SetActive (false);
			}
		}
	}
}

Order to the chaos

Projects get complex quickly, especially with large teams. Keep DisplayObject on your toolbelt for reducing issues with visual content especially where a StateMachine won't fit the bill. Don't forget you can also extend the DisplayObject class to supercharge organization of visual elements with logic.