概要
toio買って、toio SDK for Unityでアプリ開発を勉強中です。
購入時に一緒に付いてきた簡易カードを見て、真っ先に思いついたのがかるた。
ということで試作しました。
GitHub repository
WebGL sample
toio SDK for Unityの説明にある形で、WebGLビルドをGitHub Pagesで公開しています。
toioキューブと簡易カード(A〜Z)が必要です。
https://zurachu.github.io/toio-karuta/
動画(Twitter)
#toio SDK for Unityの試作一発目として、簡易カード(StandardID)読み取り機能を使ったかるたゲームを作ってみたよ!
— ヅラChu (@zurachu) December 31, 2020
開発用プレイマット買わないと簡易カードの予備がないからカード切り取れないんだよなぁ。 pic.twitter.com/6F8Duuha5F
書いている通り、手元の簡易カードが、最初に付いてきた1個しかないので、カード切り離すのは躊躇しているところ。
実際にかるたっぽくするには、開発用プレイマットを買い足して、切り離さないと…
A〜Zを使っているので、「Apple」「Banana」「Cat」などにして、絵をつけて子ども向けのアルファベットかるたにしようかなと思っています。
技術情報
StandardIDの更新をコールバックで受け取る
Update毎にCube.standardIdで取得しても良いですが、UIのボタン操作などと同様にイベントドリブンで記述するために、コールバックでStandardIDが更新された(センサーが新しい簡易カードを認識した)、またはStandardIDが失われた(センサーが簡易カードから離れた)イベントを受け取るようにします。
作法として一応前に登録したListenerを削除してから追加します。
cube.standardIdCallback.RemoveListener(callbackKey);
cube.standardIdCallback.AddListener(callbackKey, OnUpdateStandardId);
cube.standardIdMissedCallback.RemoveListener(callbackKey);
cube.standardIdMissedCallback.AddListener(callbackKey, OnMissedStandardId);
StandardIDの値を使いやすくする
StandardIDからtoio.Simulator.StandardID.SimpleCardTypeやSimpleCardNamesへの変換をUtilityとして用意しました。
using System;
using System.Collections.Generic;
using toio.Simulator;
public static class ToioSimpleCardUtility
{
public static readonly List<StandardID.SimpleCardType> AlphabetTypes = new List<StandardID.SimpleCardType>
{
StandardID.SimpleCardType.Char_A,
// 中略
StandardID.SimpleCardType.Char_Z,
};
// 以下、interfaceだけ列挙します
public static bool IsSimpleCardId(uint standardId);
public static StandardID.SimpleCardType TypeOf(uint simpleCardId);
public static string NameOf(uint simpleCardId);
public static string NameOf(StandardID.SimpleCardType simpleCardType);
public static bool IsAlphabet(uint simpleCardId);
public static bool IsAlphabet(StandardID.SimpleCardType simpleCardType);
}
プリセット効果音のIDを定数化
https://toio.github.io/toio-spec/docs/ble_sound#%E5%8A%B9%E6%9E%9C%E9%9F%B3%E3%81%AE-id
これはSDKにあっても良さそうな…!
public static class ToioSoundUtility
{
public static class PresetSoundId
{
public static readonly int Enter = 0;
public static readonly int Selected = 1;
public static readonly int Cancel = 2;
public static readonly int Cursor = 3;
public static readonly int MatIn = 4;
public static readonly int MatOut = 5;
public static readonly int Get1 = 6;
public static readonly int Get2 = 7;
public static readonly int Get3 = 8;
public static readonly int Effect1 = 9;
public static readonly int Effect2 = 10;
}
}