Skip to content

Codewithnavy/Unity-Slot-Machine

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

5 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Unity Slot Machine β€” Extended Version

A feature-rich slot machine built in Unity to demonstrate clean structure, RNG fairness, animation, and bonus mechanics.


Play (WebGL)

  • Open Build/WebGL/index.html locally via a static server.
  • Or host the /Build/WebGL folder on GitHub Pages (see below).

Project Structure

Assets/
  Art/
  Audio/
  Prefabs/
    Reels/
    UI/
  Scenes/
    Main.unity
  Scripts/
    Core/
    Gameplay/
      Reels/
      Symbols/
      Features/
  UI/
  Build/
    WebGL/

Features

Core Gameplay

  • 3Γ—3 reel layout with smooth spin animations
  • Win condition: match symbols on paylines
  • Paylines: 5 standard lines (3 rows + 2 diagonals)
  • Clean RNG (crypto-seeded for fairness)
  • Configurable payouts via ScriptableObjects
  • Balance system with spin cost and payouts

Bonus Features

  • Wild symbols β†’ substitute any match
  • Scatter symbols β†’ trigger free spins (3+ scatters)
  • Weighted rarity β†’ control symbol frequency
  • Animator option β†’ blur reel spin animations

Extensibility

  • Paylines easily expanded
  • Free spins stackable
  • Bonus features isolated in separate scripts
  • Fully data-driven using ScriptableObjects

Scene & Prefab Setup (Step-by-step)

Scene Hierarchy (Main.unity)

Canvas
  β”œβ”€β”€ UIController (scripts for spin button, balance text)
  β”œβ”€β”€ SpinButton
  └── BalanceText
SlotMachineController
  └── ReelContainer (empty parent)
       β”œβ”€β”€ ReelView_0
       β”œβ”€β”€ ReelView_1
       └── ReelView_2
AudioManager

Prefabs

  • Symbol prefab β†’ Image + Symbol script
  • Reel prefab β†’ Empty with VerticalLayoutGroup, holds symbols
  • UI prefabs β†’ Button, Text

Step-by-step Wiring

  1. Create Canvas (UI Scale Mode: Scale With Screen Size).
  2. Add Spin Button and Balance Text.
  3. Create empty SlotMachineController GameObject β†’ attach SlotMachineController.cs.
  4. Inside it, create ReelContainer β†’ add 3 child objects: ReelView_0, ReelView_1, ReelView_2.
  5. Each ReelView prefab: attach ReelView.cs.
  6. Create Symbol prefab: attach SymbolView.cs, link sprite in inspector.
  7. Drag reels and symbols into controller references.
  8. Link Spin Button β†’ UIController.OnSpinClicked().

Unity .gitignore

Add a .gitignore in repo root:

[Ll]ibrary/
[Tt]emp/
[Bb]uild/
[Bb]uilds/
obj/
[Mm]emoryCaptures/
[Bb]in/
/Logs/
*.csproj
*.unityproj
*.sln
*.userprefs
.DS_Store

πŸ›  Editor Script (Auto-wire Reels)

Place in Assets/Editor/AutoWireReels.cs:

using UnityEngine;
using UnityEditor;

[CustomEditor(typeof(SlotMachineController))]
public class AutoWireReels : Editor
{
    public override void OnInspectorGUI()
    {
        DrawDefaultInspector();

        SlotMachineController controller = (SlotMachineController)target;
        if (GUILayout.Button("Auto-Wire Reels"))
        {
            var reels = controller.GetComponentsInChildren<ReelView>();
            controller.Reels = reels;
            EditorUtility.SetDirty(controller);
            Debug.Log($"Wired {reels.Length} reels.");
        }
    }
}

UML-style Class Diagram

classDiagram
    class SlotMachineController {
        - ReelView[] Reels
        - RNGService rng
        - Wallet wallet
        + Spin()
        + EvaluateWin()
    }

    class ReelView {
        - SymbolView[] symbols
        + SpinAnimation()
        + StopAt(SymbolDefinition)
    }

    class SymbolView {
        - SymbolDefinition definition
        + Display()
    }

    class SymbolDefinition {
        <<ScriptableObject>>
        - string name
        - Sprite icon
        - int payout
        - SymbolType type (Normal/Wild/Scatter)
        - int weight
    }

    class RNGService {
        + GetRandomSymbol()
        + Seed()
    }

    class Wallet {
        - int balance
        + Add()
        + Deduct()
    }

    class UIController {
        + OnSpinClicked()
        + UpdateBalanceText()
    }

    SlotMachineController --> ReelView
    ReelView --> SymbolView
    SymbolView --> SymbolDefinition
    SlotMachineController --> RNGService
    SlotMachineController --> Wallet
    SlotMachineController --> UIController
Loading

Thought Process / Approach

  • Started with modular structure (scripts split by domain: core, reels, symbols, features)

  • Used ScriptableObjects for symbol definitions & payouts

  • Centralized RNGService with cryptographic seeding

  • Procedural spin animation with AnimationCurve

  • Extended to 3Γ—3 layout with paylines, adding:

    • Wilds (flexible symbol match)
    • Scatters (bonus trigger)
    • Weighted rarity for symbol balancing
  • Provided Animator-driven option for motion blur

  • Ensured WebGL compatibility


πŸ“š References


About

🎰 A simple slot machine game built with Unity. Features 3x3 reels, paylines, RNG fairness, payouts, wilds, and bonus spins.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages