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# 7 By Developing Games with Unity 2017 - Third Edition

Book Description

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

About This Book

  • Learn the fundamentals of C# 7 scripting to develop GameObjects and master the basics of the new UI system in Unity 2017
  • Build and develop your 2D game right from scratch and extend it to 3D 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

This book is for game developers and enthusiasts who want to get started with game development with Unity 2017. No prior experience of C# is required.

What You Will Learn

  • Create your first 2D and 3D games in Unity
  • 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 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 animations and import them inside a Unity project

In Detail

Do you want to learn C# programming by creating fun and interactive games using the latest Unity 2017 platform? If so, look no further; this is the right book for you. Get started with programming C# so you can create 2D and 3D games in Unity. We will walk you through the basics to get you started with C# 7 and its latest features. Then, explore the use of C# 7 and its latest functional programming capabilities to create amazing games with Unity 2017.

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

Throughout the book, you will 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 discover some optimization techniques for great game performance. At the end, you will learn how to transform a 2D game into 3D, and you will 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.

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. 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