Unityでノベルゲームの基礎 テキストデータの表示 スクリプト解説!
はおっ(0^∀^)ノ 渚です! circlesana.hatenablog.com
を,少し前に書きました。 読み込んだら,表示でしょう? ということで,テキストデータの表示について解説していきます。
void NextText()
{
// 次の文字
stageDataIndex++;
if (stageDataMax < stageDataIndex)
return;
//Debug.Log("MAX" + stageDataMax + "INDEX" + stageDataIndex);
// 文章表示領域の,文章を消す。
DisplayArea.text = "";
for (int i = 0; i < stageData[stageDataIndex].Count; i++)
{
if (stageData[stageDataIndex][i].Length > 1 && stageData[stageDataIndex][i].Substring(0, 2) == "@@")
{
// @@の場合
// @@?0の ?の番号により,背景を設定する
DrawBackGround(stageData[stageDataIndex][i].Substring(2, 1));
// @@0?の ?の番号により,キャラクターを設定する
DrawCharacters(stageData[stageDataIndex][i].Substring(3, 1));
if (stageData[stageDataIndex][i].Length >= 5)
{
if (stageData[stageDataIndex][i].Substring(4, 1) == ":")
{
// 名前を表示する
NameDisplayArea.text = stageData[stageDataIndex][i].Substring(5);
}
}
else
{
// 名前を非表示にする
NameDisplayArea.text = "";
}
}
else if (stageData[stageDataIndex][i].Length > 1 && stageData[stageDataIndex][i].Substring(0, 2) == "--")
{
// 選択肢を表示する
SelectButton[i - 1].gameObject.SetActive(true);
SelectButton[i - 1].transform.FindChild("Text").GetComponent<Text>().text = stageData[stageDataIndex][i].Substring(5);
DisplayArea.text += stageData[stageDataIndex][i].Substring(5) + "\n";
Selected = true;
}
else if(stageData[stageDataIndex][i].Length > 1 && stageData[stageDataIndex][i].Substring(0, 2) == "ED")
{
// 最後まで行ったので,タイトルに戻る
SceneChange("Title");
}
else
{
// 本文なので,文章表示領域に追記する。
DisplayArea.text += stageData[stageDataIndex][i] + "\n";
}
}
//Debug.Log("stageData" + stageData[stageDataIndex]);
//Debug.Log("pattern" + stageData[stageDataIndex].Substring(0, 2));
}
コメントを書いたので,どういう雰囲気で表示しているかはつかめると思います。
stageDataIndexは,int型で,どのかたまりの文章を表示するかを決めています。 選択肢が出た後,その選択肢に飛ぶときも,stageDataIndexの値を変更して,飛ぶようにしています。
DisplayAreaは,文章を表示する部分になります。 publicで,変数を定義した上で,Unity上でuGUIのTextを作り,InspectorにD&Dで設定すればよいかと。
DrawBackGroundや,DrawCharacters関数は,必要に応じて作ればよいかと思います。
NameDisplayAreaは,ノベルゲーでよくある,しゃべってる人の名前部分ですね。 これも,uGUIのTextを事前に作っておき,InspectorにD&Dで設定すればよいかと。
SelectButtonについては,ちょっとややこしいですが・・・ 1.uGUIのButtonを事前に作る 2.Scriptで,public Button[] SelectButton;を宣言する 3.D&Dで設定する 4.以下のサイトを見て,イベントを追加する。 hiyotama.hatenablog.com 5.Unity上でSelectButtonを,非アクティブにする。 こんな感じのことをすれば,大体実装できると思います。 あとは,ボタンを押したときの処理に, 適切な位置まで,文章を飛ばす処理と ボタンを非アクティブにする処理を入れておけば出来そうです。
SceneChange関数は,最近Unityの仕様が変わったので,5.3からかな 昔の,Application.LoadLevel("Title")ではなく,
using UnityEngine.SceneManagement;を書いたうえで SceneManager.LoadScene("Title");を書きましょう。
あとは,とくに気になる部分もないかと思います。
もしあったら,メールくれるとうれしいです。 コメントは,正直気付かないorz Mail: sana.inquiry[at]gmail.com
ではでは