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

Book Description

Become an expert in Unity3D's new 2D system, and then join in the adventure to build an RPG game framework!

In Detail

Unity3D has long been seen as a massive 3D game-making middleware system, with lots of power and an easy-to-use editor. Now, with 2D games back in fashion, Unity has created a 2D toolset for developers with the know-how to create great games.

If you are looking for a book that will show you how to make a fully functional, customizable RPG game end to end, then this is the book for you. You will learn how to build an RPG game framework, learning lots of tips and tricks along the way, from advanced C# scripting to getting the most out of Unity's built in features such as Mecanim and curves in ways you may have not even considered.

By the end of this book, you will be able to architect, create, deploy, and integrate your game with all of your intended platforms, and have the knowledge to build and customize the Unity editor for the games you create with confidence. You will also be tooled with tricks from the trade on marketing, monetization, targeting as many platforms as possible, with a keen focus on how to best profit from your title.

What You Will Learn

  • Explore the new features introduced in Unity3D 4.3 by building your own RPG game
  • Delve into the new sprite importing and rendering system
  • Master the best parts of the new Sprite animation system and improved Mecanim features
  • Make the most of your rendering with tags, layers, sprite sorting, and ordering
  • Dissect the proper use of Delegates, Events, and Messaging, including tips and tricks, and build a full RPG conversation system
  • Do battle with Mecanim-based AI and state machines
  • Build for success with tips on marketing, monetization, and deployment
  • Drive home the platform of choice and expand into deployment/editor extensions

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 files e-mailed directly to you.

