Ink剧情编排

快速食用

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

unity使用流程

在unity中,我们需要通过代码来驱动我们的剧情的进行。

  1. 首先我们需要创建至少2种Prefab,一种表示文字,一种表示按钮。

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

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
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();
});
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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. 要记得每次显示时清除上一次的显示结果

书写规则

ink有一套自己的书写格式,不符合规范时无法编译。

  • 选项 + xx 在选择后会重复 选项
  • 选项 + [xx] 在选择后不会重复 选项

Unity支持

更新时间:2022年12月8日16:39:09

INk提供了Unity插件来支持。我们可以在github或者是AssterStore中获取。

在Unity导入.ink文件可以在右侧检视图看到如下界面:

在这个界面可以对整个故事流程进行概览。

同时每次修改.ink文件后,都会自动生成一个json文件。我们要使用的就是生成的Json文件。

image-20221215153223990

常用功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//设置变量
Ink.variablesState["hp"] = 300;

//游戏端函数
Ink.BindExternalFunction("log", (string info) => { this.Log("日志:" + info); });

//保存
string savedJson = _inkStory.state.ToJson();

//读取
_inkStory.state.LoadJson(savedJson);

//从节点获取元数据
var tags= x.TagsForContentAtPath("DATA");
tags.ForEach(s=>this.Log("Tag|"+s));

2023年6月14日09:58:30 更新

Text Animator for Unity

这里推荐一个插件用于字符动画的播放Text Animator for Unity

只需在Text上挂上相关组件即可以使用来实现文字的动画效果。

Text Animator

同时插件提供了很多内置的效果:

image-20230614094916425

使用ink和textAnimator配合实现的对话效果预览:

效果预览

效果预览

相关链接