/ZPT.glitch-hack

React Blockchain for Unity3D Games

react-xpla-unity-game

This is a library that allows web games made with Unity3d to be easily connected to the XPLA Vault wallet through React.

This library makes it easy to connect with XPLA Vault, and also you can connect it without using this library.

We are using the following 3 libraries for easy integration.


Through this library users can perform the following:

  • Connect to your wallet.
  • Check the balance of tokens stored in the wallet.
  • Tokens stored in the wallet can be transferred to another wallet.
  • Sign messages on the connected wallet.

Table of Contents

Installation

npm i --save react-xpla-unity-game

Demo

npm i
npx parcel sample/index.html 

How to use

First, to communicate from Unity3D to react, save the default WalletAPI.jslib in the Assets > Plugins > WebGL folder of the Unity3D project.

// react
import { StrictMode } from 'react';
import { createRoot } from 'react-dom/client';
import { 
  XplaUnityGameWithWalletProvider,
  XplaUnityGameContainer,
  useRegisterEvent 
} from 'react-xpla-unity-game';

const App = () => {
  // ---------------------------------------------
  // register event
  // ---------------------------------------------
  useRegisterEvent();

  return <XplaUnityGameContainer />;
};

const config = {
  loaderUrl:
    'https://addr/web3games/Sample/BattleNFT40.loader.js',
  dataUrl:
    'https://addr/web3games/Sample/BattleNFT40.data.unityweb',
  frameworkUrl:
    'https://addr/web3games/Sample/BattleNFT40.framework.js.unityweb',
  codeUrl:
    'https://addr/web3games/Sample/BattleNFT40.wasm.unityweb',
};

const gameObjectName = 'GameManager';

const root = createRoot(document.getElementById('root') as HTMLElement);

root.render(
  <>
    <StrictMode>
      <XplaUnityGameWithWalletProvider
        config={config}
        gameObjectName={gameObjectName}
      >
        <App />
      </XplaUnityGameWithWalletProvider>
    </StrictMode>
  </>,
);

Declare and use functions declared in WalletAPI.jslib in your unity3d script.

// GameManager.cs
public class GameManager : MonoBehaviour
{
  [DllImport("__Internal")]
  private static extern void ConnectWallet();

  [DllImport("__Internal")]
  private static extern void DisconnectWallet();

  [DllImport("__Internal")]
  private static extern void GetXplaAmount();

  [DllImport("__Internal")]
  private static extern void GetContractInfo(string contract);

  [DllImport("__Internal")]
  private static extern void GetCW20TokenInfo(string contract);

  [DllImport("__Internal")]
  private static extern void GetCW20Balance(string contract);

  [DllImport("__Internal")]
  private static extern void GetCW721Tokens(string contract);

  [DllImport("__Internal")]
  private static extern void GetCW721TokenInfo(string contract, string token_id);

  [DllImport("__Internal")]
  private static extern void PostTx();
  [DllImport("__Internal")]
  private static extern void GetTxResult();

  [DllImport("__Internal")]
  private static extern void SendToken(string token, string recipient, string amount, string txmemo);

  [DllImport("__Internal")]
  private static extern void SendNFT(string contract, string recipient, string token_id, string txmemo);

  [DllImport("__Internal")]
  private static extern void ExecuteContract(string contract, string execute_msg, string txmemo);

  ...
  ...
}

A function to communicate from react to Unity3D must also be declared. In this library, the function name is used as WalletEventListener.

// GameManager.cs
public class GameManager : MonoBehaviour
{
  ...
  ...

  public void WalletEventListener(string result) {
    Debug.Log(result);
  }

  ...
  ...
}
// react
import { WALLET_EVENT_LISTENER } from '../../config';

...
...

const {
  unityContextHook: { sendMessage },
  gameObjectName,
} = useXplaUnityGame();

sendMessage(gameObjectName, WALLET_EVENT_LISTENER, 'test message');

...
...

API

<XplaUnityGameWithWalletProvider>

XplaUnityGameWithWalletProvider is composed of XPLA Vault's wallet-provider and XplaUnityGameProvider.

XplaUnityGameWithWalletProvider includes XPLA Vault wallet by default.

The default settings are:

// Game build files
const config = {
  loaderUrl:
    'https://assets.zpt.info/web3games/Sample/BattleNFT35.loader.js',
  dataUrl: 'https://assets.zpt.info/web3games/Sample/BattleNFT35.data',
  frameworkUrl:
    'https://assets.zpt.info/web3games/Sample/BattleNFT35.framework.js',
  codeUrl: 'https://assets.zpt.info/web3games/Sample/BattleNFT35.wasm',
};

// Game object name to call from React to Unity3D 
const gameObjectName = 'GameManager';

<XplaUnityGameProvider>

It can be used if there is a wallet-provider of XPLA Vault that is already linked.

The default setting is the same as XplaUnityGameWithWalletProvider.


<XplaUnityGameContainer>

XplaUnityGameContainer is a wrapper component of react-unity-webgl's Unity component.

// loading image or icon
const loading = <img src="IMAGE_LINK" alt="" />;

// Update Unity component style
// Changed the default width of 60% to 70%
const style: React.CSSProperties = {
  width: '70%'
}

return (
  <XplaUnityGameContainer loading={loading} style={style} />
)

Hook