# 2D Game Example

### Overview

This section showcases the Cronos play Gaming SDK integration with the Cronos chain, aiming to bring Unity games into the Cronos ecosystem. In particular, we will show how to do a basic integration of the Cronos play with the Cronos chain including:

* Connecting to Cronos networks;
* Installaiton of the SDK;
* Obtaining a sample NFT and;
* Import Sample package from Unity Asset Store

### Pre-requisites

1. [Unity](https://unity3d.com/get-unity/download) (Version 2020.3.25f1 or later)
2. [ChainSafe SDK](https://github.com/ChainSafe/web3.unity/releases)
3. [Metamask browser extension](https://metamask.io/download/)

{% tabs %}
{% tab title="Mainnet" %}
Network Name: **Cronos Mainnet Beta**

* New RPC URL: **<https://evm-dev.cronos.org/>**
* Chain ID: **25**
* Currency Symbol: **CRO**
* Block Explorer URL: **<https://explorer.cronos.org/>**
  {% endtab %}

{% tab title="Testnet" %}
Network Name: **Cronos Testnet**

* New RPC URL: **<https://evm-dev-t3.cronos.org/>**

* Chain ID: **338**

* Currency Symbol: **TCRO**

* Block Explorer URL: **<https://explorer.cronos.org/testnet>**
  {% endtab %}
  {% endtabs %}

* Sufficient funds on deployer address
  * [Testnet Faucet](https://cronos.crypto.org/faucet/)

* For Crypto.com Onchain Wallet integration, please refer to our [documentation](https://docs.cronos.org/getting-started/gamefi-sdk.md#enable-deficonnect-and-walletconnect).

## Integration guide for Cronos

### Create a new project

* Download the tutorial bolt kit from [Unity](https://assetstore.unity.com/packages/essentials/tutorial-projects/bolt-kit-platformer-tutorial-assets-168067) and import the assets into the blank new project. Below are the steps for importing Tutorial Projects - Bolt Kit from Unity. See [Reference #1](#reference).

### Download the latest ChainSafe SDK and import it into the project.

* For a detailed explanation, please refer to [ChainSafe official documentation](https://chainsafe.github.io/game-docs/)

### Prefabs interaction

* Step 3a. \[Unity] Double click the `Level1` scene under `Assets/Scenes` directory

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-245f021d43538b9c8649073b3416369e0d86af8f%2Fcronos-gamefi-integration-step3a.png?alt=media\&token=acd1ef70-4172-44de-be32-2d859631f121)

#### Transfer721 Example

* Drag the `Transfer721` prefab under `Assets/Web3Unity/Prefabs/WebGL` dir into the scene.

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-ebed66abd2af6cb18d49bb95e2603518b744f0da%2Fcronos-gamefi-integration-step6-1.png?alt=media\&token=aad8ad39-0a79-4b9c-abe5-a686ee3202d8)

* Update the `Contract` address under `Transfer721Script` object inspector
* Update the `To Account` address to your desired wallet address

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-867c28a5c51dbee947fe267a3d54d90cae50e0bd%2Fcronos-gamefi-integration-step6-2.png?alt=media\&token=6034183c-84ba-4fda-aaf2-1a4d08570c8d)

***

\* For acquiring the NFT, you can create your own (see [Reference #2](#_2-smart-contract)), or mint an ERC721 NFT at our [Cronos NFT faucet](https://cronos.org/nft-faucet) (see [Reference #3](#_3-nft-faucet)).

***

### Build and Run

1. Switch to WebGL platform

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-53ecdb953566450ec6d3a5289681e69a9d20ab13%2Fcronos-gamefi-integration-step8-1.png?alt=media\&token=8e3e0941-0fea-4169-b951-705c0f3e68fe)

1. Click the `Player Setting` button
2. Select `Web3GL-2020` under `Resolution and Presentation` tab

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-e4e592fca51c1d6a470cd23dfc79992c95a8a74f%2Fcronos-gamefi-integration-step8-2.png?alt=media)

1. Set `Compression Format` to `Disabled` under `Publishing Settings` tab

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-b74672fb4d6c882367d27f6059dd58e83586fd34%2Fcronos-gamefi-integration-step8-3.png?alt=media)

1. After that, click `Build and Run` button and you are ready to play.

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-8c6dc207cd9f6f63d4417ebc7ae13b0a6696baca%2Fcronos-gamefi-integration-step8-4.png?alt=media)

## What's next

The above guideline documentation only highlights the basic integration between ChainSafe and Cronos chain. You can download the full source code of the above sample game [here](https://github.com/crypto-org-chain/cronos-docs/blob/master/docs/getting-started/assets/cronos-chainsafe-unity-sample.unitypackage.zip). For other ERC protocol methods, please refer to the [ChainSafe Documentation](https://chainsafe.github.io/game-docs/).

## Reference

### Import Sample package from Unity Asset Store

#### 1a. Click to add the asset and sign in to your Unity account

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-e7eb983019563608b5b5aaf4ac60db2610003f4a%2Fcronos-gamefi-integration-step2a.png?alt=media)

#### 1b. Create a new project in Unity

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-4a97e31cc271cf4a9d880bf922922648e174f388%2Fnew-projects.png?alt=media\&token=04c7adb1-615b-4276-9cd5-8de8ea6ebfd8)

#### 1c. Open the Package Manager window in Unity and under "My Assets" download the Bolt Kit.

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-2e73b3e1efc9ffe28781625e4a40e3a20ab6e341%2Fcronos-gamefi-integration-step2c.png?alt=media)

