快速食用

  1. 下载inky编辑器
  2. 编写ink文件
  3. 下载安装unity支持
  4. 找到ink示例,将对应的ink 生成的json导入运行游戏即可。

unity使用流程

在unity中,我们需要通过代码来驱动我们的剧情的进行。
1. 首先我们需要创建至少2种Prefab,一种表示文字,一种表示按钮。

  1. 然后我们需要创建一个story对象,载入对应的json文件
    story = new Story (inkJSONAsset.text);

  2. 判断story能否继续,若能继续使用代码创建对应的游戏对象

while (story.canContinue)
{
// Continue gets the next line of the story
string text = story.Continue ();
// This removes any white space from the text.
text = text.Trim();
// Display the text on screen!
CreateContentView(text);
}

// Display all the choices, if there are any!
if(story.currentChoices.Count > 0) {
    for (int i = 0; i < story.currentChoices.Count; i++) {
        Choice choice = story.currentChoices [i];
        Button button = CreateChoiceView (choice.text.Trim ());
        // Tell the button what to do when we press it
        button.onClick.AddListener (delegate {
            OnClickChoiceButton (choice);
        });
    }
}
// If we've read all the content and there's no choices, the story is finished!
else {
    Button choice = CreateChoiceView("End of story.\nRestart?");
    choice.onClick.AddListener(delegate{
        StartStory();
    });
}

void CreateContentView (string text) {
    Text storyText = Instantiate (textPrefab) as Text;
    storyText.text = text;
    storyText.transform.SetParent (canvas.transform, false);
}

// Creates a button showing the choice text
Button CreateChoiceView (string text) {
    // Creates the button from a prefab
    Button choice = Instantiate (buttonPrefab) as Button;
    choice.transform.SetParent (canvas.transform, false);

    // Gets the text from the button prefab
    Text choiceText = choice.GetComponentInChildren<Text> ();
    choiceText.text = text;

    // Make the button expand to fit the text
    HorizontalLayoutGroup layoutGroup = choice.GetComponent <HorizontalLayoutGroup> ();
    layoutGroup.childForceExpandHeight = false;

    return choice;
}
  1. 要记得每次显示时清除上一次的显示结果

相关链接