Repository

Looks good to me!

User Tools

Site Tools


kb:tools:unity:start

Unity

Changelog

  • 2024-10-08: Created page after LDJ56 (72 hour game jam) in October 2024, as a form of after-action review.

The game development tool, not the desktop environment.

Installation instructions (via Unity Hub)

Note the information on this page may not be straightforward to understand without some basic prior knowledge on how Unity works. For tutorials catered to the inexperienced, there are many Youtube videos etc. out there.

Setting up a new project

Project setup

Add a gitignore catered for Unity before pushing to a repository.

Sample gitignore

Art and music assets may be placed separately, preferably in a Git-LFS submodule to decouple the assets. Note that metadata associated with each asset will be generated, so the art/music liaison should be responsible for loading these assets to Unity prior to updating the asset repo.

Nuke option when loading all submodule files, in case of some corruption:

user:~$ git submodule update --recursive --remote --force

Basic structure in Unity

Unity gameplay is grouped into individual Scenes, which contain a set of GameObjects. Note that game objects do not have to represent something tangible in the scene: it can hold game logic, or hold a collection of assets, or even just function as an item separator in the editor's scene menu. Some discipline/framework should be used to organize these objects.

Built-in Unity is plenty capable, especially considering the built-in physics engine, so no scripting is required. For custom behaviour such as user-scripted behaviour upon physics collisions, multiple scripts can be assigned to the same object. The base structure is a C# script inheriting from a "MonoBehaviour" class that exposes the Unity API (and boy, the syntax in C# makes it much, much more similar to Java than it is to C++):

Sample code

Scripts belong to a larger umbrella which includes colliders, transforms, renderers, as well: these are collectively called Components. Some integrations afforded by Unity:

  • Public variables exposed can be modified from the Unity Editor.
  • Game object components can be directly cross-referenced via script if the are attached to the same game object.
  • Game objects can be directly referenced by using tags (functions as a global state table).
  • The main logic happens in:
    • Awake(): Initialization happens, even if the object is not active.
    • Start(): Further intialization after the object is made active.
    • Update()/FixedUpdate(): State modifications per frame/fixed time interval.
    • OnDestroy(): Release of resources cached by the object.
    • The main execution loop in Unity will be helpful in this regard.

Some early notes to be aware of:

  • Triggers and collisions are mutually exclusive. Collider (for 3D) and Collider2D are also mutually exclusive.
  • Prefabs should be extensively used to minimize code duplication across multiple scenes.
  • Objects can be spawned and despawned, using the "Instantiate" and "Destroy" methods. These can also be done relative to the Camera viewport:
// Coordinates relative to Camera center
Camera camera = Camera.main;
float height = 2 * camera.orthographicSize;
float width = height * camera.aspect;
 
// Spawn and despawning
GameObject obj = Instantiate(spawnObject, spawnObject.transform);
obj.SetActive(true);
Destroy(other);

Some other design patterns / code blocks for future reference:

Delayed children game object activation

Timed scene transitions

Singletons for global states

Project management

More experienced people can give better tips, but some things I found useful from one of my game jams:

  • Team leads must be present to coordinate between each department, to minimize conflicting information.
    • An common communication platform should be setup to allow quick communication between teams and within teams, e.g. Discord and channels.
  • Meeting agenda must be agree beforehand to avoid unexpected meeting overruns.
  • Shot and asset lists should be prepared for asynchronous communication with developers on what assets can be integrated.
  • An all-hands physical meeting should be done at least once for introductions and communicating expectations on commitment and experience levels.
    • Opportunity to have the development environment set up as well, e.g. game engine, packages, version control.
  • Build and playtest frequently after an initial MVP, to debug possible bugs and/or technical limitations from deployment.

Example shot and asset lists

External packages/plugins

FMOD for Unity

FMOD is a game audio integration tool. It is relatively easy and intuitive to pick up given its DAW-like interface. FMOD integration in Unity is pretty seamless, since the concepts are essentially the same: audio emitters and listeners are attached to game objects, and can be controlled programmatically.

The strength of FMOD lies in offloading audio automation logic to the audio engineer, rather than doing all these manually in Unity, to achieve adaptive music. This includes effects, fades, transitions, asynchronous playback, and spatialization. The basic FMOD interface for audio playback on the Unity side are:

  • Start and stop (with optional fade out if AHDSR modulator added for track)
  • Music control parameters exposed by the audio engineer.

All the game devs need to know is what knobs can be tweaked on the FMOD event in Unity, e.g. "play the main battle music now upon event trigger, with an intensity value of medium". Corresponding automation logic in FMOD may include:

  • Gradual un-muting of background effects (seek speed)
  • Send music through auxiliary sound effects/filters (effects)
  • Trigger transition only at predetermined transition points (transition markers)
  • Insert small transition music during crossfading (transition regions)
  • Overlapping of concurrent auxiliary music stems for continuity depending on an intensity parameter (automation)

This significantly reduces development burden.

The composer working on FMOD will build the tracks, and push these to the music repo (the whole project does not need to be committed, look up the corresponding gitignore). Unity developers only need to perform initial linking to these built assets during the FMOD for Unity setup window, pointing to the "Build/" assets folder, before invoking the FMOD Unity package to interface with these tracks.

Sample code for managing a music track

Downsides do exist, mainly that FMOD Audio and the Built-In Audio are not compatible. This may pose an issue with libraries that were written only with the built-in audio in mind, e.g. VideoPlayer scripts do not support emitting FMOD-compatible audio events.

kb/tools/unity/start.txt · Last modified: 7 weeks ago ( 8 October 2024) by justin