# 快速食用

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

# 相关链接

  • inkle 官网
  • inky 编辑器
  • 相关教程参考 - 基础
  • 相关教程参考 - 高级
更新于 阅读次数

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

Fasty 微信支付

微信支付

Fasty 支付宝

支付宝

Fasty 贝宝

贝宝