Web3 NFT Shop Forum

It didn’t support WebGL.

How do I build in WebGL?
Do something with Moralis SDK or Unity ?

You can follow moralis migration video that will help you how to migrate old sdk to new one also you can shift your platform to WebGL

https://docs.moralis.io/moralis-dapp/connect-the-sdk/connect-with-unity

Hi! I hope everyone is having a good start of the week.

I inform you that I updated the project to the last version of the SDK (1.2.1) and is now using the AuthenticationKit, which allows the project to build to any platform (yes, WebGL included).

I also fixed an issue on getting the minted player items. @bugBee88 I think you had that issue. We’re investigating GetNftsForContract function as it fails to retrieve NFT metadata sometimes. For now, Calling ReSyncMetadata solves that so this is now implemented here (PlayerInventory.cs):

private async void LoadPurchasedItems()
    {
        //We get our wallet address.
        MoralisUser user = await Moralis.GetUserAsync();
        var playerAddress = user.authData["moralisEth"]["id"].ToString();

        try
        {
            NftOwnerCollection noc =
                await Moralis.GetClient().Web3Api.Account.GetNFTsForContract(playerAddress.ToLower(),
                    GameManager.ContractAddress,
                    GameManager.ContractChain);
            
            List<NftOwner> nftOwners = noc.Result;

            // We only proceed if we find some
            if (!nftOwners.Any())
            {
                Debug.Log("You don't own any NFT");
                return;
            }
            
            foreach (var nftOwner in nftOwners)
            {
                if (nftOwner.Metadata == null)
                {
                    // Sometimes GetNFTsForContract fails to get NFT Metadata. We need to re-sync
                    Moralis.GetClient().Web3Api.Token.ReSyncMetadata(nftOwner.TokenAddress, nftOwner.TokenId, GameManager.ContractChain);
                    Debug.Log("We couldn't get NFT Metadata. Re-syncing...");
                    continue;
                }
                
                var nftMetaData = nftOwner.Metadata;
                NftMetadata formattedMetaData = JsonUtility.FromJson<NftMetadata>(nftMetaData);

                PopulatePlayerItem(nftOwner.TokenId, formattedMetaData);
            }
        }
        catch (Exception exp)
        {
            Debug.LogError(exp.Message);
        }
    }

Remember to pull from https://github.com/MoralisWeb3/youtube-tutorials to get the new version.

Cheers!

2 Likes

Hi David,

I have a very important question.
How can I call from Unity a function in a contract that has, for example, an onlyOwner modifier? In other words, you can only execute that function with the wallet owner.
I see in the video that you have commented on it and you say that you have to add a security in the production contract, but I have a question about how to call it from unity.

Thanks and regards

Hi @naxovr and welcome to the community!

In Unity you would call it the same way we’re calling it here. The difference is in the contract and the security measures that you implemented on it.

There’s a lot of ways of doing that but one would be to transfer ownership to the wallet addresses that you decide. This is not a thing that you would do in Unity, you’d do it on the JS server side.

This is not the topic of this tutorial but you can take a look at this:

Cheers!

1 Like

Thank you very much for answering David.
So you think that a JS server is always needed to interact with Unity and solidity? Or do you mean within Moralis.
In another one of your videos you make a reference to a magic password and emit an event if the password is valid. It could be a way to “verify” that what Unity sends is “as the owner”.
I’ll look.
Thank you very much and I hope you cool off well since it is very hot here in Spain

Indeed it is :hot_face:!

What I mean is that if you want to create a good security system you probably want to manage it through a backend server, which communicates to the contract. Unity could also be in contact with this server, sending every address that is connecting to the game, but the server would be the one to decide what address can be set as “ownable” by calling the transferOwnership function in the contract passing the address. I hope you understand my point.

You’re well on track with that “magic password”. It can be a first step to secure a transaction coming from Unity but not a very strong one. Again, I’m not an expert on this topic so DYOR. Also, it is a much more complex topic that we might initially think of.

Saludos!

And I mean that generally speaking, you would want to have a backend server to interact with your Unity game even if you were not using Moralis and smart contracts.

thanks for your answer. I will keep investigating.
Cheers!

Hi guys, I have finished the tutorial and need help on few questions

  1. Why we need to mint the NFT in Unity instead of mint in Opensea directly?
  2. If the NFT items are minted by player itself, how we can get the earning?
    Thanks in advance.

Hi everyone!

I set up a specific repository for the updated project (using lastest version of the SDK). I’d recommend you guys to download it and test it:

Also remember to press L to Logout if you’re finding any wallet authentication issues :slight_smile:

Cheers!

1 Like

Hi There !
Great content amazing work from you as usual !
Trying to get my head around your tutorial and the big jump to 1.2.1 , so far so good.
Just one question:
What would be the easiest way to instantiate the shop NFTs on a 3d model (a quad) instead of a UI grid ?

Thanks a lot !

Hi @Hero909 and thanks for your words,

Do you mean all NFTs images in the same cube? Or each NFT image in a different 3d model?

1 Like

Managed to do it !
But made me realized it is not actual NFTs that are shown in the shop inventory but images that are used to mint nfts in the player’s inventory. correct ?
Thank you very much for your reply

Also, been struggling in finding a way to use custom erc20 to mint / buy nfts … any suggestion ? :slight_smile:

That’s correct! Information coming from NFTs happens when we open PlayerInventory.

That’s a very good idea! I might cover that in a future video soon :slight_smile:

1 Like

omg its been weeks since i have been trying to do it! almost gave up haha

1 Like

Hi There, finishing the tutorial, but having a strange behavior:
I manage to mint the nft, metadatas appear in the player’s inventory but on opensea the ipfs doesnt seem to display the infos:
https://testnets.opensea.io/collection/unity-nft-shop-tlaldv7k3h

The ipfs link for id 18 is this one:
https://ipfs.moralis.io:2053/ipfs/QmYc5eagXrzRRuga2rrPr7jY9CyA3cxWGh7sHbzXcD51wd/Health%20Potion_DkT3m97a1JAdm76BhEYG2WCE.json

Any idea what am I missing … ?