Skip to content

10. Running the samples (Samples~) ​

The package ships 7 standalone samples under Samples~/. UI chrome uses standard Unity UI Toolkit controls (Button, Label, TextField, Toggle, ScrollView) — no downstream UI package is required.

Requirements ​

  • sus-router + sus-core installed in the project
  • UPM Samples imported: Window → Package Manager → SusRouter → Samples → Import
  • Scene with UIDocument (EventSystem required)
  • Each sample: [RequireComponent(typeof(UIDocument))]

Scene setup ​

  1. Create GameObject → Add Component → UIDocument
  2. Add Component → sample script (e.g. BasicRoutingExample)
  3. GameObject → UI → Event System (if missing)
  4. Play

Overview ​

#SampleRouter features
1BasicRoutingPush, Replace, Back, Home, CurrentRoute
2KeepAliveKeepAlive=true/false, caching
3GuardsBeforeEach, CanEnter, BeforeResolve, redirect
4Modals+TransitionsSusRouterModal (InfoDialog, ConfirmDialog), Fade/Slide, NavigateWithTransition
5Nested+Namedchildren, PushNamed, :id, ?q=, alias, redirect, lazy
6RouteLinkSusRouteLink, Bind(router), router-link-active/exact-active
7FullDemoEVERYTHING: KeepAlive, guards, modals, transitions, nested, named, theming

Sample 1: BasicRouting ​

Script: BasicRoutingExample.cs

Demonstrates basic navigation: Push, Replace, Back, Home, CurrentRoute display.

4 tab Buttons: Home, About, Contact, Settings. Each tab Pushes the matching path.

Screens ​

  • HomeScreen — greeting + navigation buttons + route chip (Label)
  • AboutScreen — description + SusRouteLink to Home + image placeholder
  • ContactScreen — TextFields + submit Button
  • SettingsScreen — Toggles

Action buttons ​

  • Back — UITK Button, drives navigation
  • Current route shown as a chip Label

Key code ​

csharp
// tab bar navigation
void OnTabChanged(string path)
{
    Router.Push(path);
}

// SusRouteLink
var link = new SusRouteLink { To = "/about", Exact = true };
link.Bind(Router); // enables router-link-active

Sample 2: KeepAlive ​

Script: KeepAliveExample.cs

Shows the difference between KeepAlive=true (state preserved) and false (recreated).

3 tabs: Counter [K], Form [K], Settings. [K] = KeepAlive=true.

Screens ​

  • CounterScreen (KeepAlive) — counter + Button +/−. Count survives leave/return.
  • FormScreen (KeepAlive) — TextFields. Typed text survives tab switches.
  • SettingsScreen (NOT KeepAlive) — Toggles; recreated every time.

Key code ​

csharp
Router.Register("/counter", typeof(CounterScreen),
    new SusRouteConfig { KeepAlive = true });
Router.Register("/settings", typeof(SettingsScreen)); // KeepAlive=false

Sample 3: Guards ​

Script: GuardsExample.cs

Demonstrates the guard pipeline.

4 tabs: Home, Admin, Profile, OldAdmin. Auth via a Toggle "Logged in".

Guards ​

  • BeforeEach — blocks non-home routes when not logged in
  • AdminGuard — ISusRouteGuard.CanLeave when the admin form is dirty
  • Redirect — /old-admin → /admin via SusRouteConfig.Redirect

Screens ​

  • HomeScreen — instructions + try-it buttons
  • AdminScreen — TextField form + dirty CanLeave confirm overlay
  • ProfileScreen — available only when logged in

Key code ​

csharp
Router.BeforeEach((from, to) =>
{
    if (to.FullPath == "/home") return true;
    if (!_isLoggedIn) return false; // block
    return true;
});

Router.Register("/admin", typeof(AdminScreen), new SusRouteConfig
{
    Guard = new AdminGuard()
});

Sample 4: Modals & Transitions ​

Script: ModalExample.cs

Demonstrates SusRouterModal, modal service, and transition animations.

