Unity用対話システム

games の対話システムは、ゲーム世界内でプレイヤーとキャラクターの間でインタラクティブで没入型の会話を可能にするメカニズムです。これは、プレイヤーが 非プレイヤー キャラクター (NPC) または他のエンティティと関わるためのコミュニケーション チャネルとして機能し、ストーリーテリング、クエストの進行、キャラクター開発、世界構築の手段を提供します。

対話システムの主な目的は、プレイヤーが選択を行い、ゲームの物語に影響を与え、ゲーム内のキャラクターとの関係を形成できるようにすることで、プレイヤーにとってダイナミックで魅力的なエクスペリエンスを生み出すことです。適切に設計された対話システムは、プレイヤーの没入感、感情移入、リプレイ性を向上させることができます。

Unity でのゲーム開発に関しては、本格的な対話システムを最初から作成するのは非常に大規模な作業になる可能性がありますが、単純なサンプルから始めることは可能です。以下の例では、C# および Unity UI システムを使用した基本的なテキストベースの対話システムを取り上げます。これは単なる出発点であり、特定のニーズに基づいて拡張およびカスタマイズできることに注意してください。

ダイアログマネージャーを作成する

  • という新しいスクリプトを作成し、"DialogueManager" という名前を付けて、その中に以下のコードを貼り付けます。
using System.Collections;
using UnityEngine;
using UnityEngine.UI;

public class DialogueManager : MonoBehaviour
{
    public Text dialogueText;
    public Button choice1Button;
    public Button choice2Button;
    public Button nextButton;

    private Dialogue currentDialogue;
    private int currentLineIndex = 0;

    void Start()
    {
        // You can load your dialogue data from an external source (e.g., JSON, XML) or create it programmatically.
        // For simplicity, we'll create a sample dialogue here.
        currentDialogue = CreateSampleDialogue();

        // Set up event listeners for buttons
        choice1Button.onClick.AddListener(OnChoice1Selected);
        choice2Button.onClick.AddListener(OnChoice2Selected);
        nextButton.onClick.AddListener(OnNextButtonClicked);

        // Start the dialogue
        StartDialogue();
    }

    private void StartDialogue()
    {
        currentLineIndex = 0;
        DisplayLine(currentDialogue.lines[currentLineIndex]);
    }

    private void DisplayLine(DialogueLine line)
    {
        dialogueText.text = line.text;
        choice1Button.gameObject.SetActive(line.hasChoice);
        choice2Button.gameObject.SetActive(line.hasChoice);
        nextButton.gameObject.SetActive(!line.hasChoice);
    }

    private void OnNextButtonClicked()
    {
        currentLineIndex++;
        if (currentLineIndex < currentDialogue.lines.Length)
        {
            DisplayLine(currentDialogue.lines[currentLineIndex]);
        }
        else
        {
            // Dialogue is over
            EndDialogue();
        }
    }

    private void OnChoice1Selected()
    {
        HandleChoice(currentDialogue.lines[currentLineIndex].choice1);
    }

    private void OnChoice2Selected()
    {
        HandleChoice(currentDialogue.lines[currentLineIndex].choice2);
    }

    private void HandleChoice(Choice choice)
    {
        // Handle the chosen choice (e.g., change variables, trigger events)
        Debug.Log("Selected Choice: " + choice.text);

        // Advance to the next line
        currentLineIndex++;
        DisplayLine(currentDialogue.lines[currentLineIndex]);
    }

    private void EndDialogue()
    {
        // Reset the dialogue UI or close the dialogue box
        Debug.Log("End of Dialogue");
    }

    // Sample dialogue data (you can replace this with loading from an external source)
    private Dialogue CreateSampleDialogue()
    {
        Dialogue dialogue = new Dialogue();

        dialogue.lines = new DialogueLine[]
        {
            new DialogueLine("Hello there! Welcome to the Unity dialogue system example.", false),
            new DialogueLine("What would you like to do?", true, new Choice("Go on an adventure"), new Choice("Stay here")),
            new DialogueLine("Great choice! Have a fantastic adventure!", false),
            new DialogueLine("That's okay. Sometimes staying in one place can be just as exciting!", false),
            new DialogueLine("Thanks for trying out the Unity dialogue system example!", false)
        };

        return dialogue;
    }
}

[System.Serializable]
public class Dialogue
{
    public DialogueLine[] lines;
}

[System.Serializable]
public class DialogueLine
{
    public string text;
    public bool hasChoice;
    public Choice choice1;
    public Choice choice2;

    public DialogueLine(string text, bool hasChoice, Choice choice1 = null, Choice choice2 = null)
    {
        this.text = text;
        this.hasChoice = hasChoice;
        this.choice1 = choice1;
        this.choice2 = choice2;
    }
}

[System.Serializable]
public class Choice
{
    public string text;

    public Choice(string text)
    {
        this.text = text;
    }
}

DialogueManager スクリプトの Unity に UI Text オブジェクトと Button オブジェクトを設定するには、次の手順に従います。

  • Unity エディターで、[階層] ウィンドウを右クリックし、["UI -> Text"] を選択して、新しい UI テキスト オブジェクトを作成します。
  • UI Text オブジェクトの名前を次のように変更します。 "DialogueText."
  • 同様に、3 つの UI Button オブジェクトを作成します。1 つは選択肢 1 用、1 つは選択肢 2 用、もう 1 つは "Next" ボタン (ダイアログを進めるため) 用です。
  • ボタンにそれぞれ "Choice1Button," "Choice2Button," および "NextButton" という名前を付けます。
  • 好みのレイアウトに従って、キャンバス上に UI テキストとボタンを配置します。UI テキストを画面の中央に配置し、ボタンをテキスト ボックスの下に配置することもできます。
  • ゲームの視覚スタイルに合わせて、UI テキストのテキストのフォント、サイズ、色、その他のプロパティを調整します。
  • 色やテキストラベルの変更など、UI ボタン​​の外観をカスタマイズします。
  • Unity エディターで、"DialogueManager" ゲームオブジェクト (スクリプトを attach するために作成したもの) を選択します。
  • 「インスペクター」ウィンドウに、"Dialogue Manager" スクリプト コンポーネントが表示されます。UI Text オブジェクトと Button オブジェクトを Hierarchy ウィンドウからスクリプト コンポーネントの対応するパブリック フィールドにドラッグ アンド ドロップします。
  • これらの参照を割り当てることで、DialogueManager スクリプトはシーン内の UI テキストとボタンにアクセスできるようになり、ダイアログ中に必要に応じてテキスト コンテンツを更新し、その表示を制御できるようになります。
  • シーンを保存して変数の変更を保存します。

ゲームを実行するとき、またはダイアログ トリガーを操作するとき、DialogueManager はキャンバス上の参照された UI 要素を使用してダイアログ テキストと選択肢を表示できる必要があります。

結論

ゲームの効果的な対話システムは、プレーヤーに主体性、影響力、仮想世界への関与感を提供し、ゲーム体験をより豊かで魅力的なものにします。ゲームのナラティブとインタラクティブなストーリーテリングがより洗練されるにつれて、対話システムはプレイヤーの旅を形成し、記憶に残るゲーム体験を生み出す上でますます重要な役割を果たします。

おすすめの記事
Unity用ヘリコプターコントローラー
Unity の 2D プラットフォーマー キャラクター コントローラーにダブル ジャンプ サポートを追加する
Unityでクレーンコントロールを作る方法
Unity用カーコントローラー
キャラクター コントローラー Unity でリジッドボディをプッシュする機能を追加する方法
Unity用飛行機コントローラー
Unity でパルクール システムを実装する