Experimental integration Glean with Unity applications
You might notice Firefox Reality PC Preview has been released in HTC’s Viveport store. That is a VR web browser that provides 2D overlay browsing alongside immersive content and supports web-based immersive experiences for PC-connected VR headsets. In order to easily deploy our product into the Viveport store, we take advantage of Unity to help make our application launcher. Also because of that, it brings us another challenge about how to use Mozilla’s existing telemetry system.
As we know, Glean SDK has provided language bindings for different programming language requirements that include Kotlin, Swift, and Python. However, when we are talking about supporting applications that use Unity as their development toolkit, there are no existing bindings available to help us achieve it. Unity allows users using a Python interpreter to embed Python scripts in a Unity project; however, due to Unity’s technology being based on the Mono framework, that is not the same as our familiar Python runtime for running Python scripts. So, the alternative way we need to find out is how to run Python on .Net Framework or exactly on Mono framework. If we are discussing possible approaches to run Python script in the main process, using IronPython is the only solution. However, it is only available for Python 2.7, and the Glean SDK Python language binding needs Python 3.6. Hence, we start our plans to develop a new Glean binding for C#.
How it works
Defining pings and metrics
Before we start to write our program, let’s design our metrics first. Based on the current ability of Glean SDK C# language binding, we can create a custom ping and set a string type metric for this custom ping. Then, at the end of the program, we will submit this custom ping, this string metric would be collected and uploaded to our data server. The ping and metric description files are as below:
Testing and verifying it
Now, it is time for us to write our HelloWorld program.
As we can see, the code above is very straightforward. Although Glean
parser in C# binding hasn’t been supported to assist us create metrics,
we are still able to create these metrics in our program manually. One
thing you might notice is
Thread.Sleep(1000); at the bottom part of the main().
It means pausing the current thread for 1000 milliseconds. Because this
HelloWorld program is a console application, the application will quit
once there are no other operations. We need this line to make sure the
Glean ping uploader has enough time to finish its ping uploading before
the main process is quit. We also can choose to replace it with
to let users quit the application instead of closing the application
itself directly. The better solution here is to launch a separate
process for Glean uploader; this is the current limitation of our C#
binding implementation. We will work on this in the future. Regarding
the extended code you can go to glean/samples/csharp to see other pieces, we also will use the same code at the Unity section.
After that, we would be interested in seeing if our pings are
uploaded to a data server successfully. We can set the Glean debug view
environment variable by inserting
set GLEAN_DEBUG_VIEW_TAG=samplepings in our command-line tool,
could be any you would like to be as a tag of pings. Then, run your
executable file in the same command-line window. Finally, you can see
this result from Glean Debug Ping Viewer , the result is as below:
Environment.OSVersionto get the OS version, and it is not reliable. The follow up discussion please reference Bug 1653897. The solution is by adding a manifest file, then unmarking the lines of Windows 10 compatibility.
Your first Glean program in Unity
Thread.Sleep(1000);at the end of the
start()function. It is because in general, Unity is a Window application, the main approach to exit an Unity application is by closing its window. Hence, Glean ping uploader has enough time to finish its task. However, if your application needs to use a Unity specific API,
Application.Quit(), to close the program. Please make sure to make the main thread wait for a couple of seconds in order to preserve time for Glean ping uploader to finish its jobs. For more details about this example code, please go to GleanUnity to see its source code.