3 tabs: Page 1–3. Navigation with Fade via NavigateWithTransition (optional Toggle).

Modals ​

  • InfoDialog — info message + Close Button
  • ConfirmDialog — confirmation with OK/Cancel Buttons

Action buttons ​

  • Open Info / Open Confirm / Stack 3
  • Close Top — ModalService.Close()

Key code ​

csharp
// Show modal
Router.ModalService.Show(typeof(InfoDialog), new() {
    ["title"] = "Information",
    ["message"] = "Welcome!"
});

// Navigate with animation
Router.NavigateWithTransition("/page-2", 0.3f);

Sample 5: Nested & Named Routes ​

Script: AdvancedRoutingExample.cs

Demonstrates named routes, nested routes, alias, redirect, query params, lazy loading.

5 tabs: Main Menu (alias), Battle (:id), Settings (nested), Search (?q=), Lazy.

Capabilities ​

  • Named route — /battle/:id, PushNamed with pathParams
  • Alias — /menu → /main-menu
  • Redirect — /old-menu → /main-menu
  • Nested — /settings/profile, /settings/privacy (tab bar inside SettingsScreen)
  • Query — /search?q=hello&page=1
  • Lazy — /lazy, LazyFactory

Key code ​

csharp
Router.Register("/battle/:id", typeof(BattleScreen), new SusRouteConfig
{
    Name = "battle",
    Transition = SusRouteTransition.SlideLeft()
});

Router.PushNamed("battle", new() { ["id"] = "42" });

// Nested routes
Router.Register("/settings", typeof(SettingsScreen), new SusRouteConfig
{
    Children = new List<SusRouteRecord>
    {
        new SusRouteRecord("profile", typeof(LabelScreen)),
        new SusRouteRecord("privacy", typeof(LabelScreen)),
    }
});

Script: RouteLinkExample.cs

Demonstrates SusRouteLink with auto-highlighting.

Three SusRouteLink instances on screen: Home, Battle, Settings. Each automatically gets router-link-active / router-link-exact-active classes.

Key code ​

csharp
var homeLink = new SusRouteLink { To = "/home", Exact = true };
homeLink.Bind(Router); // auto-highlight

var battleLink = new SusRouteLink { To = "/battle/42" };
battleLink.Bind(Router);

Exact match is also available:

csharp
var exactLink = new SusRouteLink { To = "/home", Exact = true };
// router-link-exact-active only on exact /home

Sample 7: Full Demo ​

Script: FullDemoExample.cs

Comprehensive sample combining ALL router features + theming.

Layout — sidebar + content ​

  • Sidebar — vertical UITK tab Buttons: Dashboard, Users, Settings, About
  • Content — ScrollView + mounted route host

Features ​

  • KeepAlive: Dashboard / Users
  • Guards: UserDetailGuard on nested :id
  • Modals: "Open Modal" → AboutDialog (SusRouterModal)
  • Transitions: Fade between screens
  • Nested: /users/:id
  • Named: users / user-detail
  • Theming: Toggle Dark + SusThemeService

Key code ​

csharp
// Sidebar — vertical tab buttons push routes
navTabs.OnChanged += path => Router.Push(path);

// Modal
Router.ModalService.Show(typeof(AboutDialog), new() {
    ["title"] = "About",
    ["message"] = "SusRouter Full Demo"
});

Troubleshooting ​

SymptomCauseFix
Nothing showsUIDocument without PanelSettingsAssign PanelSettings / sample loads Resources
Tab buttons do nothingOnChanged not wiredBind Router.Push in the handler
Buttons ignore clicksNo EventSystemAdd Event System to the scene
PushNamed not foundNo Name in SusRouteConfigSet Name = "..."
KeepAlive does not cacheKeepAlive not trueSet KeepAlive = true
router-link-active missingSusRouteLink without BindCall link.Bind(router)
Modal does not showNo OverlayHostrouter.Init(overlayHost)
Redirect loopRedirect points at itselfCheck redirect chain