Table of Contents

  1. Mastering Unity 2D Game Development
    1. Table of Contents
    2. Mastering Unity 2D Game Development
    3. Credits
    4. About the Author
    5. Acknowledgments
    6. About the Reviewers
    7. www.PacktPub.com
      1. Support files, eBooks, discount offers, and more
        1. Why subscribe?
        2. Free access for Packt account holders
    8. 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
    9. 1. Overview
      1. Getting assets
        1. Collection of sites
      2. Welcome to 2D
        1. The new 2D mode
        2. Sprites
          1. Sprite Renderer
          2. Sprite Editor
          3. Spritesheets
          4. Texture atlases
          5. Sprite meshes
          6. The Box2D physics system
          7. The new animation Dope Sheet
        3. Other Unity 4.3 improvements
          1. Improved Mecanim performance
          2. The Windows Phone API improvements and Windows 8.1 support
          3. Dynamic Nav Mesh (Pro version only)
          4. Editor updates
          5. MonoDevelop 4.01
          6. Movie textures
      3. Summary
    10. 2. Building a Character
      1. Getting your project started – the right way
        1. Structure
        2. Object naming
      2. Understanding components
        1. Accessing components using a shortcut
        2. A better way to use components – referencing
        3. An even better way
      3. Creating the project
      4. Classes
        1. The object-orientated design
        2. The game structure
          1. The common game object
            1. The player object
          2. More later
      5. Planning behaviors
        1. Behaviors for the common game object
        2. Behaviors for the player's character
      6. Introducing the Unity sprite system
        1. Importing sprites
          1. Texture type – sprite
          2. Sprite Mode – single/multiple
          3. Packing tag
          4. Pixels to units
          5. The Sprite Editor button
        2. Sprite Editor
          1. Sprite splitter (slicer)
            1. Automatic
            2. Grid (manual)
          2. View controls
          3. Sprite region manipulation
      7. Putting it together
        1. Importing our main character
        2. The hero
        3. Controlling the hero
      8. Going further
      9. Summary
    11. 3. Getting Animated
      1. Sprite animation
        1. Animation components
          1. Animation controllers
          2. Animation clips
          3. The Animator component
        2. Setting up animation controllers
        3. Setting up animation clips
          1. Manual animation clip creation
          2. Automatic animation clip creation
          3. Animator Dope Sheet
            1. The time/recording controls
            2. Selecting the animation clip
            3. The sample rate (frames per second)
            4. Animation properties
            5. Timeline
          4. Dope/curve view
      2. Putting it together
        1. Setting up the animation controller
        2. Adding your first animation clip (idle)
        3. Adding another animation clip (run)
        4. Connecting animation states
        5. Accessing controllers from a script
        6. Extra credit
        7. Getting curvy
      3. Going further
      4. Summary
    12. 4. The Game World
      1. Backgrounds and layers
        1. To slice or not to slice
        2. The town background
        3. The town buildings
        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
      3. Working with the camera
        1. Comparing Update, FixedUpdate, and LateUpdate
        2. The perils of resolution
        3. A better FollowCamera script
      4. Transitioning and bounds
        1. Towns with borders
        2. Journeying onwards
        3. Planning for the larger picture
      5. Backgrounds and active elements
        1. Parallaxing
        2. Foreground objects
      6. Shaders and 2D
      7. Going further
      8. Summary
    13. 5. NPCs and Interactions
      1. Considering an RPG
      2. Advanced programming techniques
        1. Singletons and managers
          1. The manager approach – using empty game objects
          2. The singleton approach – using the C# singleton pattern
      3. Communicating between game objects
        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. Putting it all together
        1. Getting the NPC's talking
          1. The conversation object
          2. Saving and serializing the object for later
          3. The Conversation Component
        2. Building a basic conversation system
          1. The manager
          2. Starting a conversation
          3. Displaying the conversation
          4. Adding more
        3. Connecting the dots
      7. Going further
      8. Summary
    14. 6. The Big Wild World
      1. The larger view
        1. Types of maps
          1. Fixed maps
          2. Generated maps
            1. In-game generated maps
        2. 2D doesn't mean you have to use only 2D
        3. Going procedural
        4. Screen space and world space
      2. Putting it together
        1. Adding the map
        2. Adding places of interest
        3. Creating the MapPoint prefabs for reuse
        4. Updating the NavigationPrompt script
        5. Updating the NavigationManager script
        6. Traveling by click or touch
          1. Managing input
          2. Managing input priorities
          3. Managing input order
          4. Getting curvy
          5. Fixing the start location
          6. Traveling too far
        7. Transitions
          1. Updating level loading to use fading
        8. Updating build settings to include new scenes
      3. Going further
      4. Summary
    15. 7. Encountering Enemies and Running Away
      1. Event systems
      2. Exploring randomness
        1. Planning for random code/generation
        2. True randomness
      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. Putting it together
        1. Building the new scene
        2. Adding the first enemy
        3. Spawning the horde
          1. Getting back home
        4. The missing random piece
        5. One last thing
      7. Going further
      8. Summary
    16. 8. Shopping for Weapons
      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 system
        3. Slot-based system
        4. A mini game
        5. Real world
      5. Getting paid
        1. Paid
        2. Paid with trial
        3. Ad supported
        4. In-app purchase
        5. In-game currency
      6. Putting it together
        1. Gathering the shop assets
        2. Building the shop scene
        3. Creating inventory items
        4. Managing the shop
        5. Adding 2D button behaviors
        6. Updating the player inventory definition
        7. Stocking the shop
        8. Leaving the shop
        9. Entering the shop
        10. Managing your inventory
        11. Adding the Player inventory behavior
      7. Going further
      8. Summary
    17. 9. Getting Ready to Fight
      1. Efficient RPG UI overlays
        1. The adventurer's overlay
        2. A context-sensitive overlay
        3. Modern floating UI approach
        4. Balancing the need
      2. Putting it together
        1. The battle state manager
        2. Getting to the state manager in the code
        3. Starting the battle
        4. Adding a little flair
        5. Bring on the GUI
          1. The command bar
          2. The command button
          3. Adding the command bar to the scene
        6. Selecting the weapon
          1. Selecting a command button
          2. Managing the selections from the command bar
          3. Updating the BattleManager state with selections
          4. Updating the BattleManager state with a weapon
      3. Going further
      4. Summary
    18. 10. Fight and Gain XP
      1. Proving ground
      2. Leveling up
      3. Balancing
      4. Putting it together
        1. Preparing the BattleManager script
        2. Beefing up the enemy
          1. The enemy profile/controller
          2. Updating the Goblin prefab
          3. Setting up the enemy profile in the code
        3. Selecting a target
          1. The selection circle prefab
          2. Adding selection logic to the EnemyController class
        4. Attack! Attack!
        5. The pesky Goblin reacts with 3D particles
          1. Mixing up 2D and 3D
          2. Particle effects and 2D
          3. Adding the deathly sprites
          4. Creating the material for the particle effect
          5. Restructuring the Goblin prefab
          6. Adding the particles
          7. The death animation
          8. Adding particles to the animation
          9. Connecting the dots
          10. Making the new GoblinEnemy game object a prefab and adding it to the battle
          11. Houston, we have a problem!
            1. Mecanim AI considerations
            2. Animation, states, and looping
        6. The final run-through
      5. Going further
      6. Summary
    19. 11. Onward Wary Traveler
      1. Extending the editor
        1. The property drawers
          1. Property drawers examples
            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. The EditorApplication callbacks
            3. Mixing it up
      2. 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. About the screen
          7. Privacy policy
          8. Pause screens
          9. Additionals (purchasing, achievements, leaderboards, and so on)
          10. Social
        2. The flow
      3. Working with settings
        1. Using PlayerPrefs
        2. Serializing your data
        3. Saving data to disk
        4. Modeling your saved data
        5. Making your game save and load functions
        6. Testing your Save and Load functions
        7. Backing up to the Web
      4. Going further
      5. Summary
    20. 12. Deployment and Beyond
      1. Handling platform differences
        1. Preprocessor directives
          1. Updating the save system for another platform
          2. Build note
        2. Getting access to Unity
          1. Accessing the UnityEngine namespace
          2. The static classes
        3. Access to the platform
          1. Static events in your Unity project
          2. Embedding platform DLLs
            1. The editor plugin (.NET only)
            2. The platform plugin
            3. Accessing the plugin
        4. Native plugins (Pro only)
      2. Pushing code from Unity3D
        1. Processing assets
        2. Processing the build
      3. Building your assets
      4. Packaging gotchas
      5. Marketing your game
      6. Summary
    21. A. Additional Resources
      1. Scripting resources
        1. Extending the editor
        2. Even more AI
        3. Procedural generation
        4. Advanced coding
        5. Other scripting resources
      2. Useful assets
      3. Sources of art and SFX
      4. Highlights from the Web
    22. Index