Contents

1
.
Brief Introduction to Unity
2
.
Hands-on Demonstrations
2
.
1
.
Wrecking Ball Simulation
2
.
2
.
Playable Piano
2
.
3
.
Polyhedron Dodger Game

What is Unity?

Unity is a world-leading game engine using by millions of developers.
Supports both 2D and 3D games for a large variety of platforms.

What does Unity Provide?

Industry Standard Physics Engine

Unity uses PhysX for its physics engine. PhysX is a scalable multi-platform real-time physics solution supporting a wide range of devices, from smartphones to high-end multicore CPUs and GPUs. Unity supports a wide range of physical colliders, joints and other components, both in 2D and 3D.

Lighting & Rendering

Unity provides options for both performant and high fidelity rendering. Many rendering effects run at real-time, but more complex lighting calculations can be baked to avoid running heavy calculations at runtime. Their more recent shader pipelines support shader authoring through a graph GUI.

Extensive Built-In Utilities

Unity also provides numerous tools for tasks such as terrain generation and navigation. In addition to the built-in tools, Unity provides a simple package system that allows users to import both Unity-made standard assets or user-made assets from the AssetStore.

What does the WL Provide?

Scripting Environment

The Wolfram Language provides a unique powerful scripting environment for the Unity engine and the Unity editor. A Unity project can be created, launched, modified, monitored, and built – all from within a single Wolfram notebook. Users may also run Unity as a background process and use a notebook as the sole interface.

Content Creation

The Wolfram Language allows for the easy creation of synthesized audio, colorful visuals and procedural geometric content. Wolfram Language users also have access to the Wolfram Knowledgebase – an extensive repository of curated data, including 3D models and textures. The Wolfram Language also contains numerous functions for processing this content and exporting it into all common file formats.

Introduction to Unity

Before diving into Unity, let’s quickly review the four (4) key topics in Unity.

Scenes

