Pro Unity Game Development with CSharp - A.Thorn.pdf

(13783 KB) Pobierz
TeCHnoloGY In ACTIon
Game
Development
with
C#
Refine youR unity development skills
while building a cRoss-platfoRm
fiRst-peRson shooteR with c# and unity
Pro
Unity
Alan Thorn
For your convenience Apress has placed some of the front
matter material after the index. Please use the Bookmarks
and Contents at a Glance links to access them.
Contents at a Glance
About the Author �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½xi
About the Technical Reviewer �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½xiii
Acknowledgments �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xv
Introduction �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½
xvii
Chapter 1: Designing and Preparing�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½1
Chapter 2: Getting Started �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½37
Chapter 3: Event Handling �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½81
Chapter 4: Power-Ups and Singletons �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½113
Chapter 5: Player Controller �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½147
Chapter 6: Weapons�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½179
Chapter 7: Enemies�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½217
Chapter 8: Graphical User Interfaces �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½255
Chapter 9: Handling Persistent Data �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½281
Chapter 10: Refinements and Improvements �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½303
Index �½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½�½319
iii
Introduction
One thing I really love about the games industry today is its “openness” compared to many
industries. To start out in the games industry you don’t need a degree. All you need is a computer.
And an Internet connection. If you log on online right now, you can immediately visit web sites
such as Blender, GIMP, Inkscape, and (of course) Unity to get access to professional-grade game
development software completely free of charge! The result is that almost anybody in any place on
any budget, from any background and at any age, can sit at a computer and be a game developer
right now.
Of course, none of that openness
guarantees
you’ll automatically
know
how to use the tools,
or that you’ll even like the results you get from them. You need to bring a certain
something,
an
understanding, to the tools to fully realize their power and potential in practice. To do that, you’ll
need to develop experience and to refine your knowledge, and to learn techniques and workflows
using real-world examples, targeting your software of choice directly.
This book focuses on Unity development specifically. And when it comes to Unity development,
there’s one area where developers feel troubled or somewhat lacking in power. That area is in
C# scripting. The general feel is that so much of the C# tutorials and guides out there today are
so abstract and formal that it’s difficult to see how all of it should come together and be applied
properly in real-world cases to do what you need to do to give your games that professional edge.
You already know about variables, functions, loops, enumerations, and the fundamentals. But
what you need is something to take you further, to help you see how all these core features can be
combined in creative ways to produce a real-world, working game. The kind of game you can not
only play but study and see how it all comes together. This book aims to fill that need so you can
become a more powerful game developer.
What Is This Book About?
This book will show you how to create a small but complete first-person shooter game in the Unity
engine, step by step. In particular it’ll focus strongly on C# scripting, and on a range of related
ideas and techniques, for getting professional-grade results. We’ll explore a lot of ground, including
level design, vectors and mathematics, line-of-sight calculations, pathfinding and navigation,
xvii
xviii
Introduction
artificial intelligence, state machines, weapon creation, trajectories and paths, and load-and-save
functionality, as well as a lot more! We’ll see things not just from a more abstract and theoretical
standpoint, but we’ll see how theory is applied in real-world cases to get work done, gradually
piecing together a complete game we can play and enjoy, and also extend upon and improve. The
aim is to show you some real-world applications of C# scripting that you can take away to use on
your own projects, achieving your creative vision more easily and effectively.
There are, of course, many things we won’t cover here. Specifically, we’ll be focusing only on C#
scripting, and not on other languages such as JavaScript or Boo. That decision should not be taken
as a negative judgment of those languages. Indeed, all of them are powerful and versatile in their
own ways. But it simply reflects what most people are seeking (as I see it) when they approach Unity
seeking to extend their scripting skills.
Additionally, we won’t be covering C# basics, such as variables, functions, and loops. I’ll assume
you already know that stuff. Further, although we’ll cover
some
level and game-design issues as
we start out with our game project in Chapters 1 and 2, we won’t be going too far in depth on that
subject, as it’s covered amply elsewhere and because the main focus here is on C# and scripting
specifically.
And finally, we won’t be covering every aspect of the C# language or every possible way it can be
used. This is for the simple reason that no book could hope to do that, just as no English dictionary
can tell you about every possible combination of words or every possible application of them. This
book covers a specific set of C# features in a specific set of ways. The idea is that by showing you
specific cases and specific applications, you can see how the general techniques apply to your own
games.
Who Is This Book For?
Every technical book is written with a target audience in mind. That is, it’s written for a specific
“type” of reader. This means that when writing the book, I, the author, must make assumptions about
you, the reader. The assumptions are about the book-relevant topics I think you’ll already know,
before even starting to read this book. Specifically, I’ll assume you know the following:
n
n
How to use the Unity Editor to import assets and build levels
n
n
How to create script files and write some basic code in C#
n
n
How to use fundamental programming concepts like variables, functions, loops,
and conditional statements
n
n
How to debug games using the Unity debugging tools
However, this book may not be for you if you’re completely new to game development, or if this is
your first time using Unity, or if you’re completely new to programming specifically. In any of these
cases, I strongly recommend your picking up an introductory title before continuing with this book,
to ensure you get the most from it.
Zgłoś jeśli naruszono regulamin