Learning C# 7 By Developing Games with Unity 2017 - Third Edition

Book description

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

About This Book

  • This is a step-by-step guide to learn the fundamentals of C# 7 scripting to develop GameObjects and master the basics of the new UI system in Unity
  • Build and develop your 2D game right from scratch while implementing the principles of object-oriented programming and coding in C# 7
  • Get to grips with the fundamentals of optimizing your game using the latest features of Unity 2017

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

  • Learn C# 7 using new features like tuples, variables, and non-nullable reference types while building games
  • Understand the fundamentals of variables, methods, and code syntax in C#
  • Use loops and collections efficiently in Unity to reduce the amount of code
  • Develop a game using the object-oriented programming principles
  • Implement simple enemy characters into the game to learn point to point movement and Tree behaviors
  • Avoid performance mistakes by implementing different optimization techniques
  • Export 3D models and 3D animations and import them inside a Unity project
  • With your new knowledge of coding, you will be able to look at Unity's Scripting Reference code examples with confidence

In Detail

With the latest version of Unity 2017 released, are you interested in developing creative and interactive games while learning C# alongside? Then this is the book that you are looking for. Its all about offering you a fun introduction to the world of game programming with C#.

You'll start with the basics to get started with C# 7 and its latest features. Then you'll see how to use C# 7 and its latest functional programming capabilities to create amazing games with Unity 2017.

You'll create your first C# script for Unity, add objects into it, and learn how to create game elements with them. Then you'll work with the latest functional programming features of C# and how to leverage them for great game scripting.

Throughout the book, you'll learn to use the new Unity 2017 2D tool set and create an interactive 2D game with it. You will make enemies appear to challenge your player, and go through some optimization techniques to ensure great game performance. At the end, your 2D game will be transformed into 3D, and you'll be able to skill up to become a pro C# programmer with Unity 2017!

Style and approach

The book takes a practical, step-by-step approach where you learn C# coding while developing fun and interactive games.

Table of contents

  1. 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. Errata
      3. Piracy
      4. Questions
  2. 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
    5. C# documentation – where to find it and do I need it at all?
      1. The Unity community – asking others for help
    6. Working with C# script files
    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
      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
      6. Lots of files can create a mess
      7. Why does my Project tab look different?
    9. Instance? What is this?
    10. Summary
  3. 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 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. Pencil and paper are powerful tools
    8. Summary
  4. 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
  5. 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
  6. 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
  7. 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
  8. Object, a Container with Variables and Methods
    1. Working with objects is a class act
      1. A few facts
      2. Example
    2. Instantiating an object
    3. Bored yet?
    4. Using methods with objects
    5. Custom constructors
    6. Overloading
    7. Summary
  9. Let&#x27;s Make a Game! – from Idea to Development
    1. Your first game – avoiding the trap of the never–ending concept
    2. What do I need to learn before I start creating my own game?
      1. The concept
    3. Game mechanics and core components
    4. Breaking the concept into smaller parts
    5. Testing the mechanics
      1. Level design
      2. An animated 2D character
      3. Physics
      4. Mouse and touch controls
      5. Collectables and obstacles
      6. Scoring
      7. UI – the user interface
    6. Target platform and resolution
    7. Target screen resolution
    8. Summary
  10. 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
  11. 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
  12. The Game Level
    1. Designed levels versus generated levels
    2. Creating a designed level
    3. Creating a generated level
      1. Planning the LevelGenerator class
      2. Writing the script LevelGenerator
    4. Creating a copy of the level piece
    5. Instantiating
    6. Vector3
    7. Testing LevelGenerator
    8. Extending the level
    9. The code used in this chapter
    10. Summary
  13. The User Interface
    1. Introducting the Unity UI
    2. Views
      1. Constructing the view UI – how to keep things clean
      2. Target screen resolution
      3. Recognizing events
    3. Buttons
      1. Basic button
        1. The image
        2. The Button component
        3. Interaction
        4. 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
  14. Collectables
    1. Collectables
    2. The coin prefab
      1. The Collectable class
    3. High score and persisting data
    4. Health Points and Health bar
    5. Magic Points and Magic bar
    6. The code in this chapter
    7. Summary
  15. Enemies
    1. What makes an enemy?
    2. Movement
      1. Movement by animation
      2. Trigger movement
      3. Making it an enemy
    3. The code in this chapter
    4. Summary
  16. Audio, 3D Games, and Export
    1. How to add sound effects and music
      1. Where to find sound effects and music
      2. Adding music
      3. Adding sound effects
        1. Through animation
        2. Through script
    2. How to create a 3D game
      1. 3D models
      2. 3D animations
      3. Animator
    3. How to export and make it playable
    4. Code
      1. PlayerController.cs
    5. Summary

Product information

  • Title: Learning C# 7 By Developing Games with Unity 2017 - Third Edition
  • Author(s): Micael McCormick, Greg Lukosek
  • Release date: December 2017
  • Publisher(s): Packt Publishing
  • ISBN: 9781788478922