npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@zpteam/react-xpla-unity-game

v0.1.0

Published

The JavaScript SDK for XPLA with unity game.

Downloads

9

Readme

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 @zpteam/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://zpt.samples.app/app.loader.js',
  dataUrl: 'https://zpt.samples.app/app.data.unityweb',
  frameworkUrl: 'https://zpt.samples.app/app.framework.js.unityweb',
  codeUrl: 'https://zpt.samples.app/app.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 GetContractQuery(string contract, string query);

  [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 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://zpt.samples.app/app.loader.js',
  dataUrl: 'https://zpt.samples.app/app.data.unityweb',
  frameworkUrl: 'https://zpt.samples.app/app.framework.js.unityweb',
  codeUrl: 'https://zpt.samples.app/app.wasm.unityweb',
};

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

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 is a wrapper component of react-unity-webgl's Unity component.

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

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

Hook