◼
  • The 3D environment which contains everything you see from UI to 3D objects (terrain, buildings, players, etc.).
  • Game Objects

    ◼
  • Exists within a single scene.
  • ◼
  • Act as containers for components, which implement the real functionality.
  • ◼
  • Everything in a scene is either a game object or attached to one.
  • Game Object Components

    ◼
  • Control lighting, physics, audio, rendering, and more.
  • ◼
  • Must be attached to game objects.
  • Assets

    ◼
  • Any file that can be used within your game.
  • ◼
  • Exist within the Assets folder of your project.
  • ◼
  • Can be referenced by components in one or more scenes.
  • ◼
  • Examples: 3D models, audio files, images
  • Introduction to Unity II

    In the Unity Editor, these can be found in the following areas.

    Creating a Unity Project

    1. Download Unity Hub from the official Unity website.
    2. Choose a version of the Unity Editor to install.
    3. Once installed, load the UnityLink paclet.
    4. Create a new Unity project.

    Demo - Wrecking Ball

    In this first demo, we will create a wall of cubes to be knocked down by a swinging wrecking ball. This will require:
    ◼
  • Creating scenes, game objects, and assets.
  • ◼
  • Adding and modifying components.
  • ◼
  • Using Unity’s physics engine (PhysX).
  • Falling Cube Test

    Let’s start with something simple – making a cube fall.
    1. Create a cube game object.
    2. Show the default components on Unity’s cube primitive.
    3. Apply physics to the cube using a Rigidbody component.
    4. Create a plane for the cube to land on.
    5. Move and rotate the cube so it sits above the plane.
    6. Switch the Unity Editor to Play mode.
    7. Exit Play mode.

    Organizing the Project

    Before going any further, it’s good practice to keep your Unity project organized.
    1. Create some folders in the Assets directory.
    2. Create a new scene for the wrecking ball demo.
    Now all of our changes can be saved to this scene file.
    3. Create some Material assets. Materials are used to determine the appearance of rendered game objects (similar to graphics directives in the WL).

    Creating a Prefab

    Our goal is to build a wall of identical cube game objects. We could do this by copying a single game object in the scene.
    However, a better solution is to create a Prefab, which allows us to store a game object as an asset. Once the prefab is created, we can easy create instances of it in our scene.
    1. Create another default cube game object.
    2. Add a Rigidbody component.
    3. Assign the blue material to the cube.
    4. Create the prefab asset from the game object.
    5. Delete the original game object.

    Constructing a Wall

    1. Generate the positions of the cubes.
    2. Create a wall game object to act as the parent for all the cubes.
    3. Instantiate the Cube prefab for each position.
    4. Create a plane for the wall to rest on.

    Wrecking Ball

    1. Create a Unity mesh asset from the “Spikey” polyhedron.
    2. Create the wrecking ball game object using the spikey.
    3. Add a Rigidbody component with significant mass.
    4. Add a Hinge Joint component to swing the wrecking ball.
    5. Create a cylinder to act as a support pole.
    6. Apply materials.

    Wall Destruction

    1. Adjust the camera to give us a better view of the destruction.
    2. Save our progress.
    3. Start the simulation.

    Demo - Piano

    In this demo, we will create a playable piano. This will require:
    ◼
  • Importing audio assets into Unity.
  • ◼
  • Adding custom scripts to game objects.
  • Scene Setup

    1. Create a new scene for our piano.
    2. Adjust the camera.
    3. Adjust the light.
    4. Prepare some material assets for the keys.

    Interactivity with Scripts

    In Unity, we can write custom components using scripts. Each script defines a C# class that let’s us add unique behaviors to our game objects.
    I have written a script below called PianoKey, which checks if the player clicked on its attached game object. If so, it will play an audio clip and a short animation.
    1. Manually add the script to our project.
    2. Force the Unity scripts in our scene to recompile.*
    * This is only necessary if you don’t bring focus back to Unity before adding the script.

    Anatomy of a Piano

    The modern piano has a total of 88 keys, 52 of which are white and are used to play the natural notes (A, B, C, D, E, F and G). The remaining 36 keys are black and are used to play the accidentals (A♯/B♭, C♯/D♭, D♯/E♭, F♯/G♭ and G♯/A♭).
    The notes can be further divided into octaves, each of which contains 12 keys. Two keys with the same note but in different octaves will have different pitches.
    For this demo, we’re only going to recreate the C major scale for simplicity.
    To generate the notes, we can simply use the SoundNote function.
    We can get all the natural notes in the 4th octave by appending “4” to the note names.
    We can then do the same for the accidentals.

    Audio Assets

    In order to play audio in our game, we need to generate Audio Clip assets. We can import Audio objects directly from the WL into Unity.
    1. Define the notes we want to generate.
    2. Create an Audio Clip asset for each note.

    Key Mesh Assets

    The piano keys could be built from cube primitives, however I found that they’re simple enough to specify manually.
    1. Define the dimensions of the keys.
    2. Generate the base shape for each key type.
    3. Extrude the key shapes to create mesh regions.
    4. Create a Unity Mesh asset for each key mesh.

    Putting it All Together

    Now that we’ve generated all the necessary assets, we can combine them to make our piano.
    To save time, I’ve defined the relevant properties for each key in two lists, which will allow us to generate all the keys at once.
    1. For each key, specify the computer keyboard key it corresponds to, the musical note it should play, and the index of the mesh it should use.
    2. Create a parent game object to organize all the keys.
    3. Iterate to generate all the white key game objects.
    4. Similarly generate the black keys.
    5. Assign the materials we created earlier.

    Play the Piano

    1. Save our scene for good measure.
    2. Play the piano.

    Demo - Polyhedron Dodger

    In this demo, we will create a simple game where the player tries to dodge large polyhedra falling from the sky.

    Scene Setup

    1. Create a new scene.
    2. Create a floor game object.
    3. Adjust the camera.
    4. Adjust the light.

    Custom Scripts

    Once again, we’ll be using scripts to add custom behaviors to our game objects.

    PlayerController

    The PlayerController script does two main tasks:
    ◼
  • Apply physical forces to the player rigidbody to move it using WASD controls.
  • ◼
  • Checks if the player lost, and if so displays a game over message.
  • FallingObject

    The FallingObject script also has two main tasks:
    ◼
  • Reset the object to a random position in the sky when it falls too low.
  • ◼
  • Check if it collided with the player.
  • Create the PlayerController and FallingObject script assets.

    Create the Player

    1. Find the Spikey mesh asset we created earlier.
    2. Create the player game object with the mesh.
    3. Adjust the size and position of the player.
    4. Add a Rigidbody component.
    5. Add the PlayerController script.
    6. Update the material.
    7. Set the tag so the falling objects know when they collide with the player.

    Polyhedron Objects

    1. Generate some polyhedrons from PolyhedronData.
    2. Create a Mesh asset for each polyhedron.
    3. Create the first falling object.
    4. Update its material and renderer.
    5. Add a Rigidbody.
    6. Add the FallingObject script.
    7. Automatically generate game objects for the remaining polyhedrons, using the first one as a template.

    Play the Game

    1. Save the scene.
    2. Play the game.
    3. Build the game for my platform (MacOS).
    Example of Piano WebGL build:
    https://blog.wolfram.com/2019/06/06/how-i-built-a-virtual-piano-with-the-wolfram-language-and-the-unity-game-engine/

    Thank You!

    Learn More About UnityLink

    Documentation
    ​https://reference.wolfram.com/language/UnityLink/guide/UnityLink.html
    Overview & Examples
    ​https://www.wolfram.com/language/12/built-in-interface-to-unity-game-engine/index.html?product=language

    Previous Talks on UnityLink

    https://www.youtube.com/watch?v=9wDoxSs68P0
    https://www.youtube.com/watch?v=Z_Tz95939Vw

    Questions?

    Library

    Scripts

    CITE THIS NOTEBOOK

    Build your first game in the Wolfram Language with Unity game engine​
    by Alec Shedelbower​
    Wolfram Community, STAFF PICKS, May 18 2023
    ​https://community.wolfram.com/groups/-/m/t/2921593