2017-02-04 55 views
1

我正在研究一個簡單的樹型對話系統。它是一個基本的點擊遊戲,推動故事,有不同的選擇來選擇,讓玩家走向不同的故事路徑。基於樹型的高效對話系統?

我做的是,我創建了一個叫做Convo的過多的gameObject。每個Convo都包含Text(Component)和Children來確定屏幕上顯示哪個角色,並在這些孩子中顯示一個標籤來顯示他們的變形(例如傷心的快樂等)。 防爆

  • A-0-1(文字: 「約翰:嘿,這是怎麼回事」)(與標籤 「選擇」,如果這是最後康沃)
    • 約翰(與標籤「空閒「)
    • 簡(帶標籤 「激怒」)

Story path picture

有2個變量:CurrentDialogue(字符串)和DialogueTracking(int)。

在每次點擊時,我檢查當前的convo是否有標籤「Choice」。如果沒有,我使用CurrentDialogue(字符串)來查找具有確切名稱的gameobject並顯示它的文本。然後我使用DialogueTracking修改CurrentDialogue(字符串)從A-0-1到A-0-2等,並且當有標籤「choice」(Ex.A-0-4)時,將DialogueTracking增加一個,

),DialogueTracking(int)重置爲1,所以在玩家選擇一個選項後,它是A-1-1而不是A-1-5。

這是問題,經過很多convo和許多故事路徑後,我意識到我的新手錯誤。代碼變成大o'if/else(比較哪個選擇走哪個路徑)

我對編程和團結相當陌生,如果任何人都可以引導我在正確的方向上幫助我學習如何創建這個系統是乾淨和高效的,那真是太棒了!

謝謝!

+0

使用接口也許結構呢? https://unity3d.com/learn/tutorials/topics/scripting/interfaces – Absinthe

回答

1

我會建議製作DialogueManagerComponent這將舉行所有的對話,每個這些應該舉行參考其他對話。然後顯示對話框後,你可以檢查它是否有多個孩子或只有一個(或沒有),並顯示一些對話框/彈出選擇從這些鍵。

在代碼示例它會像:

[Serializable] 
public class Dialogue 
{ 
    [SerializeField] 
    Dictionary<string, Dialogue> _dialogues; 

    [SerializeField] 
    string _dialogueText; 

    public Dialogue(string text) 
    { 
     _dialogues = new Dictionary<string, Dialogue>(); 
     _dialogueText = text; 
    } 

    public string GetText() { return _dialogueText; }  

    public bool HasManyPaths() { return _dialogues.Count > 1; } 

    public string[] GetKeys() { return _dialogues.Keys; } 

    public Dialogue GetDialogue(string key) { return _dialogues[key]; } 
} 

現在您應該在Unity檢查中填補這一做出DialogueManagerComponent來管理Dialogue S:

public class DialogueManager 
    : Component 
{ 
    [SerializeField] 
    Dialogue _currentDialogue; 

    public void InitializeDialogue() 
    { 
     string text = _currentDialogue.GetText(); 
     // display the text 
     if (_currentDialogue.HasManyPaths()) 
     { 
      string[] keys = _currentDialogue.GetKeys(); 
      // display keys; 
      string selected_key = // user input here 
      _currentDialogue = _currentDialogue.GetDialogue(selected_key); 
     } 
     else 
     { 
      string key = _currentDialogue.GetKeys()[0]; 
      _currentDialogue = _currentDialogue.GetDialogue(key); 
     } 
    } 
} 

使用這種方法,你可以只需在Unity編輯器中進行對話並添加MonoBehaviour腳本,如下所示:

public void Start() 
{ 
    GetComponent<DialogueManager>().InitializeDialogue(); 
} 
使對話樹將是

簡單的例子:

{ currentDialogue [ "hello! choose 1 or 2" ] 
    1.{ dialogue [ "you've chosen 1" ] 
     . { dialogue [ "this ends dialogue" ] } 
    } 
    2. { dialogue [ "you've chosen 2" ] 
     . { dialogue [ "now choose another number between 3 and 4" ] 
      3. { dialogue [ "you've chosen 3" ] } 
      4. { dialogue [ "you've chosen 4" ] } 
     } 
    } 
} 
+0

感謝您的回覆!說實話,我只懂幾行代碼,因爲這是我第一次瞭解序列化。據我所知,我以前的文本(組件)將被替換爲單個對話(腳本)來存儲整個對話樹呢?和一個單一的DialogueManager(腳本)是負責從對話(腳本)獲取_currentDialogue並顯示文本,並獲得輸入移動到不同的路徑,如果有選擇的權利?我覺得我錯了,你能澄清我嗎?謝謝! – Irfx

+0

@Iffx基本上你已經正確地理解了它。事情是我認爲你必須爲此創建一些自定義檢查器,但我不太確定它是否需要。 :)如果你需要更多的評論,請評論,我會盡量做一些截圖,並編輯這個答案更清晰。 –