프로그래밍 농장

Metamask 연동하기 (Chainsafe SDK) [ Unity ] 본문

Unity

Metamask 연동하기 (Chainsafe SDK) [ Unity ]

Tennessee201 2022. 3. 19.
728x90

유니티 환경에서 Metamask / Trustwallet 등 지갑을 연동해주는 작업을 수행해보겠습니다.

이 작업과정에서 Ethereum mainnet / Testnet / Bsc (Binace smart chain) / 등 코드상 등록된 여러 네트워크들을 사용할수도 있습니다.

 


먼저, 메타마스크 지갑을 플러그인 형식으로 다운로드후 가입을 해놓습니다.

https://metamask.io/

 

A crypto wallet & gateway to blockchain apps | MetaMask

A crypto wallet & gateway to blockchain apps

metamask.io

 

이후 아래의 sdk 프로젝트에서 web3 - 유니티상에서 연동될수있도록 web3.unityPackage 를 다운받아 유니티 프로젝트 상의 Assets에 넣고 import를 진행해준다. 

https://github.com/ChainSafe/web3.unity

 

GitHub - ChainSafe/web3.unity: 🕹 Unity SDK for building games that interact with blockchains.

🕹 Unity SDK for building games that interact with blockchains. - GitHub - ChainSafe/web3.unity: 🕹 Unity SDK for building games that interact with blockchains.

github.com

아래와 같이 압축을 풀어줍니다. 

이후 위의 폴더와 내부의 WebGLTemplates 폴더 총 2개를 생성한 유니티 3D 프로젝트 Assets 내에 드래그하여 임포트해준다.

 

임포트를 완료하였다면 아래와 같이 Assets/_dLabs/Scenes/WebLogin 을 클릭하여 씬을 열어준다. 

 

아래와 같이 Metamask 로그인을 완료하였을시 사용자의 지갑주소를 Text 상에 띄워줄수있도록 씬을 더해주고, WebGL 형식으로 플랫폼을 변경한다. 

File > Build Setting .. 을 클릭 / 이후 아래와 같이 두 Secne 을 설정 및 WebGL로 Platform Switch

 

위 작업을 완료하였다면 Player Settings..를 클릭후 WebGL의 Template을 아래와 같이 Web3GL-2020으로 변경해준다.

Project Settings > Player > Resolution and Presentation > WebGL Templates 에서 Web3GL-2020을 선택하고 창을 닫아준다.

 

이후 아래와 같이 우클릭 > UI > Text를 생성 

우클릭 > Create > C# Script 를 클릭하여 'GetAccount'로 파일의 이름을 변경

 

GetAccount.cs 코드

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class GetAccount : MonoBehaviour
{
    public Text myAccount;

    // Start is called before the first frame update
    void Start()
    {
        myAccount.text = PlayerPrefs.GetString("Account");
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

 

위 작업이 모두 완료되었다면 File > Build and Run 버튼을 클릭후 컴파일을 진행. 

지갑을 로그인하고 연결하게 되면 아래와 같이 다음 씬으로 넘어가고, 연동된 내 지갑주소를 Text상에 띄워준것을 확인할수있다. 

728x90