Magic Leap (Unity on a Mac) by Example: Part 1

John Tucker
Coinmonks
3 min readJun 23, 2018

--

We begin this series with a simple example that interacts with the Magic Leap controller.

The full examples in this series are available to download.

Update: Wrote this in Lumin SDK 0.14.0 earlier this week, but just upgraded to 0.15.0. While there were some breaking changes, the performance of the simulator is night and day better (pretty much usable now). Updated article with changes.

Setup

First, this series assumes that you have built and run the Magic Leap Hello Cube example. You also may find the article helpful Unboxing Magic Leap Development (Unity on a Mac).

One additional setup step is to import the Magic Leap Unity Package into your Unity project using the menu:

Assets > Import Package > Custom Package

finding the file:

~ / Magic Leap / tools / unity / v0.15.0 / MagicLeap-0.15.0.unitypackage

This package contains the required files to use the Magic Leap Unity API available under the UnityEngine.Experimental.XR.MagicLeap namespace. It also contains a number of examples.

Also, the Unity API Documentation (reference) is available by opening the following file in a web browser (is also available from the Magic Leap portal).

~ / MagicLeap / tools / unity / v0.15.0 / api_docs / html / index.html

Control

This first example demonstrates interacting with the Magic Leap controller (or a mobile application that doubles as one); specifically it colors the cube (from the Hello Cube example) blue while the controller’s bumper is pressed.

note: You can simulate pressing the controller’s bumper using the Properties / Configuration panel; use the Properties tab and Input Controller section.

note: This example, is a simplified version of an example provided by Magic Leap on this topic.

Assets/src/control/Cube.cs

Observations

  • This script is attached as a script component of the Cube game object.
  • The MLInput.Start and MLInput.Stop are to be called in the MonoBehavior Awake and OnDestroy methods of any classes interacting with the controller.
  • We disable the entire script should MLInput.Start fail.
  • Because the controller can be disconnected, we need to check for this conditions before attempting to interact with the controller (thus the guard-clause at the top of the Update method).
  • The private variable _active is used to trigger updates only when the button (either Bumper or Move) transitions from active to inactive (or visa-versa).
  • The private variable _first is used to initialize _active on the first update.

Next Steps

While we have only touched upon the controller’s features, we will move onto gesture input in the next article Magic Leap (Unity on a Mac) by Example: Part 2.

--

--

John Tucker
Coinmonks

Broad infrastructure, development, and soft-skill background