Discussion - Build Metaverse Unity Web 3.0 App with C# | Moralis | Blockchain Unity Game Engine

Hi @Gatsby and welcome to the Moralis Unity community :slight_smile:

Yes, you should be able to use whatever Networking library you decide. Keep us updated on your process with that!

Salut!

I am trying to run this tutorial but error in the signing message.

Hi! First, thank you for this tutorial. its so amazing. But i stuck on script. I made my custom scene with Web3 Game Kit Package v1.2.1. What i need to change in script, with new version of SDK, because system cant find it. Thank you!

Hi @Navajjoo, thanks for your words and welcome to the comunity!

Can you share some screenshot on what and where are the errors? Thanks!

1 Like

Hi @khk.cryptech, do you still have this problem?

Hi!

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

//Starter Assets
using StarterAssets;

//Moralis
using MoralisWeb3ApiSdk;
using Moralis.Platform.Objects;

//Wallet Connect
using WalletConnectSharp.Core.Models;
using WalletConnectSharp.Unity;

public class GameManager : MonoBehaviour
{
    [SerializeField] private StarterAssetsInputs starterAssetsInputs;

    [Header("WEB3")]
    [SerializeField] private MoralisController moralisController;
    [SerializeField] private WalletConnect walletConnect;

    [Header("UI")]
    [SerializeField] private GameObject qrPanel;
    [SerializeField] TextMesh playerWalletAddress;
    
    private async void Start()
    {
        if (moralisController != null)
        {
            await moralisController.Initialize();
        }
        else
        {
            Debug.LogError("MoralisController not found.");
        }
    }

    private void OnDisable()
    {
        LogOut();
    }

    public async void WalletConnectHandler(WCSessionData data)
    {
        // Extract wallet address from the Wallet Connect Session data object.
        string address = data.accounts[0].ToLower();
        string appId = MoralisInterface.GetClient().ApplicationId;
        long serverTime = 0;

        // Retrieve server time from Moralis Server for message signature
        Dictionary<string, object> serverTimeResponse = await MoralisInterface.GetClient().Cloud.RunAsync<Dictionary<string, object>>("getServerTime", new Dictionary<string, object>());

        if (serverTimeResponse == null || !serverTimeResponse.ContainsKey("dateTime") ||
            !long.TryParse(serverTimeResponse["dateTime"].ToString(), out serverTime))
        {
            Debug.Log("Failed to retrieve server time from Moralis Server!");
        }

        Debug.Log($"Sending sign request for {address} ...");

        string signMessage = $"Moralis Authentication\n\nId: {appId}:{serverTime}";
        string response = await walletConnect.Session.EthPersonalSign(address, signMessage);

        Debug.Log($"Signature {response} for {address} was returned.");

        // Create moralis auth data from message signing response.
        Dictionary<string, object> authData = new Dictionary<string, object> { { "id", address }, { "signature", response }, { "data", signMessage } }; 

        Debug.Log("Logging in user.");

        // Attempt to login user.
        MoralisUser user = await MoralisInterface.LogInAsync(authData);

        if (user != null)
        {
            UserLoggedInHandler();
            Debug.Log($"User {user.username} logged in successfully. ");
        }
        else
        {
            Debug.Log("User login failed.");
        }
    }

    private async void UserLoggedInHandler()
    {
        //"Activate" game mode
        qrPanel.SetActive(false);

        starterAssetsInputs.cursorLocked = true;
        starterAssetsInputs.cursorInputForLook = true;
        Cursor.visible = false;
        
        //Check if user is logged in
        var user = await MoralisInterface.GetUserAsync();

        if (user != null)
        {
            string addr = user.authData["moralisEth"]["id"].ToString();
            playerWalletAddress.text = string.Format("{0}...{1}", addr.Substring(0, 6), addr.Substring(addr.Length - 3, 3));
            playerWalletAddress.gameObject.SetActive(true);
        }
    }
    
    private async void LogOut()
    {
        await walletConnect.Session.Disconnect();
        walletConnect.CLearSession();

        await MoralisInterface.LogOutAsync();
    }
}

Hi @Navajjoo,

Yeah, the problem here is that this project is using an old version of the SDK. There was a big change in the SDK about 1 month ago with some breaking changes. Forget about the old GameManager, create a new one and start from there. If you check AuthenticationKit, on the OnConnected event you can add your new GameManager function to start the game or do whatever you need.

Cheers!

Hey im having issues on build(mac,windows,linux) where the QR code doesn’t populate. works fine when you test in unity. any help would be appreciated thanks.

Do you get any errors? This is the project you used?

no erros yes this project or the same one khk.cryptech screen shotted

This project is quite outdated now and using an older Unity editor version as well (2020.3.2) - which editor version are you using? Are you familiar with Unity and the Moralis game kit? If so you could migrate the functionality/code to a new project.

A clone of that project works for me after when built on Windows (and adding serverUrl and appId to Prefab) - Unity Editor version 2020.3.2.

im using 2021.3.6, strange yes it works in dev for me not on build. Will migrate over if i cant resolve this. maybe a mac issue.

Try using the same editor version 2020.3.2 as the original project.