# fchb1239/photonvr architecture

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

- Interactive diagram: https://gitdiagram.com/fchb1239/photonvr
- Repository: https://github.com/fchb1239/photonvr

## Overview

##### Project Type and Purpose
- Its goal is to simplify VR networking: connection, room management, player representation, voice chat, cosmetic attachments, and optional authenticated connections (e.g., via PlayFab).

##### File Structure
- Top-level scripts:
- PhotonVRManager.cs & PhotonVRManagerGUI.cs – the central singleton Manager (connection, room, server switching, user settings) plus its editor GUI.
- “Player” folder:
- PhotonVRPlayer.cs – runtime behaviour of the networked player.
- PhotonVRPlayerBody.cs / Name.cs – subcomponents for player avatar body and nameplate.
- PlayerSpawner.cs – spawns/initializes players in scene.
- “Saving” folder:
- “Testing” folder:
- ColourChanger(.cs/.GUI), CosmeticsChanger(.cs/.GUI) – example/test scripts and inspectors to verify change APIs.
- Configuration assumed via Unity Resources/PhotonVR/Prefabs (documented in README).

##### Key Insights from README
- Dependencies: Unity, PUN 2, Photon Voice 2, TextMeshPro
- Setup flow: drop prefabs → configure AppId/VoiceAppId → assign VR rig (Head/Hands) → set queue/room limits → enable auto-connect/auto-join → (optionally) implement authenticated flows.
- Public APIs: Connect, ChangeServers, JoinRandomRoom, JoinPrivateRoom, SwitchScenes, SetUsername, SetColour, SetCosmetics.

##### Main Components
a. Unity Game Client
- Sub-components:
- PhotonVRManager (Controller/Singleton)
- Player System (PhotonVRPlayer + subcomponents + PlayerSpawner)
- Saving Subsystem (PhotonVRValueSaver)
- Scene & Prefabs (Resources/PhotonVR/Prefabs)
b. External Services
- Photon Cloud (PUN 2 Master/Region servers + Voice servers)
- Optional Auth Service (e.g., PlayFab for authenticated tokens)
c. Data Stores (local)
- PlayerPrefs or file storage via PhotonVRValueSaver

##### Relationships & Data Flow
- () → handshake
- JoinRandomRoom() / JoinPrivateRoom() → room join requests
- Voice channel connections
- Photon Cloud propagates events → triggers Manager callbacks → Manager orchestrates PlayerSpawner to instantiate PhotonVRPlayer prefab.
- Player components send/receive RPCs / networked property updates (position, avatar state, cosmetics).
- If using authenticated flows: PhotonVRManager ←→ PlayFab for token exchange, then to Photon Cloud with authentication header.

##### Architectural Patterns & Principles
- Singleton/Manager pattern (PhotonVRManager drives all network logic).
- Prefab-based composition: Player is built from modular subcomponents (body, name, cosmetics).
- Separation of concerns: Networking (Manager), Representation (Player), Persistence (ValueSaver), Testing decoupled in separate namespace.
- Event-driven callbacks (OnConnected, OnJoinedRoom, OnPlayerEntered) within Manager and Player scripts.

1. Stage 1: Connection Setup
- PhotonVRManager initiates Connect() → Photon Cloud
- (Optional) Manager calls PlayFab → retrieves token → manager.ConnectAuthenticated()
2. Stage 2: Room Management
- Manager.JoinRandomRoom(queue, maxPlayers) → Photon Cloud → OnJoinedRoom callback
3. Stage 3: Player Spawning
- Upon OnJoinedRoom, Manager triggers PlayerSpawner → instantiates PhotonVRPlayer prefab (with body, name, cosmetics containers)
4. Stage 4: Voice & State Sync
- PhotonVRPlayer components open Photon Voice channels → voice server
- /rotation & cosmetic state synchronized via PUN RPCs
5. Stage 5: Persistence & Testing
- User changes (SetUsername, SetColour, SetCosmetics) → persisted via PhotonVRValueSaver
- Testing scripts operate against Manager API to validate functionality

