This project is a Salesforce SDK for the popular game engine Unity 3d.
The SDK is written in C# and interact with the Force.com REST APIs.
It provides the following services:
- OAuth authentication
- Salesforce records query, insert, update, delete operations
- Apex remote methods calls
- Chatter feed interactions such as retrieving a feed and posting to it
This SDK is provided “as is“ without any warranty or support. Salesforce does not officially endorse it.
If you need a Salesforce Org, you can sign up for a free Salesforce Developer Edition (DE) organization.
- Log in to your Salesforce org.
- At the top right of the page, select the gear icon and then click Setup.
- From Setup, enter
App Manager
in the Quick Find and select App Manager. - Click New Connected App.
- Enter
Unity 3d API
as the Connected App Name - Enter your Contact Email.
- Under API (Enable OAuth Settings), check the Enable OAuth Settings checkbox.
- Enter
https://localhost/
as the Callback URL. - Under Selected OAuth Scope, move Access and manage your data (API) to the Selected OAuth Scopes list.
- Click Save.
- From this screen, copy the connected app’s Consumer Key and Consumer Secret some place temporarily.
- Copy the
src
folder from the SDK to theAssets
folder of your Unity project - Create an empty
GameObject
and give it a descriptive name likeSalesforce client
- Drag and drop the
SalesforceClient
script that's inAssets/src/sfdc-client
on the newGameObject
- Configure the client script with the Consumer Key and Consumer Secret you obtained earlier.
If you want to test the client, follow these extra instructions:
- Add the
Assets/src/sample/TestSalesforceClient
script on the same object that contains theSalesforceClient
script - Configure the test script with your Salesforce credentials
- Start the game and watch the Unity console
If you do not want to use the sample code, you can safely remove the Assets/src/sample
directory.
The TestSalesforceClient
class highlights the following:
- how to log in to Salesforce and how to diagnose client configuration errors
- how to perform basic CRUD operations on Salesforce records (Case object in this sample).
Note that all Salesforce Client operations require that you log in to Salesforce beforehand.
In order to support any standard or custom Salesforce objects, you must create a class per object.
This class must inherit from SalesforceRecord
. See the Case
class for example.
Assuming that you have deployed the following Apex class in Salesforce:
@RestResource(urlMapping='/CustomRestApi/*')
global class CustomRestApi {
@HttpPost
global static string sayHello(String name) {
return 'Hello '+ name;
}
}
You can call the custom Apex REST methods by using the following code:
Coroutine<String> routine = this.StartCoroutine<String>(
sfdcClient.runApex("POST", "CustomRestApi", "{\"name\": \"world\"}", "")
);
yield return routine.coroutine;
string result = routine.getValue(); // Hello world
JSONObject
This project relies on JSONObject 1.4, a simple C# JSON parser.
Copyright (C) 2012 Boomlagoon Ltd.
Coroutine extension
As a convenience, this project includes a utility class that provide Coroutines with supports for return values and exceptions.
It is important to always call the getValue()
method even if there is no expected result as this triggers exceptions.
Coroutine<bool> routine = this.StartCoroutine<bool>(
sfClient.login(SFDC_USERNAME, SFDC_PASSWORD)
);
yield return routine.coroutine;
try {
bool isUserLoggedIn = routine.getValue();
}
catch (Exception e) {
// Handle exception
}
Credit for this Coroutine utility goes to @horsman from Twisted Oak Studios.
Original code written by John Casimiro on 2014-01-30
Modified by Ammar Alammar
Modified by Bobby Tamburrino:
- removed PlayMaker dependencies
- added Chatter support
- added approval process handling
Modified by Philippe Ozil:
- refactored configuration to separate app/user authentication config
- switched all request to IEnumerator for more async control
- added exceptions to handle errors
- removed response from client class to allow parallel processing
- added debug mode for improved tracing
- updated project to use UnityWebRequest
- reduced complexity of CRUD operations on salesforce records (see SalesforceRecord class)