chiarpavel / ui-navigation

UI navigation for Unity

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

UINavigation

UI navigation for Unity

version 1.3.0

Download unitypackage

How to use

Setup

  1. Add a Navigator component to a GameObject. It doesn't matter where, just make sure it's enabled.
  2. Add NavScreen components to all the menu screens you want to be able to navigate through.
  3. (optional) Add handlers to the NavScreen's events to set up or clean up the screens, useful if a screen's content depends on external state, such as a search results page.
  4. (optional) Add one of the NavScreens to your Navigator's Initial Screen field if you have a start-up screen
  5. (optional) Add a transition to your Navigator's Transition field. There are several transitions included, or you can create your own by extending TransitionBase.

Navigation

Simply call Navigator.GoTo. This will navigate to the NavScreen passed as a parameter and add it to the navigation history. Call Navigator.GoBack from your Back buttons to return to the last NavScreen and remove the current one from the navigation history.

How to create a transition

The easiest to create a new transition is to modify one of the existing ones.

You should extend TransitionBase and implement the Play and PlayReverse coroutines.

Make sure to call NavScreen's OnEvent methods (OnShowing, OnShown, OnHiding, OnHidden) at the appropriate time if you plan on using NavScreen's events.

Because TransitionBase is a ScriptableObject you need to create an asset. Add a CreateAssetMenuAttribute to be able to create one using the editor.

See SlideTransition.cs or FadeTransition.cs for more details.

Public API

Navigator

  • initialScreen: NavScreen - This screen is added to the Navigator's stack on startup.
  • transition: TransitionBase - The transition's Play and PlayReverse methods are called when going from one screen to another.
  • emptyPathAllowed: bool - If set to false calling Navigator.GoBack when there is only one screen left in the stack will do nothing.
  • escapeKeyHandled: bool - If set to true pressing the Android back button, or the keyboard Escape key will call Navigator.GoBack.
  • Path: String - A string representation of the list of NavScreens the Navigator has in the current stack.
  • GoTo(targetScreen: NavScreen): void - Adds targetScreen to the stack and uses the set transition (if there is one, otherwise simply raises the apropriate NavScreen events).
  • GoBack(): void - Removes the NavScreen at the top of the stack using the set transition.
  • Reset(): void - Clears the screen stack and adds the initial screen if one is set.

NavScreen

  • RectTransform: RectTransform - The GameObject's RectTransform.
  • CanvasGroup: CanvasGroup - The GameObject's CanvasGroup. If the object doesn't have one a new CanvasGroup is added.
  • OnShown(): void - Called at the end of a transition when the screen has come into view.
  • OnHidden(): void - Called at the end of a transition when the screen has left view.
  • OnShowing(): void - Called at the start of a transition when the screen is coming into view.
  • OnHiding(): void - Called at the start of a transition when the screen is leaving view.

TransitionBase

  • Play(currentScreen: NavScreen, nextScreen: NavScreen): IEnumerator - Coroutine that is started when moving "forward" or "deeper" into a menu.
  • PlayReverse(currentScreen: NavScreen, previousScreen: NavScreen): IEnumerator - Coroutine that is started when going "up" or "back" to a previously visited menu.

About

UI navigation for Unity

License:MIT License


Languages

Language:C# 100.0%