You are previewing Learning C# by Developing Games with Unity 3D Beginner's Guide.
O'Reilly logo
Learning C# by Developing Games with Unity 3D Beginner's Guide

Book Description

The beauty of this book is that it assumes absolutely no knowledge of coding at all. Starting from very first principles it will end up giving you an excellent grounding in the writing of C# code and scripts.

  • 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

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

    Table of Contents

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