[UNITY]2DRPG開発日誌 #65 FungusでC#スクリプトからブロックやコマンドを追加する方法はあるか?

追加するのは無理。しかし、諦めるな!

さて、Fungusは公式Discordがかなり活発で、開発者と思われる方が常駐して世界中の人々の質問に英語で答えています。

そのなかで、Fungusのブロックやコマンドをスクリプトから追加する方法についての(恐らく開発者と思われる方からの)発言がありましたので引用します。

  • 質問(DeepL翻訳)

cobby363 — 2021/02/02
Hey guys, I'm trying to go about using a JSON file to populate text boxes or say commands in Fungus. I have a c# script that can read in my json file, that part is fine, however I have reference to a block in my script but I cannot see any ways of adding on a new command in this script, for example a say command. Does anybody know how I can go about this? Or else a way to simply add a say command with a string via a c# script would be great and I'd be able to figure out the rest? Thanks guys!

FungusでJSONファイルを使って、テキストボックスやコマンドを入力しようとしています。JSONファイルを読み込むC#スクリプトがあり、その部分は問題ないのですが、スクリプト内のブロックへの参照があり、このスクリプトに新しいコマンド、例えばsayコマンドを追加する方法が見当たりません。 どなたか、この方法についてご存じないでしょうか?それとも、C#スクリプトで文字列を使用してsayコマンドを追加する方法があれば最高で、あとは自分で考えることができるでしょうか?ありがとうございます。

  • 回答(DeepL翻訳)

Wolfrug — 2021/02/02
Hey ho.

This is a very, very common question, and unfortunately the answer is that you're approaching the issue from entirely the wrong end.

Fungus is fundamentally a VISUAL SCRIPTING ENGINE that uses FLOWCHARTS to allow people who are not proficient in C# to create their own games/visual novels. Every Fungus project needs to be done -in Fungus- using the tools Fungus provides -from the start-. Fungus is -not- a library or a plugin you use to convert e.g. your own data classes, JSON files or excels into "story" to slap onto an existing game.

I understand what it is you want: you want to be able to load your already-written story JSON (which might well contain information about e.g. characters, localization data, strings, in-text commands etc) into Fungus, creating neat Flowcharts that you can then export, change, import, etc. at will. This is -not possible-, not in the least. There may be some commercial products out there that allow this (e.g. Articy Draft) but even then you will need to adhere fully to THEIR data standards.

Fungus -does- use JSON files for localization however, so if you can convert your JSON files to use Fungus' style, you can gain access to the strings. After that however you will need to -manually- create the story flow inside Unity using Flowcharts, blocks, commands etc., and then reference your JSON string table using variable references ({$jsonkey_1} etc).

The absolutely closest you can get to 'automatic' JSON -> Fungus is using the Lua integration, as you can technically create a Lua function that can do stuff like "say all JSON strings named {$jsonkey_x} where x = 0-12 in order", but you'll still need to convert your JSON file to a Fungus stringtable, and you'll need to know a fair bit of Lua, and you will obviously still need to do stuff like set up characters and say dialogs, and manually create all menu dialogs (choices).

ヘイホー。

これはとてもとてもよくある質問で、残念ながら答えは、あなたが全く間違った方向からこの問題に取り組んでいるということです。

Fungusは基本的にFLOWCHARTSを使ったVISUAL SCRIPTING ENGINEで、C#に熟練していない人でも自分のゲームやビジュアルノベルを作ることができるようになっています。FungusのプロジェクトはすべてFungusの中で、Fungusが提供するツールを使って、最初から行われる必要があります。Fungusはライブラリでもプラグインでもなく、例えば自作のデータクラスやJSONファイル、Excelを「story」に変換して既存のゲームに貼り付けるためのものです。

あなたが欲しいのは、すでに書かれたストーリーJSON(キャラクター、ローカライズデータ、文字列、テキスト内コマンドなどの情報を含む)をFungusにロードして、きちんとしたフローチャートを作り、それを自由にエクスポート、変更、インポートできることなのでしょうが、それは不可能です。これは「ありえない」ことです。これを可能にする商用製品(例:Articy Draft)もあるかもしれませんが、その場合でも、そのデータ標準を完全に遵守する必要があります。

FungusはJSONファイルを使ってローカライズしているので、JSONファイルをFungusのスタイルに変換すれば、文字列にアクセスすることは可能です。ただし、その後、フローチャート、ブロック、コマンドなどを使ってUnity内でストーリーの流れを手作業で作成し、変数参照({$jsonkey_1}など)を使ってJSON文字列テーブルを参照する必要があります。

技術的には「{$jsonkey_x}という名前のJSON文字列をx=0~12の順番で発言する」ようなLua関数を作ることができますが、JSONファイルをFungus文字列テーブルに変換する必要がありますし、Luaの知識がかなり必要で、キャラクターの設定や発言のダイアログ、すべてのメニューダイアログ(選択肢)の作成なども当然必要になってきます。

要約すると、

かなり頑張れば何でもできるけど、そもそもFungusはそういうアセットじゃないから、不可能と考えたほうがよいです。
jsonやCSV、C#スクリプトからのローカライズが目的なら「Flowchart、Block、Commandを手作業で作って、中身の文章だけを{$jsonkey_1}みたいな変数参照にする」という方法が限界でしょう。

とのことです。

解決方法

要は、ブロックとコマンドだけ手作業で作っておいて、中身をC#スクリプトから制御すればいいわけですね。

その方法はこちらの記事で解説しています!