Learning C# by Developing Games with Unity 3D Beginner's Guide


Learning C# by Developing Games with Unity 3D Beginner's Guide
eBook: $29.99
Formats: PDF, PacktLib, ePub and Mobi formats
$25.49
save 15%!
Print + free eBook + free PacktLib access to the book: $79.98    Print cover: $49.99
$49.99
save 37%!
Free Shipping!
UK, US, Europe and selected countries in Asia.
Also available on:
Overview
Table of Contents
Author
Support
Sample Chapters
  • You've actually been creating scripts in your mind your whole life, you just didn't realize it. Apply this logical ability to write Unity C# scripts
  • Learn how to use the two primary building blocks for writing scripts: the variable and the method. They're not mysterious or intimidating, just a simple form of substitution
  • Learn about GameObjects and Component objects as well as the vital communication between these objects using Dot Syntax. It's easy, just like addressing a postal letter
  • Stay logically organized by utilizing a State Machine for your code. Use the simple concept of a State to control your Unity project. You will definitely save time by knowing where your code is located
  • With your new knowledge of coding, you will be able to look at Unity's Scripting Reference code examples with confidence

Book Details

Language : English
Paperback : 292 pages [ 235mm x 191mm ]
Release Date : September 2013
ISBN : 1849696586
ISBN 13 : 9781849696586
Author(s) : Terry Norton
Topics and Technologies : All Books, Game Development, Beginner's Guides

Table of Contents

