Start from Scratch

Throughout the following steps we will create a very simple oldschool city builder from scratch using CCBK. Please forgive the horrible programmer art.

This page covers only the first two evolutions, water and food. A completed version with all stages can be found in the CityBuilderManual project. I believe you will be able to figure the rest out after following the steps laid out here. Religion is very similar to water and pottery has just a slightly longer production chain than food.

Setup

  • Create a new unity project using the 2D template
  • Import the CCBK Core Framework
    Import
  • Place the Manual Assets inside your Assets folder

Map

  • Create a Gameobject at 0,0,0 and call it ‘Map’
  • Add a Grid Component
  • Add a DefaultMap Component 1
    • Set its Size to 32x32
    • In Walking- and BuildingBlockingTiles add the water tile 2
    • Create a child Gameobject at 0,0,0 and call it ‘Ground’
      • Add Tilemap and TilemapRenderer to it
    • Assign it to the Ground field on your map
  • Move the Main Camera Object to the center of the map (16,16,-10)
    • Add a Camera Controller Component to it
  • Open the Tile Palette Tab found in Windows/2D
    • Make sure your active Tilemap is ‘Ground’ and your Palette is ‘historic’
    • Draw your map inside the guide lines of the map(turn on gizmos if you cant see it)

Ground
We’ll later add a water layer that spreads out two tiles from water. I’ ve drawn grass tiles there to communicate that to the player.

With all that done you can start the game for the first time. You should be able to move the Camera around the map while being confined to its bounds.

Roads

Roads are generally pretty simple but we’ll have to do some setup on the Tools and Logic side to be able draw them so please bear with me.

Tilemap

  • Create a child Gameobject in Map at 0,0,0 and call it ‘Roads’
    • Add Tilemap and TilemapRenderer to it
      • Set OrderInLayer to 1
    • Add a DefaultRoadManager Component
  • Create a child Gameobject in Map at 0,0,0 and call it ‘Highlights’
    • Add Tilemap and TilemapRenderer to it
      • Set OrderInLayer to 2
    • Add a DefaultHighlightManager to it
      • Assign the Highlight Tiles of the same name

Logic

  • Create a Gameobject at 0,0,0 and call it ‘Logic’ and add the following Components
    • DefaultGameManager
    • ObjectRepository
    • DefaultStructureManager

Road Object

  • Create a Folder in your Assets called City
    • Create a Road Object
      Found in the ContextMenu under Create/CityBuilder/Road
    • Add a single stage to it by assigning 1 to Stages-Size
      • Assign ‘ROD’ to the Key 3
      • Assign the ‘road’ tile to Tile
  • On ObjectRepository assign Road

UI Tools

  • Create a UI/Panel and call it ‘Tools’
    • Move it to middle-right with a width of 50
    • Add a VerticalLayoutGroup and a ContentSizeFitter with verticalFit=Preferred
    • Add a ToggleGroup with AllowSwitchOff=1 and a ToolsManager Component
      • Assign the ToggleGroup in the ToolsManager
    • Create a UI/Toggle in Tools and call it ‘RoadTool’
      • Delete the Text
      • Resize the Toggle to 50,50
      • Assign the ToggleGroup
      • Set the Toggles IsOn to false
      • Move Background and Checkmark to middle-center with size 32,32
      • Assign toolsBorder as the Checkmark image and blank as the Background
      • Add a RoadBuilder and ToolsActivator Component to RoadTool
        • Assign the Road Object you just created in the Road field
      • Add ToolsActivator.SetToolActive to the Toggles OnValueChanged event

After selecting the tool and starting up the game you should now be able to draw roads on the map.
Roads

Buildings

Prefab

  • Create a Gameobject at 0,0,0 and call it ‘WaterSupply’ 4
    • Create a 2D Object/Sprite at 1,1,0 and call it ‘sprite’
      • Assign the waterSupply sprite and OrderInLayer 10
    • Add a Building Component to WaterSupply and assign sprite as its Pivot
  • Create a Folder called ‘Buildings’ inside the City Folder
    • Drag the WaterSupply to the Folder to create a Prefab
    • Remove the WaterSupply from the Scene

Buildinginfo

  • Create a BuildingInfo Object called ‘WaterSupply’ in Buildings
    Found in the ContextMenu under Create/CityBuilder/BuildingInfo)
    • Assign Key=’WAT’ Name=’water supply’ and Size=2,2
    • Assign the Prefab we just created
  • On the WaterSupply Prefab assign the BuildingInfo

