Game Development Live

Unity Room Generator

Procedural Room Generation

A Unity system for procedural generation of room prefabs with seed-based random generation, intelligent connector systems and comprehensive editor tools.

UnityC#Procedural GenerationEditor ScriptsPrefab System

Unity Room Generator

Overview

A Unity system for procedural generation of room prefabs with seed-based random generation. The system allows dynamic chaining of room prefabs while ensuring intelligent connections between inputs and outputs.

Technical Architecture

Core System

  • Room: Room prefabs with automatic BoundingBox generation and RoomType classification
  • Connector: Connection points with Entrance/Exit logic and automatic transform calculation
  • RoomSequencer: Orchestration with timeout protection and regeneration logic
  • Area: Collision detection with SAT algorithm for precise placement

Editor Tools

  • RoomEditor: Custom Inspector with automatic connector management
  • GenerateBoundsWindow: Visual tool for BoundingBox creation
  • ConnectorEditor: Scene View visualization with direction indicators
  • ConditionalDisableDrawer: Intelligent UI components

Generation Logic

  • Seed-based random generation with automatic seed return
  • RoomType compatibility for specific room connections
  • Collision detection with configurable overlap threshold
  • Timeout protection with regeneration logic for failed attempts

Technical Details

Room Component

public class Room : MonoBehaviour
{
    public RoomType roomType = RoomType.MainRoom;
    public Connector[] connections;
    public Bounds[] BoundingBounds;
    
    public enum RoomType { MainRoom, SideRoom, EndRoom, Corridor }
    
    // Automatic bounding area calculation
    public Area BoundingArea { get; }
    
    // Collision detection with other rooms
    public bool CanConnectTo(Connector other, out int[] possibleConnectionsIndexes, float collisionThreshhold);
}

Connector System

[ExecuteInEditMode]
public class Connector : MonoBehaviour
{
    public Room Owner;
    public ConnectionType Type = ConnectionType.Entrance | ConnectionType.Exit;
    
    public bool IsConnected => ConnectedTo != null;
    public Connector ConnectedTo { get; private set; }
    
    // Automatic transform calculation
    public void SetRoomTransformTo(Connector guideConnector);
    
    [System.Flags]
    public enum ConnectionType
    {
        Entrance = 1 << 0,
        Exit = 1 << 1
    }
}

RoomSequencer

public class RoomSequencer : MonoBehaviour
{
    public bool UseSeed = false;
    public int Seed;
    public float OverlappingThreshhold = 0.1f;
    public int TimeToGenerate = 30;
    
    public RoomGenerationTracker[] RoomSetups;
    
    // Generation logic with timeout protection
    public bool Generate();
    
    [System.Serializable]
    public class RoomGenerationTracker
    {
        public Room RoomPrefab;
        public int MinRoomsToGenerate = 1;
        public int MaxRoomsToGenerate = 100;
    }
}

Conclusion

The Unity Room Generator System demonstrates modern procedural generation with focus on flexibility and user-friendliness. The intelligent connector system and seed-based generation make it an effective solution for dynamic level creation.


Developed with Unity, C# and Custom Editor Scripts