Workshop 2

From ETC Public Wiki
Jump to: navigation, search

Workshop 2 presented by Bryan on Friday, September 20, 2013.

Download the workshop unity package, start a new Unity project, then import the package to view inside Unity.

Discussion Notes

Runnable From Every Scene

The web site 50 Tips for Working with Unity (Best Practices) offers the following wisdom:

Tip# 10. Make the game runnable from every scene.

This is often easier said than done. Quite often a game requires some persistent objects that live across scenes. The quickest way to implement this is to create some DontDestroyOnLoad GameObjects in Scene 0. While this is easy to implement, the game must be started from Scene 0 or it won't be initialized correctly. As an alternative you might consider employing a Singleton as part of a prefab which exists in each scene.

The Singleton Pattern

Wikipedia defines a Singleton as:

In software engineering, the singleton pattern is a design pattern that restricts the Instantiation of a class to one object.

The singleton, unlike a static class, is a fully instantiated instance of the class. This yields the following benefits:

  • Nearly any existing class can be made into a singleton using scaffolding.
  • Unlike static classes, the singleton can both raise and receive events.
  • Unlike static classes, singletons can use parameterized constructors.

There is a nice Singleton pattern in the Unify Community wiki which is specific to the needs of the Unity engine and is based upon a MonoBehaviour. Please refer to example files of an example of its usage.

The IsReady Pattern

This is a simple pattern designed to eliminate null reference exceptions at runtime by allowing dependent classes to wait until their dependencies are fully initialized. This is not a classic design pattern but instead one crafted specifically for the needs of Unity. It makes use of a special feature of the MonoBehaviour.Start() method which allows it work as a coroutine.

NOTE: Someone asked how can Start() be both void and IEnumerator since they have conflicting signatures. Look at the Unity documentation for MonoBehaviour and you will see that Start() is classified as a message not a method.

Please refer to the example files for implementation details.



Freeform Discussion

Use the Build Feature in MonoDevelop

Use the Build feature in MonoDevelop to validate your code before dropping back into the Unity Editor.

Avoid Abusing the Resources Folder

When Unity builds an executable for deployment, it creates a manifest of all the assets needed by analyzing each scene. All assets assigned via the editor are added to the manifest, unused assets are excluded to optimize build size and start up times. In addition, Unity can use a feature called streaming scenes which loads assets as they are needed based on the manifest.

The Resources folder is treated differently. ALL assets in the Resources folder are included whether they are used or not and they are loaded up front increasing the initial download size and delaying the start of your game.

Example Use of the Plugins Folder

The plugins folder has many uses, among them is the ability to import libraries as prebuilt Dynamic Link Libraries (DLLs). For an example, check out the Jam-O-Drum interface. The JoD plugin was written, tested, and compiled entirely within Visual Studio then exported to Unity as a DLL. Using the Plugins folder in this fashion is like adding a Project Reference in Visual Studio.

Final Thoughts

The ETC Unity Wiki is a community resource. By nature wikis are editable by their users, that means you. Please contribute to the wiki whenever you see an opportunity to improve it. It might be a simple typographical correction or it may be a cautionary tip, any improvements will help your fellow classmates.