Unity SDK
The Openfort SDK for Unity
- Windows (64-bit)
- macOS (minimum version 12.5)
- Android (minimum version 5.1)
- iOS (minimum version 15.2)
- Unity 2021.3 or newer for Windows, macOS, Android and iOS
- Unity 2019.4 or newer for macOS, Android, and iOS. Windows isn't supported on Unity versions from 2019.4 up through 2021.2.
We have added compilation flags to the Unity SDK to ensure that specific Unity editors can only build certain platform targets. Please note that the table below indicates which editor you can use to build a platform target, but it does not determine whether you can run the SDK in that editor.
For example, the SDK allows you to build iOS games using a macOS Unity Editor, but you cannot use the Windows Unity Editor.
Target Platform | Windows Unity Editor | macOS Unity Editor |
---|---|---|
Windows | ✅ | ❌ |
macOS | ❌ | ✅ |
Android | ✅ | ✅ |
iOS | ❌ | ✅ |
Installation#
The Unity SDK requires UniTask package (version 2.3.3) as specified in package.json. How to install UniTask: Follow these instructions. Please check the Unity requirements to ensure a successful integration.
Since .dll files are stored on Git Large File Storage, you must download and install git-lfs from here.
- Open the Package Manager
- Click the add + button and select "Add package from git URL..." Enter https://github.com/openfort-xyz/openfort-csharp-unity.git?path=/src/Packages/OpenfortSDK and click 'Add'
Quickstart#
Initialize OpenfortSDK#
_13using Openfort.OpenfortSDK;_13using Openfort.OpenfortSDK.Model;_13_13public class openfortManager: MonoBehaviour {_13 private OpenfortSDK openfort;_13 const string PublishableKey = "YOUR_OPENFORT_PUBLISHABLE_KEY";_13 const string ShieldApiKey = "YOUR_SHIELD_PUBLISHABLE_KEY";_13 const string ShieldEncryptionShare = "YOUR_SHIELD_ENC_SHARE";_13 private async void Start()_13 {_13 openfort = await OpenfortSDK.Init(PublishableKey, ShieldApiKey, ShieldEncryptionShare);_13 }_13}
Authentication Methods#
1. Email and Password#
_10await openfort.LogInWithEmailPassword(email, password);
2. OAuth (e.g., Google)#
_13OAuthInitRequest request = new OAuthInitRequest()_13{_13 Provider = OAuthProvider.Google,_13 UsePooling = true, // For WebGL or standalone Windows_13 // For other platforms:_13 // UsePooling = false,_13 // Options = new OAuthInitRequestOptions()_13 // {_13 // RedirectTo = "mygame://callback"_13 // },_13};_13_13var response = await openfort.AuthenticateWithOAuth();
3. Sign-In with Ethereum (SIWE)#
_10string address = "0x..."; // User's Ethereum address_10string signature = "0x..."; // Signature from the user's wallet_10await openfort.InitSiwe(address);_10var response = await openfort.AuthenticateWithSiwe(signature);
Stored Credentials#
_10bool hasCredsSaved = await openfort.GetAccessToken() != null;_10if (hasCredsSaved)_10{_10 await openfort.ValidateAndRefreshToken(new ValidateAndRefreshTokenRequest());_10 // Successfully re-logged into OpenfortSDK_10}
Log Out#
_10await openfort.Logout();
Advanced Features#
Sign Message#
_10SignMessageRequest signMessageRequest = new SignMessageRequest("Hello World!");_10string signature = await openfort.SignMessage(signMessageRequest);
Sign Typed Data#
_19var domain = new TypedDataDomain(_19 name: "Openfort",_19 version: "0.5",_19 chainId: 80002,_19 verifyingContract: "0x9b5AB198e042fCF795E4a0Fa4269764A4E8037D2"_19);_19_19var types = new Dictionary<string, List<TypedDataField>>_19{_19 // Define your types here_19};_19_19var message = new Dictionary<string, object>_19{_19 // Define your message here_19};_19_19var signTypedDataRequest = new SignTypedDataRequest(domain, types, message);_19string signature = await openfort.SignTypedData(signTypedDataRequest);
Send Transaction#
_23// 1. Request the encoded transaction from your backend_23var webRequest = UnityWebRequest.PostWwwForm("https://your-backend-url.com/mint", "");_23webRequest.SetRequestHeader("Authorization", "Bearer " + accessToken);_23webRequest.SetRequestHeader("Content-Type", "application/json");_23webRequest.SetRequestHeader("Accept", "application/json");_23await SendWebRequestAsync(webRequest);_23_23if (webRequest.result != UnityWebRequest.Result.Success)_23{_23 Debug.Log("Mint Failed: " + webRequest.error);_23 return;_23}_23_23// 2. Parse the response from your backend_23var responseText = webRequest.downloadHandler.text;_23var responseJson = JsonConvert.DeserializeObject<RootObject>(responseText);_23_23// 3. Create and send the SignatureTransactionIntentRequest_23SignatureTransactionIntentRequest request = new SignatureTransactionIntentRequest(responseJson.transactionIntentId, responseJson.userOperationHash);_23TransactionIntentResponse intentResponse = await openfort.SendSignatureTransactionIntentRequest(request);_23_23// 4. Get the transaction hash from the response_23string transactionHash = intentResponse.Response.TransactionHash;
Note: Obtaining the transaction hash does not guarantee a successful transaction. Use an appropriate method to check the transaction receipt for the final status.
Supported Functionality#
The SDK supports a wide range of methods, including user authentication, wallet management, transaction signing, and more.
Method | Functionality |
---|---|
Init | Initializes OpenfortSDK with various parameters |
LogInWithEmailPassword | Logs the user in using email and password |
SignUpWithEmailPassword | Signs up a new user with email and password |
LinkEmailPassword | Links an email and password to the user account |
UnlinkEmailPassword | Unlinks an email and password from the user account |
RequestResetPassword | Requests a password reset |
ResetPassword | Resets the user's password |
RequestEmailVerification | Requests an email verification |
VerifyEmail | Verifies the user's email |
InitOAuth | Initializes OAuth for the user |
AuthenticateWithOAuth | Authenticates the user with OAuth |
UnlinkOAuth | Unlinks OAuth from the user account |
PoolOAuth | Pools OAuth for the user account |
InitLinkOAuth | Initializes the link OAuth process |
AuthenticateWithThirdPartyProvider | Authenticates the user with a third-party provider |
InitSiwe | Initializes Sign-In with Ethereum (SIWE) |
AuthenticateWithSiwe | Authenticates the user with SIWE |
LinkWallet | Links a wallet to the user account |
UnlinkWallet | Unlinks a wallet from the user account |
StoreCredentials | Stores authentication credentials |
GetUser | Gets the user information |
Logout | Logs the user out and removes stored credentials |
GetAccessToken | Gets the currently saved access token |
ValidateAndRefreshToken | Validates and refreshes the access token |
SendSignatureTransactionIntentRequest | Sends a signature transaction intent request |
SignMessage | Signs a message |
SignTypedData | Signs typed data |
SendSignatureSessionRequest | Sends a transaction signed by a session |
GetEmbeddedState | Gets the embedded state |
GetEthereumProvider | Gets the Ethereum provider |
ConfigureEmbeddedSigner | Configures the embedded signer |
Examples#
- Sample App: Demonstrates how to use the Openfort Unity SDK.
- Sample Game: A tutorial on integrating the Openfort Unity SDK into a game.