【Unity】RPGアツマール対応
やること
UnityでつくったゲームをRPGアツマールに対応させる。
使ったUnityのバージョンは2019.3.0f1。
成果物
0.ググれ
すでにネット上に記事があるので、「Unity アツマール」とかでググりましょう。
1.最小構成 まずは動かす
WebGLでビルドし、zipにまとめてアップロードすれば動いてくれる。
アツマールではzip内にあるindex.html
が表示される。
ちなみにアツマール標準サイズである816x624は微妙に4:3からずれているので注意。
2.機能対応 ニコニコのコメント
まず結論。以下のようなコードを用意する。
Assets/Niconico.cs
using System.Collections; using System.Collections.Generic; using System.Runtime.InteropServices; using UnityEngine; // https://atsumaru.github.io/api-references/comment/ // https://docs.unity3d.com/ja/current/Manual/webgl-interactingwithbrowserscripting.html namespace TSKT { #if !UNITY_EDITOR && UNITY_WEBGL public static class Niconico { [DllImport("__Internal")] public static extern void ChangeScene(string str); [DllImport("__Internal")] public static extern void ResetAndChangeScene(string str); [DllImport("__Internal")] public static extern void PushContextFactor(string str); [DllImport("__Internal")] public static extern void PushMinorContext(); [DllImport("__Internal")] public static extern void SetContext(string str); } #else public static class Niconico { public static void ChangeScene(string str) { Debug.Log("ChangeScene : " + str); } public static void ResetAndChangeScene(string str) { Debug.Log("ResetAndChangeScene : " + str); } public static void PushContextFactor(string str) { Debug.Log("PushContextFactor : " + str); } public static void PushMinorContext() { Debug.Log("PushMinorContext"); } public static void SetContext(string str) { Debug.Log("SetContext : " + str); } } #endif }
Assets/Plugins/Niconico.jslib
mergeInto(LibraryManager.library, { ChangeScene: function (str) { window.RPGAtsumaru.comment.changeScene(Pointer_stringify(str)); }, ResetAndChangeScene: function (str) { window.RPGAtsumaru.comment.resetAndChangeScene(Pointer_stringify(str)) }, PushContextFactor: function (str) { window.RPGAtsumaru.comment.pushContextFactor(Pointer_stringify(str)) }, PushMinorContext: function () { window.RPGAtsumaru.comment.pushMinorContext() }, SetContext: function (str) { window.RPGAtsumaru.comment.setContext(Pointer_stringify(str)) }, });
使い方
コメントを流したいタイミングでいい感じにChangeScene
やPushContextFactor
、PushContextFactor
を呼べば良い。例えばBattleシーンに入ったらChangeScene("Battle")
といった具合。
おおざっぱに言うと、状態が一致するときに打ち込まれたコメントが画面に流れることになる。どうすればいい感じにコメントが流れるかはゲームの設計に依るので、アツマールのリファレンスを読み込んで考えよう。
ツクールMVの実装にあわせるなら、マップの移動でChangeScene(マップid)
、セリフの表示でPushContextFactor(セリフid)
といった具合か。
ちなみにPushContextFactor
とPushMinorContext
はサーバーアクセスが発生しないっぽいのでこの二つは気軽に呼んでよさそう。
どういうコードなのか
UnityWebGLからjsの関数を呼ぶためのあれこれ。以下を参照。
docs.unity3d.com atsumaru.github.io
課題
スマホ対応
そもそもUnityWebGLがスマホ非対応で警告が出る。が、警告を無視すると動いてくれたりする。画面サイズがおかしなことになる。
ほかのAPI対応
優先順位が高そうなのはスコアボードあたりか。