Tool

  • Duplicate the RoadTool we’ve created in the previous chapter and call it ‘WaterTool’
    • Replace the RoadBuilder with a BuildingBuilder 5
      • Assign the WaterSupply BuildingInfo to the BuildingBuilder
    • Set the Background image to waterSupply and reset the color to white

Logic

  • Add a DefaultBuildingManager to Logic

Start up the game again and build some water supplies! Buildings

Walkers

Prefab

  • Create a Gameobject at 0,0,0 and call it ‘WaterCarrier’
    • Create a child Gameobject at 0.5,0.5,0 and call it ‘pivot’6
      • Create a 2D Object/Sprite at 0,0,0 and call it ‘sprite’
        • Assign the waterCarrier sprite and OrderInLayer 20
        • Set Z Rotation to 270
    • Add a ServiceWalker Component to WaterCarrier and assign pivot as its Pivot
      • Set PathType to Road
  • Create a Folder called ‘Walkers’ inside the City Folder
    • Drag the WaterCarrier to the Folder to create a Prefab
    • Remove the WaterCarrier from the Scene

Walkerinfo

  • Create a WalkerInfo Object called ‘WaterCarrier’ in Walkers (found in the ContextMenu under Create/CityBuilder/WalkerInfo)
    • Assign Name=’water carrier’
  • On the WaterCarrier Prefab assign the WalerInfo

Spawning

  • On the WaterSupply Prefab add a ServiceWalkerComponent Component7
    • Assign Count=1 Reuse=1 Downtime=5

As soon as a WaterSupply is connected to a road it should now spawn a WaterCarrier that roams around for a bit and then returns. Walkers

  • Change Variance on the Map to 0.2 and start the game again, walkers should now be offset a little bit to prevent overlapping

Templates

Since we will be creating a number of Buildings and Walkers in the following Chapters let’s create blank templates for them that we can easily duplicate.4

  • Duplicate WaterSupply Prefab and Info
    • rename them both to ‘Building’
    • clear out key, name and prefab in info
    • clear out info and sprite in prefab
    • remove the ServiceWalkerComponent
  • Duplicate WaterCarrier Prefab and Info
    • rename them both to ‘Walker’
    • clear out name in info
    • clear out info and sprite in prefab
    • remove the ServiceWalker

From now on whenever i mention creating a new building do the following.

  • Duplicate Building Prefab and Info
  • Set prefab, key and name in info
  • Set info and sprite in prefab

Whenever i mention creating a new walker do the following.

  • Duplicate Walker Prefab and Info
  • Add whatever Walker Component is needed
  • Set name in info
  • Set info and sprite in prefab

Housing

Population Object

  • Create a Population Object in your City Folder
    Found in ContextMenu under Create/CityBuilder/Population
    • Set Key=’POP’ Name=’Population’
  • On ObjectRepository assign Population

Buildings

  • Create Buildings
name info-key info-name sprite
H0 HNO housing housing
H1Hut HUT hut hut
H2Shanty SHT shanty shanty
H3Cottage CTG cottage cottage
  • Add a HousingPlaceholderComponent to H0
    • Assign H1 as the Prefab
  • Add a HousingComponent to H1,H2,H3
    • In PopulationHousings add a single entry that uses the Population Object and the counts 20, 35, 50

Walkers

  • Create Immigrant Walker
    • Add ImmigrationWalker Component
      • Name: immigrant Sprite: migrant
      • Set PathType to MapGrid
      • Set Capacity to 10

Logic

  • Add DefaultPopulationManager to Logic
  • Add a Migration to Logic
    • Assign the ImmigrationWalker and set the count to 100
    • Set Logic as the Entry
    • Assign the Population Object

Tool

  • Duplicate the WaterTool and name it HousingTool
  • Assign H0 as the Building
  • Use the hut sprite as the Background
  • Move it between RoadTool and WaterTool

Start the game, the HousingTool now builds placeholders that are turned into huts as soon as immigrants arrive.
Housing

Food

Items

  • Create a Folder called ‘Items’ inside the City Folder
  • Create an Item Object in the Items Folder
    Found in ContextMenu under Create/CityBuilder/Item
    • Set Key=’CHK’ Name=’chickpeas’ UnitSize=100 Icon=chickpeas
  • On ObjectRepository assign Item

  • Create a Gameobject at 0,0,0 and call it ‘Chickpeas’
    • Set scale to 0.65,0.65,1
    • Create 4 Sprites with OrderInLayer 30 and assign the chickpeas1-4 sprites
    • Add a StorageQuantityVisual
      • Set Swap to true
      • Add the sprites to the Objects array
  • Drag Chickpeas to the Items Folder and remove it from the scene
  • Add it to the Chickpeas-Item Visuals

Walkers

