As someone who has produced dozens of videos on doing things with Twine and wrote most of the Twine Cookbook, a pretty common question I get from people is some version of the following: “How can I use Twine with Unity?”
The short answer is this: you can’t easily do it. Unity is not a web browser.
The long answer is much more complicated than I think some people who ask the question realize. It has to do with how Twine works and why Unity, by itself, is not designed to handle HTML.
How Twine Works
Something to realize when using the term Twine is that there is not a single “Twine”, but several story formats. Twine itself is the editor. It packages a story for use with a web browser, but does not “run” the data contained within the HTML itself. When running a story in a web browser, the different story formats act as a “runtime” and transform the data the Twine editor packaged into a presentation for the user to interact with when playing it.
Representation and Runtimes
HTML is a “markup” language. This is part of the acronym itself: HyperText Markup Language. This means it gives structure to a document and “marks up” where certain parts are within a text. For example, it “marks” where certain parts should have emphasis or be represented by strong emphasis. It does this through using tags.
An opening tag marks the beginning of some markup. It then contains some content, and the end of the content is signaled using an ending tag. A common example for “marking up” a document might look like the following:
<em>This would have emphasis!</em>
In the above example, the opening tag is <em>, its content is “This would have emphasis!”, and the closing tag is </em>. The use of the tags “mark up” the existing content, “This would have emphasis!”, and explains how to present its structure.
HTML is a representational language. It “represents” some data through a combination of text and its “mark up” to define the structure and other details for another program to read. In the case of a HTML file and a web browser, the HTML is the representation of some data and the web browser is a runtime. It “runs” the representation of the data.
Web browsers “run” HTML, a representation of data.
Unity is not a Web Browser
Unity is a game engine. It uses the programming language C# to allow developers to change some internal values while the engine is “running.” In much the same way a web browser creates an internal object based on a HTML file, Unity does the same thing on a larger scale. It makes assumptions about common systems a developer might want to use including rendering (drawing things to a screen) and physics (testing if one or more things overlap with their positions within a space). Unity then allows developers to “script” different things using the C# programming language.
Yarn and YarnSpinner are based on Twee-like code. Using similar functionality to the macros existing in some story formats as part of Twine, Yarn and YarnSpinner use a single “story format” for creating narrative projects. This eliminates the problem Cradle ran into of needing to support multiple story formats and any changes introduced between versions. Because Yarn is a different project than Twine, this also means it is not beholden to any changes a story format used with Twine might introduce or a need to follow the Twee specification. As YarnSpinner is designed to work with Unity via its plugin, this makes creating projects using code like, but different than, Twine possible without needing to use the Twine editor or its story formats to interpret HTML story data.
“Will Twine ever work with Unity?”
No, probably not.
It is possible to embed a web browser within Unity and send instructions to it and receive output from it to use Twine HTML within Unity. However, such a solution introduces many more problems than it solves. Other Twine-like solutions exist for working with Unity like YarnSpinner. For the purpose of creating interactive narrative or dialogue systems, Ink + Unity plugin or Fugus exist to solve those problems, too. These do not have the same problems of needing to read HTML nor keep up with potential story format changes for trying to read Twee code written in the “language” of different story formats.
“Could someone write a Twine story format that understood Yarn?”
Someone should, if they haven’t already.
YarnSpinner uses a text format very similar to Twee. This should make it possible to create a story format for reading “Yarn stories” as they exist in a Twine HTML format and allow the story data to also be converted into Yarn for use with YarnSpinner. I am not aware of a project doing this, but would not be surprised if it was under development. This would not solve the problem of working with Twine in Unity, of course, but it would open a path for writing a “story” capable of being understood, with some minor changes, across both Twine and YarnSpinner projects.