Dynamic menu generation from JSON in C#!


So it was nice having the fun text dialog boxes going, and I imagine they'll come in handy, but I really wanted to get generic menuing (there's that menu-as-a-verb thing again) implemented.

I got most of the way there, but I was spending too much time screwing around with getting the data structure initializations right in C# just to get test data in... so I dropped that and went straight to generating menus from JSON. I'm very pleased to have this working.

The menus there were generated from this (and navigated with this, which I just got recently and am completely in love with):

{:panels [{:dimensions {:width 0.3}
           :options
           [["Heading" nil]
            ["Pick me!"
             {:action "panels"
              :panels [{:dimensions {:left 0.3
                                     :width 0.7}
                        :options
                        [["What would you like to do?" nil]
                         ["Party" {:action "panels"
                                   :panels [{:dimensions {:left 0.1
                                                          :top 0.5
                                                          :width 0.4
                                                          :height 0.2}
                                             :options [["Woohoo!" nil]]}]}]
                         ["Relax" {:action "panels"
                                   :panels [{:dimensions {:left 0.3
                                                          :top 0.5
                                                          :width 0.4
                                                          :height 0.2}
                                             :options [["Ahhhh." nil]]}]}]
                         ["Contemplate" {:action "panels"
                                         :panels [{:dimensions {:left 0.5
                                                                :top 0.5
                                                                :width 0.4
                                                                :height 0.2}
                                                   :options [["Hmmm..." nil]]}]}]]}]}]]}]}

Well... the JSON representation of that (it's a Clojure data structure). But this is a lot more compact than pretty-printed JSON, so hopefully it's easier for everyone else to read as well. If nothing else, it won't double the length of this page.

Anyway. The action parameter value panels tells Unity it needs to use these values to render one or more menu panels, as well as what options they should have in them, and what action those options should support (if any; a null/nil action is considered a heading and not an active menu item). They're added to the current menu's focus stack; a menu can have any number of panels, and it sends input events to the topmost one, which will close and destroy itself when you back out of it.

While this is potentially useful for generating menus on the server, and it'll certainly be used that way for things like item, unit, and equipment menus, I plan on having all of the menus built from JSON, even if they're baked into the build. It's just massively easier and less finicky to switch things around and fix them without introducing new bugs when it's in a dead simple notation like this than if it's baked into a C# file somewhere. Bleh.

The only other thing I had lined up with this -- which I expected to take longer than it did -- was centralizing input handling so changing bindings would be easy. That sounds... really dull, though. So I think I might start on battle mechanics, now that I've got the menus working. :)

Leave a comment

Log in with itch.io to leave a comment.