##### Final Checklist
- Components: list all key scripts and subsystems.
- External services: Photon Cloud (PUN, Voice), PlayFab (optional).
- Data stores: local Saver.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Unity Game Client"
        subgraph "Core Components"
            Manager["PhotonVRManager"]:::core
            ManagerGUI["PhotonVRManagerGUI"]:::editor
            spawner["PlayerSpawner"]:::core
            player["PhotonVRPlayer"]:::core
            body["PhotonVRPlayerBody"]:::core
            name["PhotonVRPlayerName"]:::core
        end
        subgraph "Persistence"
            saver["PhotonVRValueSaver"]:::persistence
        end
        subgraph "Configuration"
            prefs["Resources/PhotonVR/Prefabs"]:::config
        end
        subgraph "Testing Utilities"
            cc["PhotonVRColourChanger"]:::test
            ccgui["PhotonVRColourChangerGUI"]:::test
            ccos["PhotonVRCosmeticsChanger"]:::test
            ccosgui["PhotonVRCosmeticsChangerGUI"]:::test
        end
        subgraph "Visual Assets"
            visuals["Visuals"]:::assets
        end
        readme["README.md"]:::doc
    end

    PhotonCloud["Photon Cloud (PUN & Voice)"]:::external
    PlayFab["PlayFab Auth Service"]:::external

    Manager -->|"Connect()"| PhotonCloud
    Manager -->|"JoinRandomRoom() / JoinPrivateRoom()"| PhotonCloud
    PhotonCloud -->|"OnConnected() / OnJoinedRoom()"| Manager
    Manager -->|"Trigger OnJoinedRoom"| spawner
    spawner -->|"Instantiate PhotonVRPlayer Prefab"| player
    player -->|"Uses Body & Name Components"| body
    player -->|"Uses Body & Name Components"| name
    player -->|"Voice Channel RPCs"| PhotonCloud
    player -->|"State Sync RPCs"| PhotonCloud
    Manager -->|"Save Settings"| saver
    saver -->|"Read/Write Local"| prefs
    cc -->|"Test API Calls"| Manager
    ccgui -->|"Test API Calls"| Manager
    ccos -->|"Test API Calls"| Manager
    ccosgui -->|"Test API Calls"| Manager
    Manager -->|"Optional Auth Token"| PlayFab
    PlayFab -->|"Token Response"| Manager
    Manager -->|"AuthConnect(token)"| PhotonCloud

    click Manager "https://github.com/fchb1239/photonvr/blob/main/PhotonVRManager.cs"
    click ManagerGUI "https://github.com/fchb1239/photonvr/blob/main/PhotonVRManagerGUI.cs"
    click player "https://github.com/fchb1239/photonvr/blob/main/Player/PhotonVRPlayer.cs"
    click body "https://github.com/fchb1239/photonvr/blob/main/Player/PhotonVRPlayerBody.cs"
    click name "https://github.com/fchb1239/photonvr/blob/main/Player/PhotonVRPlayerName.cs"
    click spawner "https://github.com/fchb1239/photonvr/blob/main/Player/PlayerSpawner.cs"
    click saver "https://github.com/fchb1239/photonvr/blob/main/Saving/PhotonVRValueSaver.cs"
    click cc "https://github.com/fchb1239/photonvr/blob/main/Testing/PhotonVRColourChanger.cs"
    click ccgui "https://github.com/fchb1239/photonvr/blob/main/Testing/PhotonVRColourChangerGUI.cs"
    click ccos "https://github.com/fchb1239/photonvr/blob/main/Testing/PhotonVRCosmeticsChanger.cs"
    click ccosgui "https://github.com/fchb1239/photonvr/blob/main/Testing/PhotonVRCosmeticsChangerGUI.cs"
    click visuals "https://github.com/fchb1239/photonvr/tree/main/Visuals/"
    click readme "https://github.com/fchb1239/photonvr/blob/main/README.md"

    classDef core fill:#CEF1FF,stroke:#0066CC,color:#003366
    classDef editor fill:#E8F4FF,stroke:#3399FF,color:#0055AA
    classDef persistence fill:#E6FFEA,stroke:#00AA00,color:#006600
    classDef config fill:#F0E6FF,stroke:#9933FF,color:#550088
    classDef test fill:#F2F2F2,stroke:#888888,color:#555555
    classDef assets fill:#F9F9F9,stroke:#BBBBBB,color:#666666
    classDef doc fill:#FFF5CC,stroke:#FFCC00,color:#996600
    classDef external fill:#FFE6CC,stroke:#FF6600,color:#994400
```
