|
| 1 | +# UniSecret |
| 2 | + |
| 3 | +UniSecret is a Unity package that allows developers to quickly integrate their game with the Secret Network blockchain, which in turn provides access to all benefits known from web-based DApps, but in a video game format. |
| 4 | + |
| 5 | +# Third Party code |
| 6 | + |
| 7 | +This package uses a modified version of the [SecretNET client library](https://github.com/0xxCodemonkey/SecretNET) |
| 8 | + |
| 9 | +The following changes had to be made to the library to get it to work with Unity: |
| 10 | + |
| 11 | +* Target framework changed from .NET 6 to .NET Standard 2.1 |
| 12 | +* Removed dependency on MAUI |
| 13 | +* Made the library portable and compile-able for all platforms |
| 14 | +* Rewrote parts of the code using .NET 6 features |
| 15 | +* Added hooks for injecting custom behavior from executing assemblies (Unity game in this case) to allow for custom behaviors and handlers for gRPC-web client... ... ... basically made it ***work with Unity WebGL builds*** |
| 16 | + |
| 17 | +# Requirements |
| 18 | + |
| 19 | +This package requires at least ***Unity 2022.3.1 LTS*** |
| 20 | + |
| 21 | +Additionally, your project *needs to* reference the following libraries and packages: |
| 22 | + |
| 23 | +* Newtonsoft.Json for Unity |
| 24 | +* TextMeshPro |
| 25 | +* [UnityGrpcWeb](https://openupm.com/packages/ai.transforms.unity-grpc-web/) |
| 26 | + |
| 27 | +# Installation |
| 28 | + |
| 29 | +You can install this package through the Unity Package manager (`Install from Git` option). |
| 30 | + |
| 31 | +# Integration & Setup |
| 32 | + |
| 33 | +UniSecret makes it as simple as possible to get started with the Secret Network integration. A `Secret Loader` prefab is provided out of the box which can be dragged into any scene to turn it into a wallet set-up scene. |
| 34 | + |
| 35 | +Alternatively, for quick start a fully constructed wallet scene is provided in the `Scenes/` folder. Adding this scene to the build with index `0` will ensure that the user will only get around to the actual gameplay after connecting their wallet to the game. |
| 36 | + |
| 37 | +***Note:*** The `Secret Loader` component has an inspector property that you need to adjust for everything to work. The `Post Init Scene` property has to be set to the name of your game's "first" scene - be it a splash screen, main menu or anything else. |
| 38 | + |
| 39 | +# Usage |
| 40 | + |
| 41 | +UniSecret provides most of the functionality through public API exposed through the `SecretLoader` class. You can get an instance of the class in your code by simple calling: |
| 42 | + |
| 43 | +```cs |
| 44 | +SecretLoader secret = SecretLoader.Instance; |
| 45 | +``` |
| 46 | + |
| 47 | +## Getting the Signer wallet info |
| 48 | + |
| 49 | +From there, you can easily access the connected wallet through the `Signer` property: |
| 50 | + |
| 51 | +```cs |
| 52 | +Debug.Log(secret.Signer.Address); |
| 53 | +``` |
| 54 | + |
| 55 | +## Querying the Blockchain |
| 56 | + |
| 57 | +Additionally, you are able to query all blockchain data with the `Queries` property: |
| 58 | + |
| 59 | +```cs |
| 60 | +var myScrtBalance = await this.Queries.Bank.Balance( |
| 61 | + new Cosmos.Bank.V1Beta1.QueryBalanceRequest() |
| 62 | + { |
| 63 | + Address = "...", |
| 64 | + Denom = "uscrt" |
| 65 | + } |
| 66 | +); |
| 67 | +``` |
| 68 | + |
| 69 | +## Smart Contract Query Short-hand |
| 70 | + |
| 71 | +There is also a shorthand method available for querying contract state directly from `SecretLoader`: |
| 72 | + |
| 73 | +```cs |
| 74 | +var welcomePack = await secretLoader.QueryContractState<WelcomePackQuery>( |
| 75 | + "secret1zag3hdz0e0aqnw9450dawg7j6j56uww8xxhqrn", |
| 76 | + new |
| 77 | + { |
| 78 | + qualified_for_welcome_pack = new |
| 79 | + { |
| 80 | + address = secretLoader.Signer.Address |
| 81 | + } |
| 82 | + } |
| 83 | +); |
| 84 | +Debug.Log(welcomePack.RawResponse); |
| 85 | +welcomePackRewardsButton.enabled = welcomePack.Response.Qualified; |
| 86 | +``` |
| 87 | + |
| 88 | +## Broadcasting Transactions |
| 89 | + |
| 90 | +When it comes to sending transactions, `SecretLoader` exposes a method that takes in an array of messages and an optional callback `onComplete` that gets called on a successful execution of a transaction (after it is committed): |
| 91 | + |
| 92 | +```cs |
| 93 | +await secretLoader.SignTransaction( |
| 94 | + new[] { |
| 95 | + new MsgExecuteContract( |
| 96 | + "secret1zag3hdz0e0aqnw9450dawg7j6j56uww8xxhqrn", |
| 97 | + new { receive_welcome_pack = new { } }) |
| 98 | + { |
| 99 | + Sender = secretLoader.Signer.Address |
| 100 | + } |
| 101 | + } |
| 102 | +); |
| 103 | +``` |
| 104 | + |
| 105 | +The transaction will automatically be forwarded to the user in the form of a dialog box with options to accept or deny the call. |
| 106 | + |
| 107 | +# Current State |
| 108 | + |
| 109 | +***This library is in very early stages of development and is by no means production ready. It is an early proof of concept. Any feedback and contributions are welcome.*** |
0 commit comments