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

Learning C# by Developing Games with Unity 5.x - Second Edition

Book Description

Develop your first interactive 2D platformer game by learning the fundamentals of C#

About This Book

  • Get to grips with the fundamentals of scripting in C# with Unity
  • Create an awesome, 2D platformer game from scratch using the principles of object-oriented programming and coding in C#
  • This is a step-by-step guide to learn the fundamentals of C# scripting to develop GameObjects and master the basics of the new UI system in Unity

Who This Book Is For

The book is targeted at beginner level Unity developers with no programming experience. If you are a Unity developer and you wish to learn how to write C# scripts and code by creating games, then this book is for you.

What You Will Learn

  • Understand the fundamentals of variables, methods, and code syntax in C#
  • Get to know about techniques to turn your game idea into working project
  • Use loops and collections efficiently in Unity to reduce the amount of code
  • Develop a game using the object-oriented programming principles
  • Generate infinite levels for your game
  • Create and code a good-looking functional UI system for your game
  • Publish and share your game with users

In Detail

Unity is a cross-platform game engine that is used to develop 2D and 3D video games. Unity 5 is the latest version, released in March 2015, and adds a real-time global illumination to the games, and its powerful new features help to improve a game’s efficiency.

This book will get you started with programming behaviors in C# so you can create 2D games in Unity. You will begin by installing Unity and learning about its features, followed by creating a C# script. We will then deal with topics such as unity scripting for you to understand how codes work so you can create and use C# variables and methods. Moving forward, you will find out how to create, store, and retrieve data from collection of objects.

You will also develop an understanding of loops and their use, and you’ll perform object-oriented programming. This will help you to turn your idea into a ready-to-code project and set up a Unity project for production. Finally, you will discover how to create the GameManager class to manage the game play loop, generate game levels, and develop a simple UI for the game.

By the end of this book, you will have mastered the art of applying C# in Unity.

Style and approach

