# playroomkit/unity architecture

> Architecture diagram of the GitHub repository playroomkit/unity, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-01). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/playroomkit/unity
- Repository: https://github.com/playroomkit/unity

## Overview

##### Project’s Main Purpose
- The SDK is a Unity package that wraps around the PlayroomKit JavaScript SDK.
- It is meant to facilitate multiplayer interactions for WebGL games built in Unity.
- The README emphasizes that the API mirrors the JavaScript version and that most examples are found in the “Examples” subdirectory.

##### Break Down the File Structure to Determine Key Components
- Assets/PlayroomKit: This is the central folder containing core SDK logic. Within it, you have its own subdirectories:
– Editor: Contains editor scripts (e.g., PlayroomDevEditor.cs) that enhance Unity’s editor experience.
– Examples: Contains sample projects (like “2d-platformer”, “discord-activity”, and “package-showcase”) which illustrate how the SDK is used in real scenarios.
– Modules: Hosts various functional modules such as Helpers, Interfaces, MockMode, Options, Player, RPC, and others—these are the building blocks of the SDK’s functionality.
– Dependencies: A large collection of external libraries (e.g., Castle.Core, NSubstitute, ParrelSync, UnityBrowserBridge, PowerConsole, SimpleJSON, etc.) that are either used at runtime or for development/testing.
– src folder and configuration files (e.g., vite.config.js): Likely handle the coupling with the underlying JavaScript code (the PlayroomKit JS) and browser-side interactions.
- Other top-level folders (e.g., Assets, ProjectSettings, Packages) follow standard Unity project structure.
- .github folder: Contains CI/CD workflows (for packaging, testing, releases, etc.), which are important for understanding the development process but less critical for the runtime architecture.

##### Identifying and Labeling Main Components
A. Core SDK Modules:
- Modules (Helpers, Interfaces, Options, Player, RPC etc.)
- The “PlayroomBuildService” and “PlayroomkitDevManager” that help configure and manage the SDK behavior.
- Communication interfaces (e.g., IRPC, IPlayerBase) that define how game objects interact with the multiplayer backend.
B. Editor Tools:
– Editor Scripts (in Assets/PlayroomKit/Editor) that provide in-editor support such as development consoles and settings panels.
C. External Dependencies:
– A “Dependency Layer” block that shows libraries like Castle.Core, ParrelSync, PowerConsole, SimpleJSON, and UnityBrowserBridge. Indicate that these are used to support features such as command handling, logging, cloning (for parallel testing), network/browser bridging, etc.
D. JavaScript/WebGL Bridge:
– A separate “PlayroomKit JS/WebGL” block, representing the external JavaScript SDK. This is what the Unity SDK wraps around.
– Indicate the role of Files found in Assets/PlayroomKit/src (frameworks.js, index.js) and how they enable communication between Unity and the PlayroomKit JavaScript layer when the game is exported as WebGL.
E. Examples and Testing:
– An “Examples” block (Assets/PlayroomKit/Examples) to demonstrate typical use cases.
– “Tests” folder contents also show that unit tests are available for development validation (Assets/PlayroomKit/Tests).

##### Relationships and Data Flow
- That the Core SDK communicates with the external PlayroomKit JS/WebGL component for actual multiplayer functionality.
- From the Editor Tools, show how they facilitate configuration and debugging in the Unity Editor environment.
- Dependencies layer with the core modules to highlight that external libraries provide additional functionality (e.g., for RPC calls or command parsing).

##### Architectural Patterns and Design Principles
- Modularization: The clear separation into Modules (Helpers, Interfaces, Options, etc.) points to a design that favors separation of concerns and modularity.
- Wrapper/Adapter Pattern: The SDK serves as a wrapper for the JavaScript SDK, so it acts as an adapter between Unity-specific code (C#) and web-based technology.
- MVC or similar separation: While not strictly MVC, there’s a separation between frontend (game assets prefabs, scenes), business logic (SDK modules/Services), and infrastructure (external JS/WebGL and Dependencies).

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Unity Game Environment
    subgraph "Unity Game Environment"
        GAME["Game Scripts & Logic"]:::game
    end

    %% PlayroomKit SDK Core
    subgraph "PlayroomKit SDK"
        CR["PlayroomKit Core SDK"]:::runtime
        CM["Core Modules"]:::runtime
        BD["PlayroomBuildService & PlayroomkitDevManager"]:::runtime
    end

    %% JavaScript/WebGL Bridge Layer
    subgraph "JS/WebGL Bridge"
        JS["JavaScript/WebGL Bridge"]:::runtime
    end

    %% Editor Tools Layer
    subgraph "Editor Tools"
        ED["Editor Tools"]:::development
    end

    %% External Dependencies Layer
    subgraph "External Dependencies"
        DEP["External Dependencies"]:::external
    end

    %% Examples and Tests Layer
    subgraph "Examples & Tests"
        EXA["Examples & Use Cases"]:::examples
        UT["Unit Tests"]:::development
    end

    %% Data Flow Connections
    GAME -->|"calls"| CR
    CR -->|"contains"| CM
    CM -->|"includes"| BD
    CR -->|"communicatesWith"| JS
    ED -->|"configures"| CR
    DEP -->|"supports"| CM
    CR -->|"demonstratedBy"| EXA
    CR -->|"verifiedBy"| UT

    %% Click Events
    click CR "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit"
    click CM "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/modules"
    click BD "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/modules"
    click ED "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/Editor"
    click DEP "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/dependencies"
    click JS "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/src"
    click EXA "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/Examples"
    click UT "https://github.com/playroomkit/unity/tree/main/Assets/PlayroomKit/Tests"

    %% Styles
    classDef game fill:#cccccc,stroke:#333,stroke-width:2px;
    classDef runtime fill:#a3e4d7,stroke:#1abc9c,stroke-width:2px;
    classDef development fill:#aed6f1,stroke:#3498db,stroke-width:2px;
    classDef external fill:#f9e79f,stroke:#f1c40f,stroke-width:2px;
    classDef examples fill:#d2b4de,stroke:#8e44ad,stroke-width:2px;
```
