YousicianGit / UnityMenuSystem

Apache License 2.0
154 stars 46 forks source link

Menu Framework #5

Closed PolymorphiK closed 6 years ago

PolymorphiK commented 6 years ago

Inspired by the talk at Unite (https://www.youtube.com/watch?v=wbmjturGbAQ) I looked over the GitHub repo and was in the process of working on the Menu System for my personal project. I took points from the talk and created a MenuFramework in the last 1-2 days. It's not perfect by any means but if anyone wants to use it I'd like to share it.

The MenuSystem is broken up into parts.

-> View -> Panel -> CanvasView -> Menu -> Overlay

The View is the base class that contains Methods such as Show, Hide etc.

Panel derives from View and can have Navigation Events. What this means is if the Panel in question has UI elements that can be navigated to via Unity's EventSystem the Panel will connect to it.

CanvasView is a view that must have a Canvas attached to it. This allows the ability to sort views.

Menu derives from Canvas view because menus need to be sorted via sort-order to know which renders on-top of what menu. This version of Menu is not a singleton. Multiple instances of Menu can be created though it is not recommended but feel free to do w/e you want to get your project working. Menu supports panels that be can navigated to via Input. Menu receives special Events such as OnBackWasPressed, OnPanelLeft, OnPanelRight.

OnPanelLeft: When this is called if the Menu that is active supports Panels it will navigate to the Panel on the right. The order depends on how it is assigned in the inspector via an array of Panels.

OnPanelRight: Same as OnPanelLeft but going the other direction.

Overlay derives from Canvas but does NOT receive events from Input. Overlay is expected to be a read-only type of UI to display stuff but never interact with anything.

An example of an overlay can be showing statistics about your game like frames-per-second, GPU stats, rendering stats, Ping etc.

I did not rewrite any of the code that is already in the project I just created a folder called MenuFramework that contains the code and an Example folder. The code is commented so you can read through it and implement it in your own project.

I am currently expanding this further for my own project and as I find ways to tweak it I will push updates to this repo so others can benefit from it.

NOTE: Things to note. Panels, Menus, Overlays etc can pass data around via the Context pattern. It's not well implemented I just wanted to see what I could get done in 2 days as I needed to get the UI for my project done ASAP. So if anyone can review and implement the Context pattern better than what it currently is, please do so. I will continue to optimize this as I work on my project.