Skip to content

10. Running the samples (Samples~)

The package ships 7 standalone samples under Samples~/. All samples use sus-kitcomponents (SusTabs, SusButton, SusChip, SusToggle, SusTextfield, SusModal, SusRouteLink).

Requirements

  • sus-router + sus-kit + sus-coreinstalled 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 featuressus-kit components
1BasicRoutingPush, Replace, Back, Home, CurrentRouteSusTabs, SusButton, SusChip, SusRouteLink, SusTextfield, SusToggle, SusImg
2KeepAliveKeepAlive=true/false, cachingSusTabs, SusButton, SusTextfield, SusToggle, SusChip
3GuardsBeforeEach, CanEnter, BeforeResolve, redirectSusTabs, SusButton, SusToggle, SusTextfield, SusChip
4Modals+TransitionsSusRouterModal (InfoDialog, ConfirmDialog), Fade/Slide, NavigateWithTransitionSusTabs, SusButton, SusModal
5Nested+Namedchildren, PushNamed, :id, ?q=, alias, redirect, lazySusTabs, SusChip, SusTextfield, SusToggle, SusButton
6RouteLinkSusRouteLink, Bind(router), router-link-active/exact-activeSusRouteLink, Label
7FullDemoEVERYTHING: KeepAlive, guards, modals, transitions, nested, named, themingSusTabs(vertical), SusButton, SusChip, SusToggle, SusTextfield, SusModal

Sample 1: BasicRouting

Script: BasicRoutingExample.cs

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

4 tabs: Home, About, Contact, Settings. Each tab Pushes the matching path via SusTabs.OnTabChanged.

Screens

  • HomeScreen — greeting + SusRouteLink to About, SusToggle, SusImg
  • AboutScreen — description + SusRouteLink to Contact
  • ContactScreen — SusTextfield with Prop, submit SusButton
  • SettingsScreen — SusChip with CurrentRoute

Action buttons

  • Back / Forward — SusButton, drives navigation
  • Log — logs CurrentRoute.FullPath

Key code

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

// SusRouteLink
var link = new SusRouteLink { To = "/about", Text = "About" };
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) — Prop<int> counter, SusButton +/-, multiplier SusToggle. Count survives leave/return.
  • FormScreen (KeepAlive) — SusTextfield with Prop<string>. Typed text survives tab switches.
  • SettingsScreen (NOT KeepAlive) — 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 (always), Dashboard (authenticated), Admin (admin only), About (always). Access to Admin/Dashboard via SusToggle "Login"/"Admin".

Guards

  • BeforeEach — checks auth for Meta["requiresAuth"]=true
  • AuthGuard — ISusRouteGuard.CanEnter checks "admin" role
  • BeforeResolve — redirects /old-admin/admin

Screens

  • HomeScreen — greeting + auth status (SusChip)
  • DashboardScreen — SusTextfield "Dashboard content"
  • AdminScreen — admin panel (admins only)
  • AboutScreen — guard info

Key code

csharp
Router.BeforeEach((from, to) =>
{
    if (to.Record?.Config?.Meta?.ContainsKey("requiresAuth") == true && !_isLoggedIn.Value)
        return false; // block
    return true;
});

Router.Register("/admin", typeof(AdminScreen), new SusRouteConfig
{
    Guard = new AuthGuard(),
    Meta = new() { ["requiresAuth"] = true, ["role"] = "admin" }
});

Sample 4: Modals & Transitions

Script: ModalExample.cs

Demonstrates SusRouterModal, SusModalService, and transition animations.

3 tabs: Home, Dashboard, About. Navigation with Fade/Slide via NavigateWithTransition.

Modals

  • InfoDialog — info message with SusIcon + OK SusButton
  • ConfirmDialog — confirmation with OK/Cancel SusButtons

Action buttons

  • Info — show InfoDialog
  • Confirm — show ConfirmDialog
  • Close — SusModalService.Close()
  • Toggle Dismiss — toggle dismissOnClickOutside

Key code

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

// Navigate with animation (slide)
Router.NavigateWithTransition("/dashboard",
    SusRouteTransitionType.SlideLeft, 0.4f);

Sample 5: Nested & Named Routes

Script: AdvancedRoutingExample.cs

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

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

Capabilities

  • Named route/battle/:id, PushNamed with pathParams
  • Alias/menu/main-menu
  • Redirect/old-menu/main-menu
  • Nested/settings/profile, /settings/privacy (SusTabs inside SettingsScreen)
  • Query/search?q=sus-router
  • 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(ProfileScreen)),
        new SusRouteRecord("privacy", typeof(PrivacyScreen)),
    }
});

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-activeclasses.

Key code

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

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

Exact match is also available:

csharp
var exactLink = new SusRouteLink { To = "/home", Text = "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 SusTabs: Dashboard, Users, Settings, About
  • Content — SusRouteView with screens

Features

  • KeepAlive: DashboardScreen keeps its counter
  • Guards: AboutScreen only for authenticated users (Login SusToggle)
  • Modals: "Logout" SusButton → ConfirmDialog
  • Transitions: Fade between screens
  • Nested: SettingsScreen with Profile/Privacy sub-tabs (SusTabs)
  • Named: /users/:id via PushNamed
  • Theming: SusChip with current theme

Key code

csharp
// Sidebar — vertical SusTabs
SusTabs sidebar = ...;
sidebar.Direction.Value = "vertical";
sidebar.Items.Value = new List<​TabItem>
{
    new() { Id = "dashboard", Label = "Dashboard" },
    new() { Id = "users", Label = "Users" },
    new() { Id = "settings", Label = "Settings" },
    new() { Id = "about", Label = "About" },
};
sidebar.OnTabChanged += (id) => Router.Push($"/{id}");

// Modal on Logout
Router.ModalService.Show(typeof(ConfirmDialog), new() {
    ["title"] = "Logout",
    ["message"] = "Are you sure?",
    ["onConfirm"] = (Action)(() => { _isLoggedIn.Value = false; })
});

Troubleshooting

SymptomCauseFix
Nothing showsUIDocument without PanelSettingsGetOrCreateUIDocument() sets it
SusTabs do nothingOnTabChanged 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

Open-core: Core & Router MIT · Kit & Game commercial