O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Mastering Unity 2D Game Development - Second Edition

Book Description

Master everything you need to build a 2D game using Unity 5 by developing a complete RPG game framework!

About This Book

  • Explore the new features of Unity 5 and recognize obsolete code and elements.
  • Develop and build a complete 2D retro RPG with a conversation system, inventory, random map battles, full game menus, and sound.
  • This book demonstrates how to use the new Unity UI system effectively through detailed C# scripts with full explanations.

Who This Book Is For

This book is for anyone looking to get started developing 2D games with Unity 5. If you're already accomplished in Unity 2D and wish to expand or supplement your current Unity knowledge, or are working in 2D in Unity 4 and looking to upgrade Unity 5, this book is for you. A basic understanding of programming logic is needed to begin learning with this book, but intermediate and advanced programming topic are explained thoroughly so that coders of any level can follow along. Previous programming experience in C# is not required.

What You Will Learn

  • Create a 2D game in Unity 5 by developing a complete retro 2D RPG framework.
  • Effectively manipulate and utilize 2D sprites.
  • Create 2D sprite animations and trigger them effectively with code.
  • Write beginning to advanced-level C# code using MonoDevelop.
  • Implement the new UI system effectively and beautifully.
  • Use state machines to trigger events within your game.

In Detail

The Unity engine has revolutionized the gaming industry, by making it easier than ever for indie game developers to create quality games on a budget. Hobbyists and students can use this powerful engine to build 2D and 3D games, to play, distribute, and even sell for free!

This book will help you master the 2D features available in Unity 5, by walking you through the development of a 2D RPG framework. With fully explained and detailed C# scripts, this book will show you how to create and program animations, a NPC conversation system, an inventory system, random RPG map battles, and full game menus. After your core game is complete, you'll learn how to add finishing touches like sound and music, monetization strategies, and splash screens. You’ll then be guided through the process of publishing and sharing your game on multiple platforms. After completing this book, you will have the necessary knowledge to develop, build, and deploy 2D games of any genre!

Style and approach

This book takes a step-by-step practical tutorial style approach. The steps are accompanied by examples, and all the intermediate steps will be clearly explained. The focus of this book will obviously be on the advanced topics so that the game looks and performs efficiently.

Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at http://www.PacktPub.com. If you purchased this book elsewhere, you can visit http://www.PacktPub.com/support and register to have the code file.

