How agarciadom/pun2-tic-tac-toe works
Project Type and Purpose
• This is a Unity project with two versions:
– A “starting-version” implementing a local hotseat Tic Tac Toe game.
– A “final-version” that extends the local game using Photon Unity Networking (PUN2) to add online multiplayer capabilities.
• Its primary purpose is to serve as a starter guide for implementing online multiplayer in a turn-based game. The README explains both the basics of setting up the PUN and the added online events and synchronization required.
Main Architectural Components (as seen in the file tree and README)
• Scenes and UI:
– Lobby Scene: Includes UI elements such as a “Name Field” (with the NameInputField script), “Connect Button”, “Status Text”, and a “Connect Panel”. It is where players enter a name, connect to Photon's servers, and join (or create) a game room.
– Game Scene: Contains the “Game Manager”, Grid, Cells (prefabs such as “Cell.prefab” in Prefabs), and other game-specific assets (Sprites, TextMeshPro components).
• Core Game Logic and State Management:
– GameManager.cs: The central component that holds the game state (whose turn it is, current winner) and manages gameplay. It implements Photon’s IPunObservable for state sync and defines methods such as OnCellClicked, CellPlayed, etc.
– GridCell.cs: Defines individual cell behavior and handles user interactions (clicks), linked with Unity events.
– SquareGridPopulator.cs: Instantiates the board dynamically.
• Photon Integration / Network Management:
– ConnectManager.cs: Manages network connection, handling Photon events for connecting to the server, joining/creating rooms, and scene transitions.
– Use of PhotonView on key components (e.g. the Game Manager) and use of PhotonNetwork.RaiseEvent and Photon’s callback interface (like IOnEventCallback) for synchronizing moves.
– Utilization of PhotonCloud services (with external Photon Server/AppId data in PhotonServerSettings) to deliver realtime online play.
• Supporting Assets:
– Photon SDK source files (organized under Assets/Photon/PhotonRealtime, PhotonUnityNetworking, etc.) that comprise the networking libraries.
– Configuration files provided in ProjectSettings and Packages directories that set up the Unity and Photon environment.
Relationships and Data Flow Between Components
• In the Lobby:
– The UI “Name Field” (via NameInputField.cs) captures the player’s nickname and sets PhotonNetwork.NickName.
– The “Connect Button” invokes ConnectManager’s Connect() method. This method initiates connection to Photon using PhotonNetwork.ConnectUsingSettings and, depending on connection state, either attempts to join a random room or creates a new one.
• In the Game Scene:
– The GameManager (with the PhotonView component) is the authoritative master that synchronizes game state. It uses OnPhotonSerializeView to send/receive key game state data (Winner, Turn, and each GridCell’s Mark).
– When a player clicks on a grid cell (handled by GridCell.cs events), OnCellClicked in GameManager checks if it is the master client (via photonView.IsMine). If true, it directly processes the move by calling CellPlayed. If false, it raises a Photon event (using PhotonNetwork.RaiseEvent) so that the master client can process the move. This ensures only one source of truth for game state.
– Turn management is enforced; each client stores a “MyTurn” value to decide whether to accept input.
– When a game ends, only the master client can trigger a reset (by pressing space). Remote clients are informed via Photon state synchronization.
– Additionally, event callbacks (like OnPlayerLeftRoom) transition the game back to the Lobby if a player leaves.
• External Cloud / Photon Service:
Architectural Patterns and Design Principles
• Separation of Concerns: The design clearly splits UI (Lobby scene with Connect Panel and related UI), gameplay state management (GameManager, GridCell, and prefabs), and network communication (ConnectManager plus Photon event handling).
• Master-Client Authority Pattern: The game uses the master client as the source of truth, which is critical in multiplayer games, especially for turn-based gameplay.
• Event-Driven Communication: Player actions (such as a move on the grid) are either directly applied (if master) or relayed via Photon events to the master client. This keeps state changes synchronized.
• Prefab / Scene Organization: The use of prefabs for cells and assets (including standard Unity UI components) shows how Unity organizes projects following the Entity-Component-System (ECS) pattern.