书籍简介:
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.This book is for the total beginner to any type of programming, focusing on the writing of C# code and *s 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.
作者简介:
Terry Norton
出版时间:
2013-09-25
章节目录:
Learning C# by Developing Games with Unity 3D Beginner's Guide
Table of Contents
Learning C# by Developing Games with Unity 3D Beginner's Guide
Credits
About the Author
About the Reviewers
www.PacktPub.com
Support files, eBooks, discount offers and more
Why Subscribe?
Free Access for Packt account holders
Preface
What this book covers
What you need for this book
Who this book is for
Conventions
Time for action – heading
What just happened?
Pop quiz – heading
Have a go hero – heading
Reader feedback
Customer support
Downloading the example code
Errata
Piracy
Questions
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 acion – opening the Reference Manual documentation for the transform Component
What just happened?
Time for ction – opening the scripting reference documentation for the transformcomponent
What just happened?
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
What just happened?
Introducing the MonoDevelop code editor
Syncing C# files between MonoDevelop and Unity
Time for action – opening LearningScript in MonoDevelop
What just happened?
Watching for a possible "gotcha" when creating script files in Unity
Fixing sync if it isn't working properly
Pop quiz – dealing with scripts
Summary
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
What just happened?
Time for action – changing the number 9 to a different number
What just happened?
Have a go hero – changing the value of myNumber
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
What just happened?
Have a go hero – changing the output of the method
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
Have a go hero – finding Start and Update in the Scripting Reference
Components communicating using the Dot Syntax
What's with the dots?
Pop quiz – knowing the C# building blocks
Summary
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
What just happened?
Naming your variables properly
Begin variable names with lowercase
Using multi-word variable names
Have a go hero – viewing multi-word variables in the Inspector panel
Declaring a variable and its type
The most common built-in variable types
Time for action – assigning values while declaring the variable
What just happened?
Where you declare a variable is important
Variable scope – determining where a variable can be used
Pop quiz – knowing how to declare a variable
Summary
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
What just happened?
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()
What just happened?
Have a go hero – add two more numbers together
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…
Pop quiz – understanding method operation
Summary
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
What just happened?
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
What just happened?
Have a go hero – change the value assigned to temperature
Have a go hero – change theBearMadeBigPottyInTheWoods to false
Using an if-else statement to execute alternate code
Time for action – add "else" to the if statement
What just happened?
Pop quiz – understanding if statements
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
What just happened?
Have a go hero – add another pony to the List
Storing items in a Dictionary
Time for action – create a dictionary of pony names and keys
What just happened?
Using a Collection Initializer to add items to a List or Dictionary
Time for action – adding ponies using a Collection Initializer
What just happened?
Pop quiz – understanding an array and a List
Looping though lists to make decisions
Using the foreach loop
Time for action – using foreach loops to retrieve data
What just happened?
Using the for loop
Time for action – selecting a pony from a List using a for loop
What just happened?
Using the while loop
Time for action – finding data and breakout of the while loop
What just happened?
Have a go hero – changing the pony name being searched
Summary
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
What just happened?
Accessing another Component on the current GameObject
Time for action – communicating with another Component on the Main Camera
What just happened?
Accessing other GameObjects and their Components
Time for action – creating two GameObjects and a new script
What just happened?
On LearningScript:
Have a go hero – creating and using a new variable named capsuleComp
Accessing GameObjects using drag-and-drop versus writing code
Time for action – trying drag-and-drop to assign a GameObject
What just happened?
Pop quiz – understanding communication between objects
Summary
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
What just happened?
Time for action – instantiating the BeginState class
What just happened?
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
What just happened?
Have a go hero – adding another method to the interface
Pop quiz – using a State Machine for game control
Summary
Developing the State Machine
Creating four State classes
Time for action – modifying BeginState and add three more States
What just happened?
Setting up the StateManager controller
Studying an example of inheritance
Enter the IStateBase interface again
Time for action – modify StateManager
What just happened?
Summarizing the code flow
Adding another State
Time for action – modifying PlayState to add another State
What just happened?
Adding OnGUI to the StateManager class
Time for action – adding OnGUI to StateManager
What just happened?
Changing the active State and controlling the Scene
Time for action – adding GameObjects and a button to the Scene
What just happened?
Pausing the game Scene
Time for action – adding code to pause the game Scene
What just happened?Changing the State using a timer
Time for action – creating a timer in BeginState
What just happened?
Have a go hero – changing the State switching order
Changing Scenes
Time for action – setting up another Scene
What just happened?
Changing Scenes destroys the existing GameObjects
Keeping GameManager between scenes
Time for action – adding the Awake method to StateManager
What just happened?
Changing the Scenes
Time for action – adding the code to change the Scenes
What just happened?
Pop quiz – understanding State Machine operation
Verifying the code of your classes
Summary
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
In Unity
In MonoDevelop
What just happened?
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
What just happened?
Storing game data in its own script
Time for action – creating a GameData script
What just happened?
Understanding images as textures in Unity
Using splash screens between game levels
Displaying the splash screens
Have a go hero – adjusting the button size and placement
Controlling the Player GameObject
Time for action – rotating Player in SetupState
What just happened?
Adding the Player Color option
Time for action – changing the color using GUI buttons
What just happened?
Adding the Lives option for Player
Time for action – setting the Lives for Player
What just happened?
Have a go hero – changing the setup spin speed
Pop quiz – understanding GameObjects
Summary
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
What just happened?
Attaching scripts to the new cameras
Time for actioning – attach the LookAtPlayer camera script
What just happened?
Time for action – attaching the FollowingPlayer camera script
What just happened?
Moving the Player using Rigidbody physics
Time for action – adding a Rigidbody to the Player
What just happened?
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
What just happened?
Scoring for the win
Losing when Player crashes
Shooting projectiles at the orbs
Time for action – creating the EnergyPulse prefab
What just happened?
Shooting a single-shot EnergyPulse
Shooting rapid-fire EnergyPulses
The EnergyPulse is fired
Controlling EnergyPulse objects
Have a go hero – analyzing the code for the second level of play
Pop quiz – knowing which Unity methods to call
Summary
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
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
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
C. Pop Quiz Answers
Chapter 1, Discovering Your Hidden Scripting Skills
Pop quiz – dealing with scripts
Chapter 2, Introducing the Building Blocks for Unity Scripts
Pop quiz – knowing C# building blocks
Chapter 3, Getting into the Details of Variables
Pop quiz – knowing how to declare a variable
Chapter 4, Getting into the Details of Methods
Pop quiz – understanding method operation
Chapter 5, Making Decisions in Code
Pop quiz – understanding if statements
Pop quiz – understanding an array and a List
Chapter 6, Using Dot Syntax for Object Communication
Pop quiz – understanding communication between objects
Chapter 7, Creating the Gameplay is Just a Part of the Game
Pop quiz – using a State Machine for game control
Chapter 8, Developing the State Machine
Pop quiz – understanding State Machine operation
Chapter 9, Start Building a Game and Get the Basic Structure Running
Pop quiz – understanding GameObjects
Chapter 10, Moving Around, Collisions, and Keeping Score
Pop quiz – knowing which Unity methods to call
Index
封面图: