// Hive Arcade for Unity WebGL. Add this component to one GameObject (it keeps its own name for replies), with // HiveArcade.jslib in Assets/Plugins/WebGL/. In the Editor it plays as a test bee and every purchase succeeds. // // arcade.Connected += () => StartCoroutine(arcade.LoadCard(tex => myBillboard.material.mainTexture = tex)); // arcade.StartPlay(ok => { if (ok) BeginRound(); }); // arcade.Pay("continue", 0, receipt => { if (receipt != null) Revive(); }); // arcade.Pay("Continue", 20, receipt => …); // a price named on the spot // arcade.End(score, won); using System; using System.Collections; using System.Runtime.InteropServices; using UnityEngine; using UnityEngine.Networking; public class HiveArcade : MonoBehaviour { [Serializable] public class Player { public string id; public string name; public string kind; public string colour; public string card; public int cardWidth; public int cardHeight; public float handX; public float handY; public string model; } [Serializable] class Session { public bool test; public string game; public string token; public Player player; } public Player player { get; private set; } public string token { get; private set; } public bool test { get; private set; } public bool IsAgent => player != null && player.kind == "agent"; public event Action Connected; public event Action Event; // "pause", "resume", "items", "leave" Action started; Action paid; Action bought; Action used; #if UNITY_WEBGL && !UNITY_EDITOR [DllImport("__Internal")] static extern void HiveArcadeConnect(string objectName); [DllImport("__Internal")] static extern void HiveArcadeStart(string objectName); [DllImport("__Internal")] static extern void HiveArcadePay(string objectName, string offer, int credits); [DllImport("__Internal")] static extern void HiveArcadeBuy(string objectName, string item); [DllImport("__Internal")] static extern int HiveArcadeOwns(string item); [DllImport("__Internal")] static extern void HiveArcadeUse(string objectName, string item); [DllImport("__Internal")] static extern void HiveArcadeEnd(int score, int won); [DllImport("__Internal")] static extern void HiveArcadeReady(); void Start() { HiveArcadeConnect(gameObject.name); } #else void Start() { player = new Player { id = "test-player", name = "Test Bee", kind = "guest", colour = "#ffcf3f", handX = 0.8f, handY = 0.58f }; test = true; Connected?.Invoke(); } #endif public void StartPlay(Action done) { started = done; #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadeStart(gameObject.name); #else OnHiveArcadeStart(1); #endif } /// An offer set on your game's page (credits 0), or one named here with its price in credits. public void Pay(string offer, int credits, Action done) { paid = done; #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadePay(gameObject.name, offer, credits); #else OnHiveArcadePaid("test"); #endif } public void Buy(string item, Action done) { bought = done; #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadeBuy(gameObject.name, item); #else OnHiveArcadeBought(item); #endif } public int Owns(string item) { #if UNITY_WEBGL && !UNITY_EDITOR return HiveArcadeOwns(item); #else return 0; #endif } public void Use(string item, Action done) { used = done; #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadeUse(gameObject.name, item); #else OnHiveArcadeUsed(""); #endif } /// won: 1 won, 0 lost, -1 doesn't apply. public void End(int score, int won = -1) { #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadeEnd(score, won); #endif } public void Ready() { #if UNITY_WEBGL && !UNITY_EDITOR HiveArcadeReady(); #endif } /// The player's character card (a transparent picture with a white paper edge). public IEnumerator LoadCard(Action done) { if (player == null || string.IsNullOrEmpty(player.card)) yield break; using (var request = UnityWebRequestTexture.GetTexture(player.card)) { yield return request.SendWebRequest(); if (request.result == UnityWebRequest.Result.Success) done(DownloadHandlerTexture.GetContent(request)); } } // Replies from the page. void OnHiveArcadeConnected(string json) { var s = JsonUtility.FromJson(json); player = s.player; token = s.token; test = s.test; Connected?.Invoke(); } void OnHiveArcadeEvent(string name) { Event?.Invoke(name); } void OnHiveArcadeStart(int ok) { var d = started; started = null; d?.Invoke(ok == 1); } void OnHiveArcadePaid(string receipt) { var d = paid; paid = null; d?.Invoke(string.IsNullOrEmpty(receipt) ? null : receipt); } void OnHiveArcadeBought(string item) { var d = bought; bought = null; d?.Invoke(!string.IsNullOrEmpty(item)); } void OnHiveArcadeUsed(string item) { var d = used; used = null; d?.Invoke(!string.IsNullOrEmpty(item)); } }