# 快速食用

  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 能否继续,若能继续使用代码创建对应的游戏对象

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. 要记得每次显示时清除上一次的显示结果

# 书写规则

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

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

# Unity 支持

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

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

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

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

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

image-20221215153223990

# 常用功能

// 设置变量
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 配合实现的对话效果预览:

效果预览

效果预览

# 相关链接

  • inkle 官网
  • inky 编辑器
  • 相关教程参考 - 基础
  • 相关教程参考 - 高级
  • ink/RunningYourInk.md at master · inkle/ink (github.com)
  • Ink 脚本语言学习笔记(一)_嘉神川优衣的博客 - CSDN 博客_unity ink 使用
更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

Fasty 微信支付

微信支付

Fasty 支付宝

支付宝

Fasty 贝宝

贝宝