Preface
Chapter 1: Discovering Your Hidden Scripting Skills
Chapter 2: Introducing the Building Blocks for Unity Scripts
Chapter 3: Getting into the Details of Variables
Chapter 4: Getting into the Details of Methods
Chapter 5: Making Decisions in Code
Chapter 6: Using Dot Syntax for Object Communication
Chapter 7: Creating the Gameplay is Just a Part of the Game
Chapter 8: Developing the State Machine
Chapter 9: Start Building a Game and Get the Basic Structure Running
Chapter 10: Moving Around, Collisions, and Keeping Score
Chapter 11: Summarizing Your New Coding Skills
Appendix A: Initial State Machine files
Appendix B: Completed code files for Chapters 9 and 10
Appendix C: Pop Quiz Answers
Index
  • Chapter 1: Discovering Your Hidden Scripting Skills
    • Prerequisite knowledge for using this book
    • Dealing with scriptphobia
      • Teaching behaviors to GameObjects
    • Choosing to use C# instead of UnityScript
      • Reason 1 for choosing C# – vast amount of documentation on the Internet
      • Reason 2 for choosing C# – flexibility to use Unity scripts and regular C# code files
      • Reason 3 for choosing C# – coding rules are specific
    • Maneuvering around Unity's documentation
    • Time for action – opening the Reference Manual documentation for the transform Component
    • Time for action – opening the scripting reference documentation for the transform component
      • Are we really supposed to know all that stuff?
      • What is all that information?
    • Working with C# script files
    • Time for action – create a C# script file
      • Introducing the MonoDevelop code editor
      • Syncing C# files between MonoDevelop and Unity
    • Time for action – opening LearningScript in MonoDevelop
      • Watching for a possible "gotcha" when creating script files in Unity
      • Fixing sync if it isn't working properly
    • Summary
    • Chapter 2: Introducing the Building Blocks for Unity Scripts
      • Using the term method instead of function
      • Understanding what a variable does in a script
        • Naming a variable
        • A variable name is just a substitute for a value
      • Time for action – creating a variable and seeing how it works
      • Time for action – changing the number 9 to a different number
      • Using a method in a script
        • What is a method?
      • Time for action – learning how a method works
        • What's in this script file?
        • Method names are substitutes too
      • Introducing the class
      • By using a little Unity magic, a script becomes a Component
        • A more technical look at the magic
        • Even more Unity magic
      • Components communicating using the Dot Syntax
        • What's with the dots?
      • Summary
      • Chapter 3: Getting into the Details of Variables
        • Writing C# statements properly
        • Understanding Component properties in Unity's Inspector
          • Variables become Component properties
          • Unity changes script and variable names slightly
          • Changing a property's value in the Inspector panel
        • Displaying public variables in the Inspector panel
        • Time for action – making a variable private
        • Naming your variables properly
          • Begin variable names with lowercase
          • Using multi-word variable names
        • Declaring a variable and its type
          • The most common built-in variable types
        • Time for action – assigning values while declaring the variable
          • Where you declare a variable is important
          • Variable scope – determining where a variable can be used
        • Summary
        • Chapter 4: Getting into the Details of Methods
          • Ending a method definition using curly braces
          • Using methods in a script
          • Naming methods properly
            • Begin method names with an uppercase letter
            • Using multi-word names for a method
            • Parentheses are part of the method name
          • Defining a method properly
            • The minimum requirements for defining a method
            • Understanding parentheses – why are they there?
          • Time for action – adding code between the parentheses
            • Specifying a method's parameters
            • How many parameters can a method have?
          • Calling a method
            • Using arguments in the parentheses of a method
          • Returning a value from a method
          • Time for action – returning a value from AddTwoNumbers()
            • Calling a method is a logic detour
          • Using Unity's Update and Start methods
            • The Start method is called one time
            • The Update method is called over and over and over…
          • Summary
          • Chapter 5: Making Decisions in Code
            • Testing conditions with an if statement
              • Testing if conditions are true or false
            • Time for action – create a couple of if statements
              • Using the NOT operator to change the condition
              • Checking many conditions in an if statement
            • Time for action – create if statements with more than one condition to check
            • Using an if-else statement to execute alternate code
            • Time for action – add "else" to the if statement
            • Making decisions based on user input
            • Storing data in an array, a List, or a Dictionary
              • Storing items in an array
              • Storing items in a List
            • Time for action – create a List of pony names
              • Storing items in a Dictionary
            • Time for action – create a dictionary of pony names and keys
              • Using a Collection Initializer to add items to a List or Dictionary
            • Time for action – adding ponies using a Collection Initializer
            • Looping though lists to make decisions
              • Using the foreach loop
            • Time for action – using foreach loops to retrieve data
              • Using the for loop
            • Time for action – selecting a pony from a List using a for loop
              • Using the while loop
            • Time for action – finding data and breakout of the while loop
            • Summary
            • Chapter 6: Using Dot Syntax for Object Communication
              • Using Dot Syntax is like addressing a letter
                • Simplifying the dots in Dot Syntax
                • Using access modifiers for variables and methods
              • Working with objects is a class act
              • Using Dot Syntax in a script
                • Accessing a Component's own variables and methods
              • Time for action – accessing a variable in the current Component
                • Accessing another Component on the current GameObject
              • Time for action – communicating with another Component on the Main Camera
                • Accessing other GameObjects and their Components
              • Time for action – creating two GameObjects and a new script
              • Accessing GameObjects using drag-and-drop versus writing code
              • Time for action – trying drag-and-drop to assign a GameObject
              • Summary
              • Chapter 7: Creating the Gameplay is Just a Part of the Game
                • Applying your new coding skills to a State Machine
                • Understanding the concepts of a State Machine
                  • Benefits of by using a State Machine
                • Following the State Machine logic flow
                  • Delegating game control to a State
                  • Switching to another State when called to do so
                  • Keeping track of the active State
                • Creating Components objects and C# objects
                  • Unity creates Components behind the scenes
                  • Instantiate a class to create an object
                • Time for action – creating a script and a class
                • Time for action – instantiating the BeginState class
                  • Specifying a file's location with a namespace declaration
                  • Locating code files with a using statement
              • Introducing the C# interface
                • The State Machine and the interface guarantee
              • Time for action – implementing an interface
              • Summary
                • Chapter 8: Developing the State Machine
                  • Creating four State classes
                  • Time for action – modyifing BeginState and add three more States
                  • Setting up the StateManager controller
                    • Studying an example of inheritance
                    • Enter the IStateBase interface again
                  • Time for action – modify StateManager
                    • Adding another State
                  • Time for action – modifying PlayState to add another State
                    • Adding OnGUI to the StateManager class
                  • Time for action – adding OnGUI() to StateManager
                  • Changing the active State and controlling the Scene
                  • Time for action – adding GameObjects and a button to the Scene
                    • Pausing the game Scene
                  • Time for action – adding code to pause the game Scene
                  • Time for action – creating a timer in BeginState
                  • Changing Scenes
                  • Time for action – setting up another Scene
                    • Changing Scenes destroys the existing GameObjects
                      • Keeping GameManager between scenes
                  • Time for action – adding the Awake method to StateManager
                    • Changing the Scenes
                  • Time for action – adding the code to change the Scenes
                    • Verifying the code of your classes
                  • Summary
                  • Chapter 9: Start Building a Game and Get the Basic Structure Running
                    • Easing into Unity's scripting documentation
                      • Reading the Unity Reference Manual first
                      • Finding code examples in the Scripting Reference as needed
                    • Setup the State Machine and add a Player GameObject
                    • Time for action – setting up nine States and three Scenes
                      • Calling the Restart method of the StateManager
                      • Add a Player GameObject
                        • Placing and using the Player Collider
                        • Placing and using the Sphere Collider
                    • Time for action - adding a Player GameObject
                      • Storing game data in its own script
                    • Time for action – creating a GameData script
                      • Displaying the splash screens
                    • Controlling the Player GameObject
                    • Time for action – rotating Player in SetupState
                      • Adding the Player Color option
                    • Time for action – changing the color using GUI buttons
                      • Adding the Lives option for Player
                    • Time for action – setting the Lives for Player
                    • Summary
                    • Chapter 10: Moving Around, Collisions, and Keeping Score
                      • Visualizing the completed game
                      • Switching to the first play State and playable scene
                        • Loading Scene1 using code
                      • Adding cameras for different viewing options
                      • Time for action – setting up two additional cameras in the scene
                        • Attaching scripts to the new cameras
                      • Time for actioning – attach the LookAtPlayer camera script
                      • Time for action – attaching the FollowingPlayer camera script
                      • Moving the Player using Rigidbody physics
                      • Time for action – adding a Rigidbody to the Player
                      • Keeping score during the game
                        • Initializing the scoring system
                        • Keeping score in the Scene1 play State
                          • Losing the game in Scene1
                          • Winning the level in Scene1
                        • Determining how to win or lose
                      • Time for action – creating a good and bad prefab
                        • Scoring for the win
                        • Losing when Player crashes
                    • Shooting projectiles at the orbs
                    • Time for action – creating the EnergyPulse prefab
                      • Shooting a single-shot EnergyPulse
                      • Shooting rapid-fire EnergyPulses
                      • The EnergyPulse is fired
                      • Controlling EnergyPulse objects
                    • Summary
                      • Chapter 11: Summarizing Your New Coding Skills
                        • Coding a Unity Project
                          • Working with objects
                        • Scratching the surface of C# programming
                          • Looking at even more C# features
                          • Looking at even more Unity features
                        • Controlling the game with a State Machine
                          • Using a State Machine is a design pattern choice
                          • Using the State Machine at the GameObject level
                          • Pulling all the little C# pieces together
                        • Learning more after this book
                          • Visit my favorite website for C#
                          • Visit my favorite websites for Unity coding:
                        • Summary
                        • Appendix A: Initial State Machine files
                          • BeginState
                          • SetupState
                          • PlayStateScene1_1: (1 of 2 available States in Scene1)
                          • PlayStateScene1_2: (2 of 2 available States in Scene1)
                          • WonStateScene1
                          • LostStateScene1
                          • PlayStateScene2
                          • WonStateScene2
                          • LostStateScene2
                          • StateManager
                          • IStateBase
                          • Appendix B: Completed code files for Chapters 9 and 10
                            • BeginState
                            • SetupState
                            • PlayStateScene1_1: (1 of 2 available States in Scene1)
                            • PlayStateScene1_2: (2 of 2 available States in Scene1)
                            • WonStateScene1
                            • LostStateScene1
                            • PlayStateScene2
                            • WonStateScene2
                            • LostStateScene2
                            • StateManager
                            • PlayerControl
                            • GameData
                            • LookAtPlayer
                            • FollowingPlayer
                            • EnergyPulsePower
                            • IStateBase

                            Terry Norton

                            Terry Norton was born and raised in California. During the Vietnam era, he served six and half years in the US Air Force. While in the military, he was trained in electronics for electronic counter-measures. Upon discharge, he earned his Electrical Engineering degree, and later working for Joslyn Defense Systems in Vermont, designing and developing test equipment for the US Navy. When personal computers came on the scene, he took an interest in building computers, but never quite delved deep into the programming side. It wasn't until 2004 that programming peaked his interest. He began writing articles for OS/2 Magazine to teach C++ programming. Unfortunately, damaging his left hand in a snowblower accident in 2005 ended his writing for a couple years. IBM abandoned OS/2, so Terry bought his first Apple computer in early 2006. He tried a few times to learn Objective-C, but work and family always seemed to sidetrack his efforts. It wasn't until about 2010 when he discovered Unity and the need to write scripts, that he finally made some progress into the programming world. He began writing an online tutorial for UnityScript titled UnityScript for Noobs. It was a basic tutorial for beginners made available just before Unite 2011. Since then, Terry has been learning C# for writing scripts for Unity. Packt Publishing noticed UnityScript for Noobs and asked if he would be interested in writing a book about learning UnityScript. He declined. He felt that C# was a better language, and his heart just wasn't into UnityScript any longer. Two weeks later, Packt offered him the opportunity to write a book about learning C# for Unity. He jumped on it.
                            Sorry, we don't have any reviews for this title yet.

                            Code Downloads

                            Download the code and support files for this book.


                            Submit Errata

                            Please let us know if you have found any errors not listed on this list by completing our errata submission form. Our editors will check them and add them to this list. Thank you.


                            Errata

                            - 9 submitted: last submission 26 May 2014

                            Errata Category: Typo  Chapter Number: 5 Page Number: 77

                            Section name is:

                            Looping through lists to make decisions

                            instead of

                            Looping though lists to make decisions 

                            Errata Category: Typo  Chapter Number: 9 Page Number: 176

                            Section name: Displaying the splash screens

                            Let us have a look at the code in the following screenshot:
                            instead of
                            Let us have a look a the code in the following screenshot:

                            Errata type: Technical | Page no: 67

                            The given sentence is : hingeJoint is the name of the array being created

                            This should be:

                            hingeJoints is the name of the array being created

                             

                            Errata type: Grammar | Page no: 135

                            Using an inherited method is optional which is not what need what.

                            should be

                            Using an inherited method is optional which is not what we want.

                            Errata type: Grammar | Page no: 151

                            The 60 mean: from the last pixel on the right, come back 60 pixels to the left, and that's where to start drawing the rectangle box.

                            should be 

                            The 60 means from the last pixel on the right, come back 60 pixels to the left, and that's where to start drawing the rectangle box.

                             

                            Errata type: Grammar | Page no: 16

                            Q3. The Scripting Reference is a large document. How much it should you know before attempting to write any scripts? 

                            should be

                            Q3. The Scripting Reference is a large document. How much of it should you know before attempting to write any scripts? 

                             

                            Errata type: TYPO | Page no: 67

                            GameObject is the variable that stores the GameObject this script is attached to

                            should be

                            gameObject is the variable that stores the GameObject this script is attached to

                            Errata type: TYPO | Page no: 70

                            The code on line 8 is as fololws:

                            should be

                            The code on line 8 is as follows:

                            On page 127, the “Have a go hero” section needs to be ignored as this will cause code errors if the reader adds the code suggested.

                            Sample chapters

                            You can view our sample chapters and prefaces of this title on PacktLib or download sample chapters in PDF format.

                            Frequently bought together

                            Learning C# by Developing Games with Unity 3D Beginner's Guide +    Oracle E-Business Suite R12 Core Development and Extension Cookbook =
                            50% Off
                            the second eBook
                            Price for both: $45.60

                            Buy both these recommended eBooks together and get 50% off the cheapest eBook.

                            What you will learn from this book

                            • Understand what a variable is and how it works
                            • Learn about methods and functions is and how they are used to manipulate information
                            • Learn the concept of an object, a component of a GameObject, and the class they come from
                            • Learn about communication between objects using Dot Syntax
                            • Understand how to make decisions in code
                            • Learn how to use a State Machine to control and organize a Unity project
                            • Master the Scripting Reference to bring GameObjects to life
                            • Learn how to use the Unity Physics engine for moving and detecting GameObject collisions and triggers
                            • Display information on the game screen

                            In Detail

                            For the absolute beginner to any concept of programming, writing a script can appear to be an impossible hurdle to overcome. The truth is, there are only three simple concepts to understand: 1) having some type of information; 2) using the information; and 3) communicating the information. Each of these concepts is very simple and extremely important. These three concepts are combined to access the feature set provided by Unity.

                            "Learning C# by Developing Games with Unity 3D Beginner's Guide" assumes that you know nothing about programming concepts. First you will learn the absolute basics of programming using everyday examples that you already know. As you progress through the book, you will find that C# is not a foreign language after all, because you already know the words. With a few keywords and using substitution, before you know it, you'll be thinking in code.

                            The book starts by explaining in simple terms the three concepts you need for writing C# code and scripts: 1) variables to hold information; 2) methods (functions) to use the information; and 3) Dot Syntax to communicate the information where it's needed. The book builds on these concepts to open up the world of C# coding and Unity scripting. You will use this new power to access the features provided in Unity's Scripting Reference.

                            The first half of this book is devoted to the code writing beginner. The concepts of variables, methods, Dot Syntax, and decision processing are fully explained. Since C# is an actual programming language, we take advantage of this to develop a State Machine to help control and organize each phase of a Unity project. Once the basic programming concepts are established and we have some State Machine organization, the features and power of Unity are accessed using the Scripting Reference.

                            The goal of "Learning C# by Developing Games with Unity 3D Beginner's Guide" is to teach to you how to use the Unity Scripting Reference.

                            Approach

                            This book uses the learning-by-example approach. It takes simple examples from games to introduce all the main concepts of programming in an easy-to-digest and immediately recognizable way.

                            Who this book is for

                            This book is for the total beginner to any type of programming, focusing on the writing of C# code and scripts only. There are many parts that make up the Unity game engine. It is assumed that the reader already knows their way around Unity's user interface. The code editor used in this book is the MonoDevelop editor supplied by Unity.

                            Code Download and Errata
                            Packt Anytime, Anywhere
                            Register Books
                            Print Upgrades
                            eBook Downloads
                            Video Support
                            Contact Us
                            Awards Voting Nominations Previous Winners
                            Judges Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software
                            Resources
                            Open Source CMS Hall Of Fame CMS Most Promising Open Source Project Open Source E-Commerce Applications Open Source JavaScript Library Open Source Graphics Software