From d633e9a096f77588f83c95e5d9edf25daf684eeb Mon Sep 17 00:00:00 2001 From: Quinn Purdy <49766546+BellringerQuinn@users.noreply.github.com> Date: Fri, 12 Apr 2024 10:46:39 -0400 Subject: [PATCH] Unity/building jelly forest (#54) * Initial version of the Making of Jelly Forest guide. No images or video are included yet. * Miscellaneous changes to written content. Embedded some images and videos. Embedded additional links. Removed links that are likely to break (those with specific line numbers) that are important and replaced them with code snippets where appropriate * Removed todo for now --- docs/pages/guides/unity-guide.mdx | 360 +++++++++++++++++- .../ShopItemExtensionScreenshot.png | 3 + .../TransactionQueuer.png | 3 + docs/public/video/unity/ShopItemExtension.mp4 | 3 + 4 files changed, 364 insertions(+), 5 deletions(-) create mode 100644 docs/public/img/guides/making-of-jelly-forest/ShopItemExtensionScreenshot.png create mode 100644 docs/public/img/guides/making-of-jelly-forest/TransactionQueuer.png create mode 100644 docs/public/video/unity/ShopItemExtension.mp4 diff --git a/docs/pages/guides/unity-guide.mdx b/docs/pages/guides/unity-guide.mdx index 020f81f8c0d..71db6b71c43 100644 --- a/docs/pages/guides/unity-guide.mdx +++ b/docs/pages/guides/unity-guide.mdx @@ -1,7 +1,357 @@ -## Integrate SequenceKit with WaaS +import Video from "../../components/Video"; + +## Intro to Jelly Forest + +Jelly Forest is a blockchain-enabled 2D runner game. The game features social sign in, multi-tiered upgrades (with higher tier requiring lower tier upgrades as inputs to build/mint), and cosmetic upgrades, all of which are stored in an embedded non-custodial smart contract wallet. There are no transaction signing popups or gas fee payment requirements emposed on players. + +Learn [why smart contract wallets here](https://docs-v2.sequence.xyz/solutions/technical-references/wallet-contracts/why) + +Learn [what an embedded wallet is here](https://docs-v2.sequence.xyz/solutions/wallets/overview#what-are-the-differences-between-these-options) + +This guide will walk you through how we built Jelly Forest and how you too can build your own web3 game using [Sequence's Unity SDK](https://docs-v2.sequence.xyz/sdk/unity/overview/)! + +## Build a game loop + +Step number one is building basic game loop. Don't forget to think about your monetization strategy and how you'll be using web3 elements first! + +For our game loop, we purchased the [Infinite Runner Engine](https://assetstore.unity.com/packages/templates/systems/2d-3d-infinite-runner-engine-51328) from the Unity Asset Store. Inside the asset, we found a demo scene `JellyForest`, which, with a few tweaks, we were able to get a functional build from on iOS and Android. + +## Integrate social sign in and Sequence's Embedded Wallet solution + +### Configuration + +1. [Install Sequence's Unity SDK using package manager](https://docs-v2.sequence.xyz/sdk/unity/installation#package-manager---recommended) +2. [Sign in to the Sequence Builder Console](https://docs-v2.sequence.xyz/solutions/builder/getting-started) +3. [Create a project for your game in the Builder Console](https://docs-v2.sequence.xyz/solutions/builder/project-management) +4. Setup an [Embedded Wallet in the Builder Console](https://docs-v2.sequence.xyz/solutions/builder/in-game-wallet#embedded-wallet-in-builder) +5. In your `SequenceConfig` [scriptable object](https://docs.unity3d.com/Manual/class-ScriptableObject.html) which you imported via the Samples menu in Package Manager during the [installation stage](https://docs-v2.sequence.xyz/sdk/unity/installation#package-manager---recommended), add your Google and Apple client ids which you added to the Builder as well as your Configuration Key under `WaaSConfigKey` + - Don't forget to put your Android and iOS client ids under their respective platforms! +6. Add your [Builder API Key from the Builder Console](https://docs-v2.sequence.xyz/solutions/builder/getting-started#step-3-optional-for-development-claim-an-api-access-key) under `Settings > API Access Keys` - you want the `prod` key + +### Social Sign In + +1. Create a basic scene where you will have your players login. + - In our case, we [created a new scene](https://docs.unity3d.com/Manual/scenes-working-with.html#creating-a-new-scene) and added a background image to it +2. Create a `Canvas`, attach a `Canvas Scaler` component and use the "Scale with Screen Size" UI Scale Mode. This will make it so that the LoginPanel (and any other UI elements under this Canvas) are scaled automatically when switching between build targets. +3. Drag the `LoginPanel` prefab into your Scene Hierarchy under the Canvas. This can be found in the Project window under `Packages > Sequence WaaS SDK > SequenceExamples > Prefabs`. +4. Create a UI manager to call `Open` on the `LoginPanel`. See [our implementation](https://github.com/0xsequence/sequence-unity-demo/blob/JellyForest/Scripts/UI/LoginScreenUIManager.cs) below: + +``` +private void Start() +{ + LoginPanel loginPanel = GetComponentInChildren(); + if (loginPanel == null) + { + Debug.LogError("LoginPanel not found!"); + + loginPanel.Open(); +} +``` + +5. Break the reference to the `LoginPanel` prefab in the Hierarchy so that you can edit it freely in the scene view + 1. Select the `LoginPanel` GameObject in the Hierarchy + 2. Right click the `LoginPanel` GameObject in the Hierarchy + 3. `Prefab > Unpack Completely` +6. Customize the LoginPanel to fit your game's theme + +The LoginPanel will handle all of the social sign in logic for you. If you're curious how it's implemented, you can checkout the [LoginPage](https://github.com/0xsequence/sequence-unity/blob/master/Assets/SequenceExamples/Scripts/UI/LoginPage.cs) and [OpenIdAuthenticator](https://github.com/0xsequence/sequence-unity/blob/master/Assets/SequenceSDK/Authentication/OpenIdAuthenticator.cs) implementations. +The authentication works via the [Open ID Connect Implicit Flow](https://auth0.com/docs/authenticate/login/oidc-conformant-authentication/oidc-adoption-implicit-flow). + +### Registering a session with the Sequence API + +Once social sign in is complete, you will automatically make a register session request with the Sequence WaaS (Wallet as a Service) APIs. Here's how it works: + +When social sign in is complete, the [OpenIdAuthenticator.SignedIn](https://github.com/0xsequence/sequence-unity/blob/master/Assets/SequenceSDK/Authentication/OpenIdAuthenticator.cs#L15) event is fired. This initiates the authorization process in [WaaSLogin.ConnectToWaaS](https://github.com/0xsequence/sequence-unity/blob/master/Assets/SequenceSDK/WaaS/WaaSLogin.cs#L217). + +### Retrieving the user's wallet + +In order to retrieve the wallet, you'll need to subscribe to the `WaaSWallet.OnWaaSWalletCreated` event. + +``` +WaaSWallet.OnWaaSWalletCreated += OnWaaSWalletCreatedHandler; +public void OnWaaSWalletCreatedHandler(WaaSWallet wallet) { + // Do something +} +``` + +We highly recommend you import `SequenceConnector` via the "Useful Scripts" under Samples in the Package Manager page for the "Sequence WaaS SDK".[ By default, it contains a lot of helpful starting code](https://github.com/0xsequence/sequence-unity/blob/master/Assets/Samples~/Scripts/SequenceConnector.cs) and acts as a useful interface to communicate with the SDK. We used it quite heavily [in our integration with JellyForest](https://github.com/0xsequence/sequence-unity-demo/blob/JellyForest/Scripts/SequenceConnector.cs). + +In JellyForest, we also created a [LevelLoader](https://github.com/0xsequence/sequence-unity-demo/blob/JellyForest/Scripts/LevelLoader.cs) MonoBehaviour that loads the next scene when the `WaaSWallet.OnWaaSWalletCreated` event is fired. + +``` +private void Awake() +{ + WaaSWallet.OnWaaSWalletCreated += OnWaaSWalletCreated; +} + +private void OnWaaSWalletCreated(WaaSWallet wallet) +{ + SceneManager.LoadScene("MenuScene"); +} +``` + +For more information on how auth in Sequence's Embedded Wallet solution works, please see our [docs](https://docs-v2.sequence.xyz/solutions/wallets/embedded-wallet/overview/) and [blog post](https://sequence.xyz/blog/sequence-embedded-wallets). + ## Deploy a collectibles contract + +Now that our players can sign in and get a wallet, let's add some collectibles! + +We highly recommend using an [ERC1155](https://eips.ethereum.org/EIPS/eip-1155) contract. They are a flexibly token standard that are well suited for games. You can easily deploy our audited ERC1155 implementation via the Builder Console like this: + +