A helpful Ink-Knot PropertyDrawer


TL;DR: I've made a struct & propertyDrawer to dynamically create a dropdown menu to pick any knot from your assets folder.

~~~

Hey folks, so got another useful bit of editor code to share, this one is here to deal with a common problem in the project:

  1. we have a lot of ink knots as our story files are little solo chats with another character
  2. we need to trigger these knots from multiple places in the project
  3. and because strings are strings, we want to avoid misspellings

Now the best way to deal with this is to just list a dropdown of all the possible options you can pick... which is actually fairly straightforward!

Previously in the project I already made this little tool to help with testing a story in the story-player scene. So I already had the actual search code ready, next up was making the property drawer itself. A catch I ran into with the drawer though: you kind of need to side-step setting the serialisedProperty directly, instead setting a local string, then setting the string afterwards. (This is because the Menu dropdown is technically happening after `OnGUI(...)` finishes, so you've got some kind of Async thing going on)

Here's the code itself

~~~

For reference if you want to learn how this was put together, here are the various strands to research:

  • Use AssetDatabase.FindAssets to find all assets under a given path
  • With that, you've got the GUIDs for each one, so turn those into file paths
  • Now you can use File.OpenText with each path to read it line-by-line (This MSDN article covers the basic & provides follow-up links)
    • Note the using(...) construct, this is a handy thing that opens, then disposes the StreamReader for you!
  • Then, you just read through the .ink text file line-by-line checking for "==" with StartsWith
  • This then gets fed into a GenericMenu (which isn't much changed from unity's docs page)

Because this happens in edit-time, I find you can afford to do some proper file-crawls like this, however once this scales up to 100s of ink files you'd want to avoid a crawl every time you click on a dropdown. That's why it uses a static dictionary. There may be other optimisations to add, but this is a first implementation.

Get A Day of Maintenance (Jam Version)

Leave a comment

Log in with itch.io to leave a comment.