How to Extend the ChainSafe Web3 Unity SDK - HackFS 2024
ETHGlobal·Wed, Oct 9, 2024, 12:00 AM
Join Jay Albert & Nikola Garabandic of Chainsafe for a workshop titled, "How to Extend the ChainSafe Web3 Unity SDK." In this workshop, Jay and Nikola will go over how to extend the SDK to empower developers to contribute their own scenes, prefabs, and third-party integrations. This workshop is part of HackFS 2024, an async, online hackathon that takes place May 17 – June 7, 2024. ----------------- Follow us: https://x.com/ethglobal Check us out: https://ethglobal.com Email us at: hello@ethglobal.com
Transcript
[Music] hello everyone and welcome to the hacks 2024 chains safe Workshop how to extend the chains safe web through Unity SDK joining us today is Jay Albert and Nicola Ganda who will be taking us through this session and with that I'll pass over to Jay and Nicola to get the session started thanks uh so as where just mentioned we'll be going overextending our SDK for anyone who's not familiar with chains safe we're research and development firm focus on protocol level research our most notable achievements include moving ethereum from proof of work to proof of stake writing the code behind Avalanche seat chain and the file coin virtual machine amongst many things that we offer we also offer uh web 3 development tools which includes our web 3 Unity SD and web3js so today Nica will be going over our web3 Unity s and how you can uh get started with extending it so with that said I'll pass it over to niola for the presentation thank you Jan let me just quickly then share my screen hey everyone I'm Nicola garab bandic I am a gaming engineering team lead and I'll be showing you how to extend easily the chain of SDK so the first question I guess is is the SDK really that extendable and the answer to that is absolutely yes because this is possible due to the way we rebuilt the version 2.5 from the ground up where we are heavily relying on the pendency injection for building all of the core SDK functionality there are two main ways that you can extend our SDK and I'll be showcasing the first one in this demo the first one is make a new Unity package that has a dependency of the web 3. Unity SDK package and you should use this approach when the functionality itself is just a nice addition to the SDK and it isn't must have and when the third party provider has a Unity SDK and you just want to make a wrapper for it that utilizes web 3. Unity examples include that we already built our web3 o loot boxes and ramp ramp is maybe kind of different because we had to make wrappers for all of the uh Native platforms that they have built and we had to make a CP wrapper that communicates with the native code but that's topic for another session maybe the second way of extending dsdk is to make a new project directly inside of the chains save. gaming solution what is that I'll come to that in the demo and you should use this approach when it's a feature we've completely missed When developing the core solution and when it's a common feature for more than one single platform or engine like if you want to Port this to use in unity unreal or gdo you are free to update our chain.
gaming solution there are several R rules of tumb that you should uh follow but uh I guess this is not going to be super clear now but more clear after demo essentially you should always create the interface first then the concrete type you are implementing and that is very important for the dependency injection itself implementing I like cycle participant interface especially if there are some things you need to do when the web instance gets initialized or destroyed like in case of logging out for example you want to close the connection to an API that you're listening to or save the file you were reading from it and stuff like that you should implement the ioner or not implement but uh using your Constructor igner and in transaction executor to sign messages and send transactions and you should use extension methods to use features when exported the when exporting the solution now this all sounds I guess kind of abstract so uh let's uh go to the demo and see how we should actually do stuff like there the first thing that I'm going to do is go to code and download the zip what I would actually suggest if you're actually going to make the real contribution and not the demo stuff I suggest you to to Fork our Branch I would suggest even to uh Fork the dev Branch because that one is up to dat with most of the development things we've been working on including updating to the latest Unity version that is 2022 3.26 but let's say that you just want to download the zip and follow along you can start the download I already have it downloaded and let me just quickly show it so in here we have web3 Unity Main and this is the folder structure we have this chains save. gaming solution this solution is the core solution that contains all of the basic functionality the web 3 Unity SDK has like signing transactions uh sending transactions contracts all of that stuff and I'm not going to go over how to create a new project here because it takes way more time than doing it for Unity and I guess most of you are probably going to do it uh for extending the SDK in terms of like adding new uh I don't know new provider for signing or something like that so that's where you can just use the unity where you can extend Unity itself and here you can see that in the SRC folder we have the unity sample project but that is still not where our project is this is just to show you where the main solution is and where you should through where you should find the unity sample project that is going to be opened and let's go back to web3 Unity Main and in the packages folder we have the things that we were looking for we have the io. chains. web 3- Unity that's uh our core solution all of these Solutions are plugins and let me just quickly show you how do them they look for example web3 OD has editor runtime and samples with Tilda because Tilda here means that samples are not imported and in the package.
json you are actually saying where your samples are and you can import them after you import your uh package now let's quickly for example extend or create a new project for for example mud the way we advise you to do it I'm copy pasting it here just for the faster demo execution we don't need samples for this specific demo we need to delete from runtime everything and in the package that Json we are not going to call this either chains safe Unity ramp we're going to call it mud we're going to call this mud description here can be also mud the dependencies themselves can be train safe 2.5.5 samples we don't need samples per se so let's just make it like this and you you should be able then to open your Unity sample project but because I already have it open and it takes a lot of time to initialize it I am here with the unity sble project open and now what we need to do is we need to add our mud folder that or package that we have just created and we're going to click on plus and that package script uh from the dis sorry and as you can see it's here web3 un fdk mud and important thing that needs to be addressed here is to go to go back to the SRC Unity sample project and you need to go to packages manifest.json because as you can see in the Manifest the Json it's getting the actual uh app abolute path and not the relative path that all the other uh packages are using so that's why we need to literally just do this and it should be reading everything correctly once again one second and now we have successfully integrated a new package into the project now let's go to create and create an assembly definition because that's a good practice to do and we are going to call it chain save. gaming.
web 3- unity. mud the name should be this as well and the root name space should be chains save. gaming. mud so every script that we are going to create create now is going to utilize that name space so the thing is that we need to create four different scripts the first one is going to be called IUD and that represents the interface the second one is going to be called just mud third one is going to be called Mud exensions and the last one is going to be called Mud service adapter so if we go back to the presentation itself we can see that we always need to create the interface first and then the concrete type that you are implementing and that is important for the dependency injection and that is what we are going to do here exactly so we are going to change this from class to an interface and let's for example add a method called test method and that's it in the mud uh class itself we're not going to extend from mon Behavior we're just going to implement the imod interface now comes the fun part and that is how we are going to integrate mud into the web 3 Unity solution itself we're going to make a static class it's going to be called Mud extensions back here and say static and this class should have two methods the first one indicates that we need to use mod in the project and the second one is going to be the one that is actually getting the mud instance from the service provider the way we are doing this is public static web 3 service collection or let me just say I three service collection use mud and this is going to be I web3 service collection and uh this is going to return service collection but before that we are going to add singl ton of type IUD and it's going to be mud but we forgot one thing and that is to implement for example this I life cycle participant interface in here and these are especially useful for as we already mentioned starting some processes or uh en closing connections for stopping when stopping and here we need to implement both of these participants and that way we have injected mud into our SDK but now we need to somehow retrieve mud from this service collection and that is this web 3 we need to use the train. gaming.
web3 instance and we are just going to return web 3. service provider. getet required service and is going to be of type Imad and that's basically it that is the I think it's pretty easy and straightforward I mean it's for the first time it's kind of uh difficult to wrap your hand head around it but there is also one last thing to be done here the or two things actually the first thing that we need to do is to call these extension methods and the way we have done it here at chains safe is this service adapter essentially is used uh to inject let me just go back to the login scene so that you can see what I'm talking about so in order for you to customize the all of the all of the components or services that you want to use we've done something like here in the ramp service adapter essentially in uh we created this interface I web3 Builder service adapter and that we are going to implement here as well and mud service adap once we add uh mud service adapter here it needs to be added to the login uh provider because login provider gets all the components for that implement this I web3 Builder service adapter iterates through them and then configures what this method these methods said that need to be configured here let's just quickly return the web3 Builder but now let's uh actually inject ramp uh not ramp sorry uh mud so web3 builder. configure and this takes a Lambda expression and Lambda expression is X that use mud and that gives us our mud inside of the project and that should be it for when we are injecting things like when we are wanting to inject certain Services into the project but the other thing that we need to do is that we need to go into web 3 Unity samples and in here we need to add the mud assembly in order to utilize all of the stuff that we have written in package so for example in the sample main scene we have SDK calls and we have this erc20 call and let's say that in the balance off we just want to call the method from mud I mean it's not the place that where you would actually call something mod related but we are just doing this to demonstrate it and we are going to call web3 do mud and as you can see you have this uh using train. gaming.
mud and we are going to call the test method and let's just B maybe print out something in the test method for example debug.log hello from train saves mod and let's try to log in with the web three o and to see if this is actually working and as you can see if we click on balance up we get hello from Chive SMUD and we can start expanding our SDK and that should be it Jay stage is yours thank you Nicola for the presentation and how to extend our SDK at this time I want to give people some ideas you know how to do this but what are you going to build so I'll start sharing my screen and you should be able to see my slides here so what extensions can I build uh first is third party Integrations we went over how to extend it with mud and we've used the same uh process to extend our SDK with ramp with web3 off and tons of other tools so if I was a hacker in uh this hackathon this week what I would be doing is looking at the tools that are already partners and figuring out how can I integrate those tools with the unity SDK and the reason why is because you can submit the same code based to multiple prizes so by working with several Partners you increase your chances of winning a prize so we have a few listed here generative nfts with lilypad lilypad is decentralized compute Network they have a module for stable diffusion so you could do something like generating profile pictures for a game minting them directly in the game and allowing your players to use it player IDs with ens if you're let's say playing a game where there's a lot of resource management maybe you want to see how someone does with their actual finances and you could see their ens ID look at their portfolio and see what they were doing and kind of use that to draft your teams a video game AI agents with cive there's plenty of games that are springing up where you just tell AI agents what to do and it kind of simulates the environment whether that's like a war game in your genro or maybe just another Resource Management game and of course as we mentioned earlier today you know updating our SDK with mud if there's other Frameworks out there that can be used with our SDK we'd love to see that in addition to extending it with third party applications you could also use prefab scripts so if there's a smart contract that's out there for let's say Wagers or tournaments you can create a prefab script that interacts with those smart contracts and allow other players to actually use that in their video games as well so think about common web 3 gameplay mechanics and figure out how to add that into our SDK besides our uh extension prize we're also offering two additional prizes that you can look into so the first is the marketplace uh toolkit if you have a video game already and you use nfts and you want to figure out how to trade them but you don't want to use something like open C then take a look at our Marketplace toolkit there you can easily build the marketplace using our dashboard and incorporate it directly into your unity game with a few clicks so reach out to us if you want to learn more about that and lastly we have our onchain Loop boxes so they're fully decentralized onchain Loop boxes that uses chain links vrf to Generate random numbers and that ensures a transparency because your players can see what numbers are being generated and it also ensures that it's uh verifiably random so check out our onchain Loop boxes our marketplaces and look to our extend our SDK Nicola and I will be in the chats so if you have any questions things you want to discuss feel free to reach out to us and if you want to learn more about chains saafe go to gaming. chains. or follow us on Twitter at chainsa eth and join our Discord at chains saafe thank you this is Jay from uh chains saafe gaming and thank you for the presentation Nicola awesome cheers thank you both yeah are there any questions for nicolar J uh Ser if you're talking you're muted what's up Rory uhy I was actually at the chains safe uh Meetup last week for the base presentation so if you're in Toronto there I think that's the office window um but I had a quick question around if you guys support uh spatial which is like the web um you can do Unity projects and then export them to web don't know if you guys have seen that um can you send the okay toolkit that spatial Theo so the thing with them is they need to support a specific version of unity which is 2021 something something uh which is a little bit I guess predated I don't know which Unity uh version yeah we are on 2020 2021 but this but we are upd we are literally updating uh soon to 2022 this is something interesting we do not officially support this but this seems like a pretty interesting uh thing like is it blockchain backed by blockchain or what what is this is it like a multiplayer framework but I'm not so this is not uh blockchain related we're trying to build so we we're trying to build a game that's going to have web 3 components to it um but the idea with spatial is that you can play it in the browser instead of um having to like have the game be published um on like the quest or whatever um so if you go to spatial.ai which you can just play right in the browser so this is kind of like enabling people to play Unity developed games in an environment so it's similar to it basically no idea what that is um I'm I'm exploring the space completely like I'm very brand new to all of this but I thought for a hackathon it would be cool to dive into this um itch.
io I mean I can say that we do support spatial because I mean there's no reason why we wouldn't uh if they have their own toolkits we are not clashing with them in any way right it's just I guess a matter of versions cuz I think I was checking for uh Mona and their um support is only like 2022 and above um so I don't our Curr version supports 20213 and webg with the no like cool okay um yeah so I guess either completely like integrating this and working with it or setting up a um like a bridge I guess for chains safe to like I guess an extension of Swords um but yeah yeah cool that's it thank you thanks aome cool any other questions and if not then as uh Nicola and Jay said you can always find them on the partner Discord channel uh they'll be there to help answer any questions that you may have um but yeah thank you thanks again Nicola and Jay for the great workshop and presentation this will be again posted on YouTube um right after this and yeah hopefully everyone has a great rest of your week so happy hacking we'll see yall here in a little bit for another Workshop cheers [Music]
Automatic transcript — names and jargon may be misspelled.