name info-name sprite component settings
CartPusher cart pusher cartPusher DeliveryWalker PathType:Road
BazaarBuyer bazaar buyer bazaarBuyer PurchaseWalker PathType:Road Capacity:3
BazaarSeller bazaar seller bazaarSeller SaleWalker PathType:RoadBlocked

Buildings

name info-key info-name sprite
Farm FAM farm farm
Granary GRN granary granary
Bazaar BAZ bazaar bazaar
  • Granary and Farm are larger buildings so we’ll have to adjust size and pivot position
    • Farm size:3x3 pivot:1.5,1.5,0
    • Granary size:4x4 pivot:2,2,0
  • Farm
    • Add a ProductionComponent
      • Set Interval to 20
      • Assign the CartPusher as Prefab in DeliveryWalkers
      • Create an entry in ItemsProducers
        • Set Item to Chickpeas and Quantity to 100
        • Set Storage to FreeUnityCapped with Capacity:1
    • Add a ProgressThresholdVisualizer
      • Create 4 Sprites under sprite with OrderInLayer:100 and assign farmProgress1-4 sprites
      • Create 4 entries in ProgressThresholds with the above sprites and the thresholds: 0.2, 0.4, 0.6, 0.8
  • Granary
    • Add a StorageComponent
      • Set Storage to Stacked Mode with 8 Stacks and 4 Capacity
      • Create an entry in Orders for Chickpeas with Ratio:1
    • Add a StorageQuantityVisualizer to Granary
      • Create 8 Transforms and move them over the centers of the holes in the sprite
      • Add them to the Origins array
  • Bazaar
    • Add a DistributionComponent
      • Set Storage to FreeUnitCapped with Capacity 5
      • Create an entry in Orders for Chickpeas with Ratio:1
      • Assign the BazaarBuyer as Prefab in PurchaseWalkers
      • Assign the BazaarSeller as Prefab in SaleWalkers with Reuse:1 and Downtime:5


  • Add DefaultItemManager to Logic and set Storage to Free Mode
  • Duplicate the WaterTool 3 times and assign the sprites and BuildingInfos of Farm, Granary and Bazaar

Build some farms and watch them grow. Connect them to a granary for the chickpeas to be stored there. The Bazaar wont do anything because the seller wont find anyone in need of chickpeas yet.
Food

Layer

  • Create a Layer Object in the City Folder and name it Water
    Found in ContextMenu under Create/CityBuilder/Layer
  • On Logic Add DefaultLayerManager
    • Create an Entry in AffectingTiles
      assign the water tile, water layer and set value:10 Range:2 Falloff:0
  • In the BuildingInfos of Farm and WaterSupply add a LayerRequirement entry
    • Set Layer:Water Min:5 Max:100

Farms and WaterSupplies can now only be built close to water.

Evolution

Water Service

  • Create a Folder called ‘Services’ inside the City Folder
  • Create a Service Object in the Services Folder and name it Water
    Found in ContextMenu under Create/CityBuilder/Service
  • In the WaterCarrier Walker Prefab assign the Water Service

Evolution Sequence

  • Create an EvolutionSequence Object in the City Folder
    Found in ContextMenu under Create/CityBuilder/Service
  • Add the following Stages
    • BuildingInfo: H1Hut
    • BuildingInfo: H2Shanty Services: Water
    • BuildingInfo: H3Cottage Items: Chickpeas

Evolution Component

  • Add an EvolutionComponent to all 3 Housing Buildings(H1, H2, H3)
  • Assign the EvolutionSequence
  • Add a ServiceRecipient for Water to all 3 with a Loss of 5
  • Add an ItemsRecipient for Chickpeas to H2 and H3
    Set Storage to FreeItemCapped Mode with Capacity 10 and ConsumptionInterval to 2

Start up the game and combine everything to evolve the housing to Cottages Evolution

Saving

  • Create a BuildingInfoSet Object in the Buildings Folder
    Found in ContextMenu under Create/CityBuilder/Sets/BuildingInfoSet
  • On ObjectRepository assign Buildings with it.

Thats all we were missing for saving and loading. In the absence of any UI you can use the In-Editor Buttons on Logic/DefaultGameManager to save and load.



  1. CCBK supports XYZ and XZY swizzles, the default XYZ is good for 2D 

  2. blocking and ground are needed later to prevent walking and building on water 

  3. keys have to be unique within their category, they are mostly used for saving 

  4. the completed project uses a tree of prefab variants for buildings(skipped here for brevity)  2

  5. switch the inspector to debug and select the other script, switch inspector back to normal 

  6. sprite is a separate object in this one because is drew the walkers in the wrong direction and they have to be rotated 

  7. i know