Table of Contents

  1. Mastering Unity 2D Game Development - Second Edition
    1. Mastering Unity 2D Game Development - Second Edition
    2. Credits
    3. About the Authors
    4. About the Reviewer
    5. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    6. Preface
      1. What this book covers
      2. What you need for this book
      3. Who this book is for
      4. Conventions
      5. Reader feedback
      6. Customer support
        1. Downloading the example code 
        2. Downloading the color images of this book 
        3. Errata
        4. Piracy
        5. Questions
    7. 1. Overview
      1. Getting assets
      2. Unity's 2D features
        1. 2D mode versus 3D mode
        2. Working with sprites
          1. Sprite Renderer
          2. Sprite Editor
          3. Sprite sheets
          4. Texture atlases
          5. Physics 2D
      3. Changes to Unity 5
        1. Licensing
        2. Component access
        3. Animator changes
        4. Audio mixing
      4. Summary
    8. 2. Building Your Project and Character
      1. Project overview and structure
        1. Project overview
        2. Structure
        3. Asset naming
      2. Creating the project
      3. Creating a scene
      4. Sprite system
        1. Importing sprites
          1. Texture Type - Sprite (2D and UI)
          2. Sprite Mode - Single/Multiple/Polygon
          3. Packing Tag
          4. Pixels Per Units
          5. Pivot
          6. Generate Mip Maps
          7. Filter Mode
          8. Default settings and per-platform overrides
        2. Sprite Editor
          1. Sprite slicer
            1. Automatic
            2. Grid By Cell Size and Grid By Cell Count (Manual)
          2. View controls
          3. Sprite region manipulation
        3. Importing our main character
      5. GameObjects and components
        1. Sprite GameObjects
        2. Bringing our hero into the scene
      6. Classes
        1. MonoDevelop
        2. The object-orientated design
        3. The game structure
          1. The common game object
            1. The player object
      7. Planning behaviors
        1. Behaviors for the common game object
      8. Coding with components
        1. Accessing components
        2. Referencing a component
        3. Controlling the hero
      9. Going further
      10. Summary
    9. 3. Getting Animated
      1. Fundamentals of sprite animation
        1. Animation clips
          1. Animation Dope Sheet
            1. The time/recording controls (1)
            2. Animation drop-down selection (2)
            3. The sample rate (frames per second) (3)
            4. Animation properties (4)
            5. Timeline (5)
            6. Curve view (6)
        2. The Animation Controllers
        3. The Animator component
      2. Animating the main character
        1. Adding your first Animation Clip
        2. Setting up the Animator and default state
        3. Adding the other Animation Clips
        4. Planning the animation transitions
        5. Connecting the animation states
        6. Accessing controllers from a script
        7. Making her stop animating and face the correct direction
      3. Going further
      4. Summary
    10. 4. The Town View
      1. Backgrounds and layers
        1. To slice or not to slice
        2. The town background
        3. The town buildings and roads
        4. The extra scenery
      2. Building the scene
        1. Adding the town background
        2. Sprite sorting layers
        3. Sprite Sorting Order
        4. Updating the scene sorting layers
        5. Building out the scene
      3. Working with the camera
        1. Comparing Update, FixedUpdate, and LateUpdate
        2. Moving our camera with the player
        3. The perils of resolution
        4. Setting our aspect ratio and camera parameters
      4. Transitioning and bounds
        1. Towns with borders
        2. Journeying onward
      5. Going further
      6. Summary
    11. 5. Working with Unitys UI System
      1. UI Canvas
        1. EventSystem
        2. Canvas Render Mode
          1. Screen Space - Overlay
          2. Screen Space - Camera
          3. World Space
        3. Using multiple Canvases
      2. UI Text and Images
        1. UI Text
        2. UI Image
      3. UI Layout and Rect Transform
        1. Rect Tool
        2. Pivot
        3. Anchors
      4. UI Buttons
        1. Transition types
        2. Text child
        3. On Click ()
      5. Going further
      6. Summary
    12. 6. NPCs and Interactions
      1. Considering an RPG
      2. Advanced programming techniques
        1. Singletons and managers
          1. The manager approach - using empty GameObjects
          2. The singleton approach - using the C# singleton pattern
      3. Communicating between GameObjects
        1. Delegates
          1. The configurable method pattern
          2. The delegation pattern
          3. Compound delegates
        2. Events
        3. Messaging
        4. A better way
      4. Background tasks and coroutines
        1. Enter coroutines
        2. IEnumerator
        3. Yielding
        4. Starting coroutines
        5. Closing the gap
      5. Serialization and scripting
        1. Saving and managing asset data
        2. Using the serialized files in the editor
        3. Accessing the .asset files in the code
      6. Adding NPCs and a conversation system to our game
        1. Let the player walk around the NPC
        2. Stopping the player from walking through the NPC
        3. Getting the NPCs talking
          1. The conversation object
          2. Saving and serializing the object for later
          3. The conversation component
        4. Building a basic conversation system
          1. The manager
          2. Starting a conversation
          3. Preparing the UI
          4. Displaying the conversation
        5. Connecting the dots
      7. Going further
      8. Summary
    13. 7. The World Map
      1. The larger view
        1. Types of map
          1. Fixed maps
          2. Generated maps
            1. In-game generated maps
        2. Going procedural
      2. Creating our game's map
        1. Adding the world map
        2. Adding a player prefab to the overworld
        3. Adding places of interest
      3. Leaving town
        1. Creating a NavigationManager script
        2. Blocking off paths
        3. Updating build settings to include new scenes
        4. Changing scenes
      4. Returning to town
        1. Updating the NavigationManager script
      5. Going further
      6. Summary
    14. 8. Encountering Enemies and Running Away
      1. Event systems
      2. Exploring randomness
        1. True randomness
        2. Planning for random code/generation
      3. Basic Artificial Intelligence
      4. State machines
        1. Defining states
        2. Simple singular choice
        3. Planning for multiple cases
        4. State managers
      5. Sensors
      6. Setting up your battle scene
        1. Building the new scene
        2. Adding the first enemy
        3. Spawning the Dragons
          1. Creating the BattleManager
        4. Allowing the player to run away
        5. Starting the battle
        6. Saving the map position
        7. Stop immediately re-entering battle
        8. Going back to town
      7. Going further
      8. Summary
    15. 9. Getting Ready to Fight
      1. Setting up our battle state manager
        1. The battle state manager
        2. Getting to the state manager in the code
        3. Starting the battle
        4. Introductory animation
      2. Efficient RPG UI overlays
        1. The adventurer's overlay
        2. A context-sensitive overlay
        3. Modern floating UI approach
        4. Balancing the need
      3. Bring on the GUI
        1. Laying out the HUD
        2. Displaying the correct buttons
      4. Going further
      5. Summary
    16. 10. The Battle Begins
      1. Designing an interesting battle system
        1. Leveling up
        2. Balancing
      2. Preparing to attack a single enemy
      3. Beefing up the enemy AI
        1. The enemy profile/controller
        2. Updating the Dragon prefab
        3. Setting up the enemy profile in the code
      4. Selecting an attack
        1. Adding a visual effect to attack selection
      5. Selecting a target
        1. The selection circle prefab
        2. Adding selection logic to the EnemyController class
      6. Attack! Attack!
      7. Using particle effects to represent an attack
        1. Creating the materials for the particle effects
        2. Adding the particles
        3. Displaying the particles upon attack
      8. Finishing up the battle
      9. Going further
      10. Summary
    17. 11. Shopping for Items
      1. Why do we shop?
      2. The power of an item
      3. Building your shop
      4. Laying out your inventory
        1. Rule of 99'
        2. Encumbrance systems
        3. Slot-based systems
        4. A mini game
        5. Real world
      5. Creating a shop and inventory
        1. Gathering shop assets
        2. Building the shop scene
        3. Creating inventory items
        4. Managing the shop
        5. Updating the player inventory definition
        6. Stocking the shop
        7. Linking up the buttons
        8. Turning off the Buy Button
        9. Entering the shop
        10. Leaving the shop
        11. Managing your inventory
        12. Adding objects to the player's inventory
      6. Going further
      7. Summary
    18. 12. Sound and Music
      1. Choosing the appropriate sound and music
      2. Where to get sound and music for your game
        1. Free resources
      3. Audio listeners and audio sources
      4. Adding background music
        1. Creating a splash screen
        2. Adding the audio source
        3. Transitioning to the next scene
        4. Keeping the music after the scene transition
      5. Adding sound effects
        1. Adding a sound to the buy button
      6. Muting/unmuting audio
      7. Going further
      8. Summary
    19. 13. Putting a Bow on It
      1. Building in-game menu structures
        1. The screens
          1. Splash screens
          2. Loading screens
          3. The main menu
          4. Save slots/level selections
          5. Settings pages
          6. The About screen
          7. Privacy policy
          8. Pause screens
          9. Additional menus (purchasing, achievements, leaderboards, and so on)
          10. Social
        2. The flow
      2. Finishing our splash screen
      3. Building our start screen
      4. Extending the editor
        1. The property drawers
          1. Examples property drawers
            1. Built-in property drawers
            2. Custom property drawers
        2. Custom editors
        3. The editor window
        4. Gizmos
        5. Building your editor menus
          1. Adding a MenuItem attribute
          2. Enabling/disabling a MenuItem attribute
          3. Adding shortcut keys to a MenuItem attribute
          4. Adding contextual MenuItems
        6. Running scripts in the Editor folder
          1. Alternative approaches
            1. The [InitialiseOnLoad] attribute
            2. Editor application callbacks
            3. Mixing it up
      5. Working with settings
        1. Using PlayerPrefs
        2. Serializing your data
        3. Saving data to disk
        4. Backing up to the Web
      6. Going further
      7. Summary
    20. 14. Deployment and Beyond
      1. Handling platform differences
        1. Preprocessor directives
      2. Pushing code from Unity
        1. Processing assets
        2. Processing the build
      3. Building your assets
      4. Packaging gotchas
      5. Distributing to mobile
      6. Social network integration
      7. Monetization
        1. Paid
        2. Paid with trial
        3. Ad supported
        4. In-app purchases
        5. In-game currency
      8. Going further
      9. Summary