#### 1d. Select `Bolt Kit: Platformer Tutorial Assets` and click Import

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-147ac384c4d0839d525cee5609bfa4db433de082%2Fcronos-gamefi-integration-step2d.png?alt=media)

***

### Smart contract

#### 2a. Create an ERC standard smart contract

* ERC20 example

```javascript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts@4.4.2/token/ERC20/ERC20.sol";
import "@openzeppelin/contracts@4.4.2/access/Ownable.sol";

contract MyToken is ERC20, Ownable {
    constructor() ERC20("MyToken", "MTK") {
        _mint(msg.sender, 1000 * 10 ** decimals());
    }

    function mint(address to, uint256 amount) public onlyOwner {
        _mint(to, amount);
    }
}
```

* ERC721 example

```javascript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts@4.4.2/token/ERC721/ERC721.sol";
import "@openzeppelin/contracts@4.4.2/access/Ownable.sol";
import "@openzeppelin/contracts@4.4.2/utils/Counters.sol";

contract MyNFT is ERC721, Ownable {
    using Counters for Counters.Counter;

    Counters.Counter private _tokenIdCounter;

    constructor() ERC721("MyNFT", "MNFT") {}

    function _baseURI() internal pure override returns (string memory) {
        return "https://opensea-creatures-api.herokuapp.com/api/creature/";
    }

    function safeMint(address to) external onlyOwner {
        uint256 tokenId = _tokenIdCounter.current();
        _tokenIdCounter.increment();
        _safeMint(to, tokenId);
    }
}
```

* ERC1155 example

```javascript
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.2;

import "@openzeppelin/contracts@4.4.2/token/ERC1155/ERC1155.sol";
import "@openzeppelin/contracts@4.4.2/access/Ownable.sol";

contract My1155Token is ERC1155, Ownable {
    constructor()
        ERC1155("https://opensea-creatures-api.herokuapp.com/api/creature/")
    {}

    function setURI(string memory newuri) public onlyOwner {
        _setURI(newuri);
    }

    function mint(address account, uint256 id, uint256 amount, bytes memory data)
        public
        onlyOwner
    {
        _mint(account, id, amount, data);
    }

    function mintBatch(address to, uint256[] memory ids, uint256[] memory amounts, bytes memory data)
        public
        onlyOwner
    {
        _mintBatch(to, ids, amounts, data);
    }
}
```

#### 2b. Deploy Smart Contract via [Remix IDE](https://remix.ethereum.org/)

```
-  Switch the chain network to Cronos Testnet via Metamask extension or other similar provider
-  Select the `Injected Web3` under ENVIRONMENT tab
```

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-76d5a6348182d6e4e324390e46e9e0d3e60782e3%2Fcronos-gamefi-integration-step5-1.png?alt=media)

```
-  Approve the connection
```

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-84762929d00d6ecbc4ed987ad67a8e0c78110fe5%2Fcronos-gamefi-integration-step5-2.png?alt=media)

```
-  Select your contract name under CONTRACT tab
```

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-f15b279ac23eda65bfb902cb930e920c0d3e16bf%2Fcronos-gamefi-integration-step5-3.png?alt=media)

```
-  Approve the transaction after clicking `Deploy` button
```

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-07aaf8c00f42445ff653ca41c11bee676f609a82%2Fcronos-gamefi-integration-step5-4.png?alt=media)

```
-  Copy the contract address
```

***

### NFT Faucet

* 1\. Connect your wallet via metamask or walletconnect

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-6a519ac81024754a7e9da6eabbf3eaccabdd2757%2Fcronos-gamefi-integration-nft-fauct-1.png?alt=media)

* 2\. Click `Create NFT` under `YOUR COLLECTION` tab

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-53f2acf0773c7ee1f31d49e6171bc31fce12c977%2Fcronos-gamefi-integration-nft-fauct-2.png?alt=media)

* 3\. Create and sign the transaction after uploading the image

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-66992be28a6aa37d7235fcb8525c0e8246b6ad3e%2Fcronos-gamefi-integration-nft-fauct-3.png?alt=media)

* 4\. Copy the contract address and image URL from the NFT modal

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-1488a20d151e2fe0df774976bdc21b09f6ccb9c5%2Fcronos-gamefi-integration-nft-fauct-4.png?alt=media)

* 5\. Paste into the corresponding input box inside Unity inspector

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-867c28a5c51dbee947fe267a3d54d90cae50e0bd%2Fcronos-gamefi-integration-step6-2.png?alt=media\&token=6034183c-84ba-4fda-aaf2-1a4d08570c8d)

***

### (Optional) List NFT for sale in marketplace faucet

* 1\. Fill in the price at list item modal and confirm

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-2e4451938338177ae5d9e82bc54e333461bb0396%2Fcronos-gamefi-integration-nft-fauct-5.png?alt=media)

* 2\. Sign approval and create item transaction

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-81c66c2a8ff4a41005848fb7718fa3502c92cc05%2Fcronos-gamefi-integration-nft-fauct-6.png?alt=media) ![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-f0a8a91c36423a33f8668e41973f66ca6220d925%2Fcronos-gamefi-integration-nft-fauct-7.png?alt=media)

* 3\. View the listed item in Marketplace tab

![](https://1786307500-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FqXsIo4b4WtOTfICyOIxa%2Fuploads%2Fgit-blob-ac7c235c5015d443a53eb6be50cd92c7664c9d6f%2Fcronos-gamefi-integration-nft-fauct-8.png?alt=media)