This is a step-by-step guide to developing a game from scratch by applying the fundamentals of C# and Unity scripting.

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. Learning C# by Developing Games with Unity 5.x Second Edition
    1. Table of Contents
    2. Learning C# by Developing Games with Unity 5.x Second Edition
    3. Credits
    4. About the Author
    5. About the Reviewer
    6. www.PacktPub.com
      1. eBooks, discount offers, and more
        1. Why subscribe?
    7. 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
    8. 1. Discovering Your Hidden Scripting Skills and Getting Your Environment Ready
      1. Prerequisite knowledge to use this book
      2. Dealing with scriptphobia
      3. Downloading Unity
        1. Obtaining a free license
      4. Teaching behavior to GameObjects
        1. Using Unity's documentation
        2. Do I need to know all that?
      5. C# documentation – where to find it? Do I need it at all?
        1. The Unity community – asking others for help
      6. Working with C# script files
        1. Lots of files can create a mess
        2. Why does my Project tab look different?
      7. Creating a C# script file
      8. Introducing the MonoDevelop code editor
        1. Syncing C# files between MonoDevelop and Unity
        2. Opening LearningScript in MonoDevelop
          1. The namespace – highlighted in blue
          2. The class definition – highlighted in green
        3. Watching for possible gotchas while creating script files in Unity
        4. Fixing synchronization if it isn't working properly
        5. Adding our script to GameObject
      9. Instance? What is it?
      10. Summary
    9. 2. Introducing the Building Blocks for Unity Scripts
      1. Understanding what a variable is and what it does
        1. Naming a variable
        2. A variable name is just a substitute for a value
        3. Creating a variable and seeing how it works
          1. Declaration
          2. Assignment
        4. Click on Play!
        5. Changing variables
        6. Watching for a possible gotcha when using public variables
      2. What is a method?
        1. Using the term "method" instead of "function"
        2. Method names are substitutes, too
      3. Introducing the class
        1. Inheritance
      4. The Start(), Update(), and Awake() methods and the execution order
      5. Components that communicate using dot syntax
        1. What's with the dots?
      6. Making decisions in code
        1. Using the NOT operator to change the condition
        2. Checking many conditions in an if statement
        3. Using else if to make complex decisions
        4. Making decisions based on user input
      7. Paper and pencil are powerful tools

      8. Summary
    10. 3. Getting into the Details of Variables
      1. Writing C# statements properly
      2. Understanding component properties in Unity's Inspector
        1. Variables become component properties
        2. Unity changes script and variable names slightly
      3. Changing a property's value in the Inspector panel
        1. Displaying public variables in the Inspector panel
      4. Private variables
      5. Naming your variables properly
      6. Beginning variable names with lowercase
      7. Using multiword variable names
      8. Declaring a variable and its type
        1. The most common built-in variable types
      9. Assigning values while declaring a variable
      10. Where you declare a variable is important
      11. Variable scope – determining where a variable can be used
      12. Summary
    11. 4. Getting into the Details of Methods
      1. Using methods in a script
      2. Naming methods properly
        1. Beginning method names with an uppercase letter
        2. Using multiword names for a method
        3. Parentheses are part of the method's name
      3. Defining a method the right way
        1. The minimum requirements for defining a method
      4. Understanding parentheses – why are they there?
      5. Specifying a method's parameters
        1. How many parameters can a method have?
      6. Returning a value from a method
        1. Returning the value
          1. Example
      7. Summary
    12. 5. Lists, Arrays, and Dictionaries
      1. What is an array?
        1. Declaring an array
      2. Storing items in the List
        1. Common operations with Lists
      3. List<T> versus arrays
      4. Retrieving the data from the Array or List<T>
        1. Checking the size
      5. ArrayList
      6. Dictionaries
        1. Accessing values
        2. How do I know what's inside my Hashtable?
      7. Summary
    13. 6. Loops
      1. Introduction to loops
      2. The foreach loop
      3. The for loop
        1. An example
      4. The while loop
      5. while versus for loops
      6. Loops in statements
      7. Modulo
      8. Searching for data inside an array
      9. Breaking the loop
      10. Summary
    14. 7. Object, a Container with Variables and Methods
      1. Working with objects is a class act
        1. Few facts
        2. Example
      2. Instantiating an object
      3. Bored yet?
      4. Using methods with objects
      5. Custom constructors
      6. Overloading
      7. Summary
    15. 8. Let's Make a Game! – From Idea to Development
      1. Your first game – avoiding the trap of the never-ending concept
      2. The idea
        1. Game mechanics and core components
      3. Breaking a complex idea into smaller parts
      4. Jake on the mysterious planet – the feature list
        1. Procedural level generation
        2. An animated 2D character
        3. Physics
        4. Mouse and touch controls
        5. Collectables and obstacles
        6. Scoring
        7. UI – the user interface
      5. Target platform and resolution
      6. Target screen resolution
      7. Summary
    16. 9. Starting Your First Game
      1. Setting up a new Unity project for our game
        1. Backup
        2. Keeping your project clean
      2. Preparing the player prefab
        1. Rigidbody2D
        2. CircleCollider2D
      3. PlayerController
      4. User input
        1. Jump
      5. Animator
        1. Running
      6. Code
        1. PlayerController.cs
      7. Summary
    17. 10. Writing GameManager
      1. Gameplay loops
      2. Singleton class
      3. Starting the game
      4. Setting up input keys
      5. Using triggers
      6. Restarting the game
      7. Setting up the player starting position
      8. Code in this chapter
      9. Summary
    18. 11. The Game Level
      1. Generating levels versus designed levels
        1. Creating a level chunk
        2. Planning the LevelGenerator class
        3. Writing LevelGenerator
        4. Commenting on your code
      2. Creating a copy of the level piece
      3. Instantiating
      4. Vector3
      5. Testing LevelGenerator
      6. Extending the level
      7. The code used in this chapter
      8. Summary
    19. 12. The User Interface
      1. Introducting the Unity UI
        1. Constructing the view UI – how to keep things clean
        2. Target screen resolution
        3. Recognizing events
      3. Buttons
        1. A simple button
        2. Image
        3. The Button component
        4. Interaction
        5. The Button action
      4. Hiding and showing the Canvas
      5. Reference exceptions
      6. GameView
        1. Game Over
      7. The code in this chapter
      8. Summary
    20. 13. Collectables — What Next?
      1. Collectables
      2. The coin prefab
        1. The Collectable class
      3. High score and persisting data
      4. The Update function and UI values
      5. What next?
      6. The code in this chapter
      7. Summary
    21. Index