Unity Guide
The JellyFaaS SDK is capable of working in every environment which has support for .Net Standard 2.0. This means that the Unity game engine and projects developed with it can also take advantage of the features provided by the JellyFaaS SDK.
Supported versions
Only the versions of Unity which have support for .Net Standard 2.0 are capable of making use of the SDK. This means that versions of Unity prior to Unity 2018.1 cannot be used with the SDK.
Here is a walkthrough using the joesjokes
function and Unity 2021.3.38f1:
1. Create a new project or open an existing project
First we have to create or open up the project we want to add the SDK to with a modern version of Unity.
.Net compilation targets
The Unity game engine can compile C# code with several versions of .Net, and can also pre-compile your C# code into C++ code. All of these targets have been confirmed to work, and the JellyFaaS SDK does work with pre-compiled projects.
In this guide we will use a simple Unity project with only a button and a text label which looks like this:
2. Add the JellyFaaS SDK to the project
Now that you have created your project we should add the JellyFaaS SDK to it. As Unity does not have support for the NuGet package manager we will have to manually download and install the SDK as a managed plugin.
Go to This Link and download the zip file with the SDK. Once you have the zip file with the plugin you should create a new folder called Plugins
in your project's main folder if it doesn't exist already.
The zip file with the plugin should contain a single folder with multiple .dll files. Extract this folder into the Plugins
folder we just created in your Unity project. Now when you go back to Unity it should detect that a new plugin has been added, and it should begin to re-build your project.
3. Using the JellyFaaS SDK
After you have successfully installed the JellyFaaS SDK in your project you should be able to access the functions and classes provided by the SDK through the Jellyfaas
namespace in your C# files. This means that now we can start using it.
First we should create a new script, and attach it to the button. You should then open the file in the C# IDE of your choice.
Now that we have our script file we should import the SDK into it. We can do this by adding the following line at the top of our script:
Now inside the class responsible for our button we should define some variables. First lets define the variable responsible for storing our JellyClient object which will access our JellyFaaS functions.
Lets also define a property which will store a reference to our label node we want to put the joke in.
Now we should initialize our JellyFaaS client in our Start
function. If you used Unity's templates when creating the script this function should have been provided for you. We can do this with the following line of code:
The above line initializes our JellyClient
with a JellyConfig
object. This JellyConfig
object will store our JellyFaaS Api key which we have to provide when constructing it. We may also provide additional flags to our client through this object.
In this guide we will be using the joesjokes
function. You can learn more about it Here. This page will also generate you the class which will match the output of the joesjokes
function. This is important, as it allows us to simply copy and paste the code from the website into our script instead of writing the class ourselves. We should copy and paste the C# code from the website to somewhere in our script. The code will look something like this:
Finally we can call our JellyFaaS function. We can easily do this by creating a new function which we can later attach to our button:
Inside this function we should first look up the joesjokes
function with our JellyClient
, and then call it. We should then store the resulting joke in a variable like the following code does:
We can also rewrite this code to fit inside a single line like this:
The invoke call in above takes in a type as a generic argument. In C# generic arguments have to be put inside angle brackets. The invoke function will create a new object of this type, and put the response from the JellyFaaS function inside it. Any fields and properties which do not match the values needed by the function will be discarded, which is why we should use the class we generated earlier here.
The last part of code we need to write inside the OnClick
function will display the joke inside our label. The joesjokes
function returns the joke and the punchline separately, meaning that we will have to combine them for the joke to make sense. This can be done with the following code:
This code will put the punchline of the joke two lines below the joke, making it easy to read.
4. Final steps
Now that we have our script, we can go back to the Unity Editor. In the editor we should go to our button's properties, and find the On Click ()
section. In this section we should click on the +
button at the bottom.
Pressing this button should add a new event listener to the On Click ()
section. In this event listener we should drag and drop our button into the object selection box, and in the dropdown menu next to it we should select our OnClick
function we created earlier. This should make it so whenever we press the button the OnClick
function will run.
Finally we should drag and drop the label we want to display our joke on into the Output Label
property in our script component section in the button.
Now we can run the project! Press the run button in the editor to run and see the results of our work.
After pressing the button we can see that the function has been called, and has provided us with a randomly generated joke. Pressing the button multiple times should give us a new joke after every press.
Related Links
- JellyFaaS SDK Unity plugin download link
- Github repository with the example project from this guide
- C# JellyFaaS SDK Reference
The code we have wrote
using System.Collections;
using System.Collections.Generic;
using TMPro;
using UnityEngine;
using Jellyfaas;
using System.Text.Json.Serialization;
public class JokeButton : MonoBehaviour
{
// Export this property so we can set the text label we want to put the joke in from the editor
public TMP_Text OutputLabel;
// Store the JellyFaaS client so we don't have to remake it every time we call a function
private JellyClient client;
// Start is called before the first frame update
void Start()
{
client = new JellyClient(new JellyConfig("Insert your Api Key here")); // < -- Insert your Secret key here
}
public void OnClick() {
var joke_info = client.Lookup("joesjokes").Invoke<JoesJoke>(); // The joesjokes function does not take any parameters, and returns information on a randomly generated joke
OutputLabel.text = joke_info.joke + "\n\n" + joke_info.Punchline;
}
// The class which matches the definition of the Json object returned by the joesjokes function
// The joesjokes function returns a json object with two strings called "joke" and "punchline".
public class JoesJoke
{
// As long as the property name matches the Json property the SDK will read the data into the matching properties
public string joke { get; set; }
// Json property names are case sensitive, meaning that this property does not match
// Fortunately, the JellyFaaS SDK uses the System.Text.Json library when deserializing Json objects, meaning that you can use any attributes provided by it
// The following attribute tells the SDK to assume this property is called "punchline" when translating to and from Json.
[JsonPropertyName("punchline")]
public string Punchline { get; set; }
}
}