(Download PDF) Learning C Programming With Unity 3D Alex Okita Online Ebook All Chapter PDF

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 29

Learning C# programming with Unity

3D Alex Okita
Visit to download the full and correct content document:
https://textbookfull.com/product/learning-c-programming-with-unity-3d-alex-okita/
More products digital (pdf, epub, mobi) instant
download maybe you interests ...

C# Game Programming Cookbook for Unity 3D 2nd Edition


Jeff W. Murray

https://textbookfull.com/product/c-game-programming-cookbook-for-
unity-3d-2nd-edition-jeff-w-murray/

Learning C 7 By Developing Games with Unity 2017 Learn


C Programming by building fun and interactive games
with Unity Third Edition Micael Dagraca

https://textbookfull.com/product/learning-c-7-by-developing-
games-with-unity-2017-learn-c-programming-by-building-fun-and-
interactive-games-with-unity-third-edition-micael-dagraca/

Learning C by developing games with Unity 2020 An


enjoyable and intuitive approach to getting started
with C programming and Unity Fifth Edition Harrison
Ferrone
https://textbookfull.com/product/learning-c-by-developing-games-
with-unity-2020-an-enjoyable-and-intuitive-approach-to-getting-
started-with-c-programming-and-unity-fifth-edition-harrison-
ferrone/

Beginning Game AI with Unity: Programming Artificial


Intelligence with C# Sebastiano M. Cossu

https://textbookfull.com/product/beginning-game-ai-with-unity-
programming-artificial-intelligence-with-c-sebastiano-m-cossu/
Developing 2D Games with Unity: Independent Game
Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern/

Developing 2D Games with Unity: Independent Game


Programming with C# 1st Edition Jared Halpern

https://textbookfull.com/product/developing-2d-games-with-unity-
independent-game-programming-with-c-1st-edition-jared-halpern-2/

Game Programming in C Creating 3D Games Creating 3D


Games Sanjay Madhav

https://textbookfull.com/product/game-programming-in-c-
creating-3d-games-creating-3d-games-sanjay-madhav/

Getting Started with Unity 2018 A Beginner s Guide to


2D and 3D Game Development with Unity 3rd Edition
Edward Lavieri

https://textbookfull.com/product/getting-started-with-
unity-2018-a-beginner-s-guide-to-2d-and-3d-game-development-with-
unity-3rd-edition-edward-lavieri/

Deep Reinforcement Learning in Unity: With Unity ML


Toolkit 1st Edition Abhilash Majumder

https://textbookfull.com/product/deep-reinforcement-learning-in-
unity-with-unity-ml-toolkit-1st-edition-abhilash-majumder/
Learning C# Programming
with Unity 3D
Learning C# Programming
with Unity 3D
Second Edition

Alex Okita
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742

© 2020 by Taylor & Francis Group, LLC


CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed on acid-free paper

International Standard Book Number-13: 978-1-138-33681-0 (paperback)


978-1-138-33682-7 (hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable efforts have been made
to publish reliable data and information, but the author and publisher cannot assume responsibility for the validity of all
materials or the consequences of their use. The authors and publishers have attempted to trace the copyright holders of all
material reproduced in this publication and apologize to copyright holders if permission to publish in this form has not been
obtained. If any copyright material has not been acknowledged please write and let us know so we may rectify in any future
reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced, transmitted, or utilized
in any form by any electronic, mechanical, or other means, now known or hereafter invented, including photocopying,
microfilming, and recording, or in any information storage or retrieval system, without written permission from the
publishers.

For permission to photocopy or use material electronically from this work, please access www.copyright.com (http://www.
copyright.com/) or contact the Copyright Clearance Center, Inc. (CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400.
CCC is a not-for-profit organization that provides licenses and registration for a variety of users. For organizations that have been
granted a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and are used only for
identification and explanation without intent to infringe.

Library of Congress Cataloging‑in‑Publication Data

Names: Okita, Alex, author.


Title: Learning C# programming with Unity 3D / Alex Okita.
Description: Second edition. | Boca Raton, FL : CRC Press/Taylor & Francis
Group, 2019.
Identifiers: LCCN 2019010020 | ISBN 9781138336810 (pbk. : acid-free paper) |
ISBN 9781138336827 (hardback : acid-free paper)
Subjects: LCSH: Computer games--Programming. | C# (Computer program language)
| Unity (Electronic resource) | Three-dimensional display systems.
Classification: LCC QA76.76.C672 O43 2019 | DDC 794.8/1526--dc23
LC record available at https://lccn.loc.gov/2019010020

Visit the Taylor & Francis Web site at


http://www.taylorandfrancis.com

and the CRC Press Web site at


http://www.crcpress.com
Contents

1. Introduction: What This Book Is About ........................................................................................ 1


1.1 Why Read a Book: Why This Book May or May Not Be for You ......................................... 1
1.1.1 Do I Need to Know Math? ........................................................................................ 2
1.1.2 Programming as a Form of Expression .................................................................... 2
1.1.3 Games as a Stage with Lights ................................................................................... 2
1.2 Personal Information .............................................................................................................. 2
1.3 A Brief History of Computer Programming: How Programming Came to Be ..................... 3
1.3.1 Mechanical Computers ............................................................................................. 3
1.3.2 Logic ......................................................................................................................... 3
1.3.3 Computer Science ..................................................................................................... 3
1.3.4 The Origin of Software ............................................................................................. 4
1.3.5 Modern Computer Language .................................................................................... 4
1.3.6 The Future of Computer Languages ......................................................................... 4
1.4 C#: A Flexible Programming Language ................................................................................ 4
1.4.1 C# Paradigm ............................................................................................................. 5
1.4.2 Unity 3D: A Brief History of Game Engines ........................................................... 5
1.4.3 Why Use Unity 3D to Learn? ................................................................................... 6
1.4.4 How Does Unity 3D Use C#? ................................................................................... 6
1.4.4.1 How to Tell Unity 3D What to Do............................................................ 7
1.5 What Is Programming? ........................................................................................................... 7
1.5.1 What Does C# Look Like? ....................................................................................... 8
1.5.2 Learning to Copy and Paste ...................................................................................... 9
1.5.3 Iteration ..................................................................................................................... 9
1.6 Compiling: Turning Words into Computer Instruction .......................................................... 9
1.7 What We’ve Learned ............................................................................................................ 10
1.8 Leveling Up: The Path Ahead .............................................................................................. 10

2. Before You Begin .............................................................................................................................11


2.1 What Will Be Covered in This Chapter ................................................................................11
2.2 Unity 3D Installation ............................................................................................................ 13
2.2.1 Visual Studio Installation ....................................................................................... 15
2.2.2 Unity Version Numbering ........................................................................................16
2.3 Git SCM Installation ..............................................................................................................17
2.3.1 Git-SCM.com Git Software (Optional) ....................................................................17
2.3.2 GitHub.com Sign Up! (Also Optional) ....................................................................18
2.3.3 Git: The Basics ........................................................................................................ 20
2.3.4 Your First Repo ....................................................................................................... 22
2.3.5 Before Moving On .................................................................................................. 25
2.4 Unity General Overview ....................................................................................................... 26
2.4.1 The Main Window .................................................................................................. 27
2.4.1.1 Toolbar .................................................................................................... 27
2.4.1.2 Scene, Game View, and Asset Store....................................................... 28
2.4.1.3 Hierarchy Panel ...................................................................................... 28

v
vi Contents

2.4.1.4 Inspector Panel ....................................................................................... 29


2.4.1.5 Project and Console Panel ...................................................................... 29
2.4.1.6 Assets Directory ......................................................................................31
2.4.1.7 Project Settings Directory ...................................................................... 32
2.4.1.8 Packages Directory ................................................................................. 32
2.4.2 Git Ignore ................................................................................................................ 32
2.4.3 What We’ve Learned .............................................................................................. 32
2.5 Sample Code ......................................................................................................................... 33
2.5.1 Code Fragments ...................................................................................................... 33
2.5.2 Are Errors Bad? ...................................................................................................... 34
2.5.2.1 Compile-Time and Run-Time Errors...................................................... 34
2.5.3 How to Follow Along .............................................................................................. 35
2.5.4 Comments: A First Look ........................................................................................ 36
2.5.5 What We’ve Learned .............................................................................................. 37
2.6 Working with C#: Game Engines ......................................................................................... 37
2.6.1 Creating and Assigning a New C# File: Now It’s Your Turn ................................. 40
2.6.2 Naming Your New File ........................................................................................... 42
2.6.3 Using Your New File............................................................................................... 43
2.6.4 Additional Unity 3D Tools ...................................................................................... 48
2.6.5 Working in a Live Scene ......................................................................................... 50
2.6.6 Saving a Scene ........................................................................................................ 50
2.6.7 Opening a Scene ..................................................................................................... 50
2.6.8 What We’ve Learned ...............................................................................................51
2.7 Leveling Up: The Journey Has Just Begun............................................................................51

3. First Steps: Just Getting Started .................................................................................................. 53


3.1 What Will Be Covered in This Chapter ............................................................................... 53
3.2 Review................................................................................................................................... 54
3.2.1 Submitting Changes to Git ...................................................................................... 54
3.3 Let’s Build a Game—Part 1 ................................................................................................. 56
3.3.1 Movement Experiments .......................................................................................... 57
3.3.2 Reading Programmer Documentation .................................................................... 58
3.4 Tokens ....................................................................................................................................61
3.4.1 Writing C#............................................................................................................... 62
3.4.2 Comments a First Look........................................................................................... 63
3.4.3 Separator Tokens ..................................................................................................... 64
3.4.4 Operator Tokens ...................................................................................................... 65
3.4.5 Literals .................................................................................................................... 66
3.4.6 Transitive and Non-Transitive Operations .............................................................. 67
3.4.7 Putting It All Together ............................................................................................ 67
3.4.8 What We’ve Learned .............................................................................................. 69
3.5 Statements and Expressions .................................................................................................. 69
3.5.1 Expressions ............................................................................................................. 70
3.5.2 How Unity 3D Executes Your Code ....................................................................... 70
3.5.3 Thinking in Algorithms .......................................................................................... 70
3.5.3.1 Wash, Rinse, Repeat ............................................................................... 73
3.5.4 What We’ve Learned .............................................................................................. 73
3.6 Keywords .............................................................................................................................. 73
3.6.1 The Class Keyword ................................................................................................. 74
3.6.2 What We’ve Learned .............................................................................................. 76
3.7 White Space .......................................................................................................................... 76
3.7.1 Code Style ............................................................................................................... 78
3.7.2 What We’ve Learned .............................................................................................. 79
Contents vii

3.8 Code Blocks ........................................................................................................................ 79


3.8.1 What We’ve Learned .......................................................................................... 81
3.9 Classes: A First Look.......................................................................................................... 81
3.9.1 Objects ................................................................................................................ 82
3.9.2 What We’ve Learned .......................................................................................... 86
3.10 Variables ............................................................................................................................. 86
3.10.1 Identifiers ............................................................................................................ 87
3.10.2 Data ..................................................................................................................... 88
3.10.3 Variable Manipulation ........................................................................................ 89
3.10.3.1 Declaration Placement Examples ..................................................... 90
3.10.4 Dynamic Initialization ........................................................................................ 90
3.10.5 What We’ve Learned .......................................................................................... 91
3.11 Variable Names ................................................................................................................... 91
3.11.1 Useful Variables in Unity 3D .............................................................................. 94
3.11.2 Variable Assignment ........................................................................................... 96
3.11.3 Putting It Together: Using the Variables............................................................. 98
3.11.4 What We’ve Learned ........................................................................................ 100
3.12 Types: A First Look .......................................................................................................... 100
3.12.1 Value and Reference Types ............................................................................... 100
3.12.2 What We’ve Learned .........................................................................................101
3.13 Strong Typing.................................................................................................................... 102
3.13.1 Dynamic Typing ............................................................................................... 102
3.13.2 What We’ve Learned ........................................................................................ 103
3.14 Type Casting, Numbers .................................................................................................... 103
3.14.1 Explicit versus Implicit Casting ........................................................................ 104
3.14.1.1 A Basic Example ............................................................................. 105
3.14.2 What We’ve Learned ........................................................................................ 107
3.15 Comments ......................................................................................................................... 108
3.15.1 Line Numbers.....................................................................................................111
3.15.2 Code Folding ......................................................................................................111
3.15.3 Summary Comments .........................................................................................113
3.15.4 Navigating in Code ............................................................................................114
3.15.5 What We’ve Learned .........................................................................................115
3.16 Leveling Up: Moving on to Basics ....................................................................................115
3.16.1 Git Revision History ..........................................................................................115

4. Basics: The Building Blocks of Code ...........................................................................................117


4.1 What Will Be Covered in This Chapter ............................................................................117
4.2 Review ...............................................................................................................................117
4.3 Building a Game—Part 1 ..................................................................................................118
4.3.1 Mouse Point .......................................................................................................119
4.3.2 Move to Point .................................................................................................... 120
4.3.3 What We’ve Learned .........................................................................................121
4.4 Class Construction .............................................................................................................121
4.4.1 Class Declaration .............................................................................................. 123
4.4.1.1 A Basic Example ............................................................................. 123
4.4.1.2 Value and Reference Types ............................................................. 124
4.4.2 Adding Data Fields ........................................................................................... 125
4.4.3 Access Modifiers and the Dot Operator ........................................................... 125
4.4.4 Class Scope ....................................................................................................... 127
4.4.5 Class Members .................................................................................................. 128
4.4.6 What We’ve Learned ........................................................................................ 128
viii Contents

4.5 Directives .......................................................................................................................... 129


4.5.1 Libraries.......................................................................................................... 130
4.5.2 Ambiguous NameSpaces .................................................................................133
4.5.3 What We’ve Learned ...................................................................................... 134
4.6 Functions........................................................................................................................... 134
4.6.1 What Are Functions? .......................................................................................135
4.6.2 Unity 3D Entry Points .................................................................................... 136
4.6.3 Writing a Function .......................................................................................... 137
4.6.4 More on White Space and Tabs ...................................................................... 139
4.6.5 What We’ve Learned ...................................................................................... 140
4.7 Order of Operation: What Is Calculated and When ......................................................... 140
4.7.1 Evaluating Numbers ........................................................................................141
4.7.1.1 Math ...............................................................................................141
4.7.1.2 Operator Evaluation...................................................................... 144
4.7.2 What We’ve Learned .......................................................................................145
4.8 Scope: A First Look .......................................................................................................... 146
4.8.1 Class Scope ..................................................................................................... 146
4.8.2 Function Scope ................................................................................................149
4.8.3 Blank Scope .....................................................................................................151
4.8.4 What We’ve Learned .......................................................................................152
4.9 This ....................................................................................................................................152
4.9.1 A Basic Example .............................................................................................152
4.9.2 When This Is Necessary..................................................................................153
4.9.3 What We’ve Learned ...................................................................................... 154
4.10 Building a Game—Part 2 ..................................................................................................155
4.10.1 Additional Components ...................................................................................155
4.10.2 The Projectile ................................................................................................. 158
4.10.3 The Target ....................................................................................................... 160
4.10.4 What We’ve Learned .......................................................................................161
4.11 Logic and Operators ..........................................................................................................162
4.11.1 Booleans ..........................................................................................................162
4.11.2 Equality Operators ..........................................................................................163
4.11.3 Logical Not! .....................................................................................................165
4.11.4 Greater and Less than Operators .................................................................... 166
4.11.5 Logical And and Or Operators ........................................................................168
4.11.6 If and Branching ..............................................................................................169
4.11.7 What We’ve Learned .......................................................................................173
4.12 Loops .................................................................................................................................173
4.12.1 Unary Operators ..............................................................................................173
4.12.2 While ...............................................................................................................174
4.12.3 For ....................................................................................................................175
4.12.4 Do-While .........................................................................................................177
4.12.5 Postfix and Prefix Notation..............................................................................178
4.12.6 Using Loops .....................................................................................................179
4.12.7 Loops within Loops.........................................................................................182
4.12.8 Runaway Loops ...............................................................................................183
4.12.9 Breakpoints: A First Look ...............................................................................185
4.12.10 What We’ve Learned .......................................................................................186
4.13 ScopeAgain ........................................................................................................................187
4.13.1 Visibility or Accessibility ................................................................................187
4.13.1.1 A Basic Example ...........................................................................187
4.13.2 Global Scope....................................................................................................188
4.13.3 What We’ve Learned ...................................................................................... 190
Contents ix

4.14 Warnings versus Errors .................................................................................................... 190


4.14.1 Warnings ........................................................................................................... 190
4.14.2 Errors .................................................................................................................191
4.14.3 Understanding the Debugger ............................................................................ 192
4.14.4 What We’ve Learned ........................................................................................ 194
4.15 Leveling Up: Fundamentals .............................................................................................. 194
4.15.1 Style Guides ...................................................................................................... 194

5. Fundamentals: Building a Foundation ...................................................................................... 195


5.1 What Will Be Covered in This Chapter ........................................................................... 195
5.2 Review .............................................................................................................................. 196
5.2.1 Modular Code ................................................................................................... 199
5.3 Inheritance: A First Look ................................................................................................. 202
5.3.1 Class Members .................................................................................................. 204
5.3.1.1 Inheriting Members .......................................................................... 205
5.3.1.2 Is-A and Has-A ................................................................................. 206
5.3.2 Instancing .......................................................................................................... 206
5.3.3 Parent Child ...................................................................................................... 208
5.3.4 Object ................................................................................................................ 209
5.3.4.1 A Type Is Not an Object ....................................................................212
5.3.5 != null ................................................................................................................213
5.3.6 What We’ve Learned .........................................................................................214
5.4 Instancing...........................................................................................................................214
5.4.1 Class Initialization .............................................................................................215
5.4.2 New ....................................................................................................................215
5.4.3 Constructors .......................................................................................................216
5.4.4 What We’ve Learned .........................................................................................219
5.5 Static ................................................................................................................................. 220
5.5.1 A Basic Example ............................................................................................... 220
5.5.2 Static Variables ................................................................................................. 222
5.5.2.1 A Basic Example............................................................................... 222
5.5.3 Static Functions ................................................................................................. 224
5.5.4 Putting It All Together ...................................................................................... 225
5.5.5 What We’ve Learned ........................................................................................ 228
5.6 Building A Game—Part 3 ................................................................................................ 228
5.6.1 The Player ......................................................................................................... 228
5.6.2 The Mover ......................................................................................................... 229
5.6.3 The Shooter ....................................................................................................... 230
5.6.4 Physics Settings................................................................................................. 232
5.6.5 What We’ve Learned ........................................................................................ 233
5.7 Jump Statements and Return ............................................................................................ 233
5.7.1 Return................................................................................................................ 234
5.7.1.1 A Basic Example............................................................................... 235
5.7.2 Returning Objects ............................................................................................. 235
5.7.3 A Class Is a Type .............................................................................................. 236
5.7.4 Tuples ................................................................................................................ 238
5.7.5 Null Is Not Void ................................................................................................ 240
5.7.6 What We’ve Learned ........................................................................................ 240
5.8 Arrays: A First Look......................................................................................................... 241
5.8.1 Fixed-Sized Arrays ........................................................................................... 241
5.8.1.1 A Basic Example............................................................................... 241
5.8.2 Foreach .............................................................................................................. 246
5.8.2.1 A Basic Example............................................................................... 246
x Contents

5.8.3 Dynamic Initialization ...................................................................................... 246


5.8.4 Using the While Loop with Arrays................................................................... 247
5.8.4.1 Setting Array Values ....................................................................... 247
5.8.4.2 Getting Array Values ...................................................................... 248
5.8.5 What We’ve Learned ........................................................................................ 248
5.9 Jump Statements: Break and Continue ............................................................................. 248
5.9.1 A Basic Example ............................................................................................... 249
5.9.1.1 Continue .......................................................................................... 249
5.9.2 ZombieData ....................................................................................................... 249
5.9.3 Foreach Again ....................................................................................................251
5.9.4 What We’ve Learned ........................................................................................ 252
5.10 Multidimensional Arrays .................................................................................................. 252
5.10.1 Columns and Rows ........................................................................................... 253
5.10.1.1 A Basic Example ............................................................................. 254
5.10.2 A Puzzle Board ................................................................................................. 255
5.10.3 Camera and Screen Setup ................................................................................. 257
5.10.4 Checking Range ................................................................................................ 259
5.10.5 What We’ve Learned .........................................................................................261
5.11 Array List .......................................................................................................................... 262
5.11.1 A Basic Example ............................................................................................... 263
5.11.2 ArrayList.Contains() ............................................................................. 265
5.11.3 Remove.............................................................................................................. 266
5.11.4 Sort and Reverse ............................................................................................... 267
5.11.5 What We’ve Learned ........................................................................................ 268
5.12 Strings ............................................................................................................................... 268
5.12.1 Declaring a String ............................................................................................. 268
5.12.1.1 A Basic Example ............................................................................. 269
5.12.2 Escape Sequences ............................................................................................. 270
5.12.3 Verbatim Strings: @.......................................................................................... 271
5.12.4 What We’ve Learned ........................................................................................ 272
5.13 Combining What We’ve Learned ..................................................................................... 272
5.13.1 Timers ............................................................................................................... 273
5.13.2 What We’ve Learned ........................................................................................ 278
5.14 Leveling Up: On to the Cool Stuff.................................................................................... 278

6. Intermediate: The Tricks of the Trade ...................................................................................... 281


6.1 What Will Be Covered in This Chapter ........................................................................... 281
6.2 Review .............................................................................................................................. 281
6.2.1 Class Members .................................................................................................. 282
6.2.1.1 A Basic Example ............................................................................. 282
6.2.1.2 Thinking Like a Programmer ......................................................... 283
6.2.2 Return................................................................................................................ 286
6.2.2.1 A Basic Example ............................................................................. 286
6.2.3 Arguments aka “Args” (Not Related to Pirates) ............................................... 287
6.2.3.1 The Basic Example ......................................................................... 287
6.2.3.2 Multiple Args .................................................................................. 288
6.2.3.3 Using Args ...................................................................................... 290
6.2.4 Assignment Operators....................................................................................... 292
6.2.4.1 A Basic Example ............................................................................. 292
6.2.5 What We’ve Learned ........................................................................................ 294
Contents xi

6.3 Building a Game—Part 4 ................................................................................................. 294


6.3.1 ChaseCamera .................................................................................................... 294
6.3.2 Tile Floor Generator ......................................................................................... 295
6.3.3 What We’ve Learned ........................................................................................ 297
6.4 Class Constructors ............................................................................................................ 298
6.4.1 A Basic Example ............................................................................................... 299
6.4.2 What We’ve Learned ........................................................................................ 301
6.5 Arrays Revisited ............................................................................................................... 302
6.5.1 Using Arrays in Unity 3D ................................................................................. 302
6.5.1.1 Starting with 0 .................................................................................. 304
6.5.1.2 Mathf................................................................................................. 306
6.5.1.3 Time .................................................................................................. 307
6.5.2 Instancing with AddComponent(); .............................................................. 307
6.5.3 Type Casting Unity 3D Objects ........................................................................ 309
6.5.4 What We’ve Learned .........................................................................................311
6.6 Enums ................................................................................................................................311
6.6.1 Using Enums ......................................................................................................312
6.6.2 Combining What We’ve Learned ......................................................................315
6.6.3 What We’ve Learned .........................................................................................317
6.7 Switch ................................................................................................................................317
6.7.1 A Basic Example ................................................................................................318
6.7.2 Default ................................................................................................................319
6.7.3 More On Cases .................................................................................................. 322
6.7.4 Fall Through ..................................................................................................... 326
6.7.5 goto Case ........................................................................................................... 327
6.7.6 Finite State Machine ......................................................................................... 328
6.7.7 What We’ve Learned ........................................................................................ 330
6.8 Structs ............................................................................................................................... 330
6.8.1 Building a Struct ................................................................................................331
6.8.2 Struct versus Class .............................................................................................332
6.8.3 Without Structs ................................................................................................. 334
6.8.4 Handling Structs ................................................................................................335
6.8.5 Accessing Structs .............................................................................................. 336
6.8.6 Global Access ................................................................................................... 337
6.8.7 What We’ve Learned ........................................................................................ 337
6.9 Class Data ......................................................................................................................... 338
6.9.1 Character Base Class ........................................................................................ 339
6.9.2 Const ................................................................................................................. 340
6.9.3 Readonly ............................................................................................................341
6.9.4 What We’ve Learned ........................................................................................ 342
6.10 Namespaces ...................................................................................................................... 342
6.10.1 A Basic Example ............................................................................................... 342
6.10.2 Directives in Namespaces ................................................................................. 344
6.10.3 Ambiguous References ..................................................................................... 345
6.10.4 Alias Directives................................................................................................. 345
6.10.5 What We’ve Learned ........................................................................................ 346
6.11 Functions Again ................................................................................................................ 346
6.11.1 Parameter Lists ................................................................................................. 346
6.11.2 Side Effects ....................................................................................................... 347
6.11.3 Multiple Arguments .......................................................................................... 348
xii Contents

6.11.4 Useful Parameters ............................................................................................. 348


6.11.4.1 The Rule of Three ........................................................................... 349
6.11.5 Foreach versus For ............................................................................................ 350
6.11.6 What We’ve Learned .........................................................................................351
6.12 Unity 3D Execution Order .................................................................................................351
6.12.1 A Basic Example ................................................................................................353
6.12.2 Component Execution Order ............................................................................ 356
6.12.3 What We’ve Learned ........................................................................................ 358
6.13 Inheritance Again ............................................................................................................. 358
6.13.1 Function Overrides ........................................................................................... 359
6.13.1.1 A Basic Example ............................................................................. 359
6.13.2 Class Inheritance............................................................................................... 362
6.13.2.1 Sharing Common Attributes ........................................................... 362
6.13.3 Object ................................................................................................................ 364
6.13.4 What We’ve Learned ........................................................................................ 367
6.14 Type Casting Again .......................................................................................................... 368
6.14.1 (<Type>) versus “as” ......................................................................................... 368
6.14.2 User-Defined Type Conversion ......................................................................... 370
6.14.3 Implicit versus Explicit Type Conversion ......................................................... 372
6.14.4 What We’ve Learned ........................................................................................ 373
6.15 Working with Vectors ........................................................................................................374
6.15.1 Vectors Are Objects ...........................................................................................374
6.15.2 Stepping through MonsterGenerator .................................................................375
6.15.3 Gizmos .............................................................................................................. 377
6.15.3.1 A Basic Example ............................................................................. 377
6.15.3.2 DrawRay.......................................................................................... 379
6.15.4 Using Gizmos.................................................................................................... 380
6.15.5 What We’ve Learned .........................................................................................381
6.16 goto Labels .........................................................................................................................381
6.16.1 Skipping Down ................................................................................................. 383
6.16.2 Zombie State Machine ...................................................................................... 383
6.16.3 This as a Reference to Yourself ........................................................................ 386
6.16.4 Just for Zombies ................................................................................................ 386
6.16.5 What We’ve Learned ........................................................................................ 388
6.17 More on Arrays ................................................................................................................. 389
6.17.1 Length and Count.............................................................................................. 389
6.17.1.1 A Basic Example ............................................................................. 389
6.17.2 Foreach: A Reminder ........................................................................................ 390
6.17.3 Discovery .......................................................................................................... 393
6.17.4 Putting It Together ............................................................................................ 394
6.17.4.1 Breakpoint, Observing Inheritance ................................................ 395
6.17.5 What We’ve Learned ........................................................................................ 399
6.18 Out Parameter ................................................................................................................... 399
6.18.1 A Basic Example ............................................................................................... 401
6.18.2 Simple Sort (Bubble Sort) ................................................................................. 403
6.18.3 Simple Sort Proof .............................................................................................. 405
6.18.4 What We’ve Learned ........................................................................................ 406
6.19 Ref Parameter ................................................................................................................... 407
6.19.1 A Basic Example ............................................................................................... 407
6.19.2 Code Portability Side Effects............................................................................ 408
6.19.3 What We’ve Learned .........................................................................................410
Contents xiii

6.20 Type Casting Numbers ......................................................................................................411


6.20.1 Number Types ....................................................................................................413
6.20.2 Integers ...............................................................................................................413
6.20.2.1 Signed Numbers ...............................................................................414
6.20.3 Floating Point .....................................................................................................414
6.20.4 What We’ve Learned .........................................................................................415
6.21 Types and Operators ..........................................................................................................415
6.21.1 GetType() .......................................................................................................416
6.21.2 More Type Casting .............................................................................................417
6.21.3 Type Aliasing .................................................................................................... 420
6.21.4 Boxing and Unboxing ....................................................................................... 420
6.22 Operator Overloading ....................................................................................................... 422
6.22.1 A Basic Example ............................................................................................... 423
6.22.2 Overloading* ..................................................................................................... 424
6.22.3 Overloading < and > ......................................................................................... 426
6.22.4 What We’ve Learned ........................................................................................ 427
6.23 Controlling Inheritance .................................................................................................... 427
6.23.1 Sealed ................................................................................................................ 428
6.23.1.1 A Basic Example ............................................................................. 428
6.23.2 Extension Functions .......................................................................................... 429
6.23.3 Abstract ............................................................................................................. 430
6.23.3.1 A Basic Example ............................................................................. 430
6.23.4 Abstract: Abstract ..............................................................................................432
6.23.5 Putting This to Use ........................................................................................... 434
6.23.6 What We’ve Learned .........................................................................................435
6.24 Building A Game—Part 5 ................................................................................................ 436
6.24.1 Generating Rooms ............................................................................................ 436
6.24.2 What We’ve Learned ........................................................................................ 439
6.25 Leveling Up ...................................................................................................................... 440

7. Advanced ....................................................................................................................................... 441


7.1 What Will Be Covered in This Chapter ........................................................................... 441
7.2 Review .............................................................................................................................. 442
7.2.1 Moving Forward ............................................................................................... 442
7.3 Visual Studio .................................................................................................................... 442
7.3.1 The Debugger.................................................................................................... 443
7.3.2 Mono History .................................................................................................... 443
7.4 Function Overloading ....................................................................................................... 444
7.4.1 A Closer Look at Functions .............................................................................. 444
7.4.1.1 A Basic Example ............................................................................. 445
7.4.2 Function Signature ............................................................................................ 446
7.4.3 Different Signatures .......................................................................................... 447
7.4.4 Putting It Together ............................................................................................ 448
7.4.5 Not Quite Recursion.......................................................................................... 449
7.4.6 DrawWords........................................................................................................ 450
7.4.7 What We’ve Learned .........................................................................................453
7.5 Accessors (or Properties) ...................................................................................................453
7.5.1 Value ................................................................................................................. 454
7.5.1.1 A Basic Example ..............................................................................455
7.5.2 Set Event.............................................................................................................455
7.5.3 Read-Only Accessor ......................................................................................... 457
7.5.4 Simplification .................................................................................................... 458
7.5.5 What We’ve Learned ........................................................................................ 459
xiv Contents

7.6 Base Classes: Another Look ............................................................................................. 459


7.6.1 Generalization—Base Classes .......................................................................... 459
7.6.2 Specialization .................................................................................................... 461
7.6.3 Base ................................................................................................................... 461
7.6.4 Partial ................................................................................................................ 463
7.6.5 Protected, Private, and Public ........................................................................... 466
7.6.6 What We’ve Learned ........................................................................................ 467
7.7 Optional Parameters ......................................................................................................... 467
7.7.1 Using Optionals................................................................................................. 468
7.7.1.1 A Basic Example ............................................................................. 469
7.7.2 Optional Arguments ......................................................................................... 470
7.7.3 Named Parameters .............................................................................................471
7.7.3.1 A Basic Example ............................................................................. 472
7.7.4 Combining What We’ve Learned ..................................................................... 473
7.7.5 What We’ve Learned .........................................................................................474
7.8 Delegate Functions ............................................................................................................474
7.8.1 Delegates ............................................................................................................474
7.8.1.1 A Basic Example ..............................................................................474
7.8.2 Delegate Signatures............................................................................................476
7.8.3 Stacking Delegates .............................................................................................476
7.8.4 Using Delegates................................................................................................. 477
7.8.5 Updating Delegates ............................................................................................478
7.8.6 What We’ve Learned ........................................................................................ 480
7.9 Interface ............................................................................................................................ 480
7.9.1 Early Planning .................................................................................................. 480
7.9.1.1 A Basic Example ............................................................................. 481
7.9.1.2 Using Accessors .............................................................................. 483
7.9.2 Multiple Interfaces ............................................................................................ 486
7.9.3 IComparer ......................................................................................................... 488
7.9.4 Using IComparer ............................................................................................... 490
7.9.5 What We’ve Learned .........................................................................................491
7.10 Class Constructors Revisited .............................................................................................491
7.10.1 A Basic Example ............................................................................................... 492
7.10.2 When to Create a New Class ............................................................................ 495
7.10.3 What We’ve Learned ........................................................................................ 498
7.11 Preprocessor Directives .................................................................................................... 498
7.11.1 A Basic Example ............................................................................................... 499
7.11.2 UNITY_EDITOR ............................................................................................. 500
7.11.3 Mobile Development ......................................................................................... 502
7.11.4 Warning............................................................................................................. 504
7.11.5 Organizing ........................................................................................................ 505
7.11.6 What We’ve Learned ........................................................................................ 506
7.12 Try Catch Finally .............................................................................................................. 506
7.12.1 A Basic Example ............................................................................................... 506
7.12.2 Exception Messages .......................................................................................... 508
7.12.3 Custom Exceptions ............................................................................................510
7.12.4 Finally ................................................................................................................512
7.12.5 Try-Catch and Finally in Use.............................................................................513
7.12.6 What We’ve Learned .........................................................................................513
7.13 IEnumerator .......................................................................................................................514
7.13.1 Enumeration .......................................................................................................515
7.13.1.1 A Basic Example ..............................................................................515
7.13.1.2 What Doesn’t Work ..........................................................................517
Contents xv

7.13.2 Implementing IEnumerator ................................................................................517


7.13.3 What We’ve Learned .........................................................................................521
7.14 Generics .............................................................................................................................521
7.14.1 Generic Functions ............................................................................................. 522
7.14.1.1 A Basic Example ............................................................................. 522
7.14.1.2 Why T? ............................................................................................ 523
7.14.2 Making Use of Generic Functions .................................................................... 523
7.14.3 Generic Types ................................................................................................... 526
7.14.4 Var ..................................................................................................................... 527
7.14.5 Multiple Generic Values ................................................................................... 529
7.14.6 Anonymous Objects .......................................................................................... 530
7.14.7 What We’ve Learned .........................................................................................533
7.15 Events .................................................................................................................................533
7.15.1 A Basic Example ............................................................................................... 534
7.15.2 A Proper Event .................................................................................................. 536
7.15.3 EventArgs ...........................................................................................................537
7.15.4 Generic EventArgs .............................................................................................539
7.15.5 What We’ve Learned ........................................................................................ 540
7.16 Extension Functions .......................................................................................................... 540
7.16.1 Extensions ..........................................................................................................541
7.16.2 A Basic Example ................................................................................................541
7.16.3 Overloading with Extensions ............................................................................ 543
7.16.4 Magic Mirror .................................................................................................... 544
7.16.5 What We’ve Learned ........................................................................................ 546
7.17 Destructors ........................................................................................................................ 546
7.17.1 A Basic Example ............................................................................................... 547
7.17.2 Clearing Events or Delegates ............................................................................ 549
7.17.3 OnDestroy ......................................................................................................... 550
7.17.4 The Ugly Truth...................................................................................................552
7.17.5 What We’ve Learned ........................................................................................ 554
7.18 Concurrency or Co-Routines .............................................................................................555
7.18.1 Yield ...................................................................................................................555
7.18.1.1 A Basic Example ..............................................................................557
7.18.2 Setting Up Timers ..............................................................................................558
7.18.3 Arrays of Delegates ............................................................................................561
7.18.4 Stopping a Co-Routine ...................................................................................... 562
7.18.5 What We’ve Learned ........................................................................................ 565
7.19 Dictionary, Stacks, and Queues ........................................................................................ 565
7.19.1 A Basic Example ............................................................................................... 565
7.19.2 ContainsKey ...................................................................................................... 567
7.19.3 Stacks ................................................................................................................ 568
7.19.3.1 A Basic Example ............................................................................. 568
7.19.4 Movement Stack ................................................................................................ 569
7.19.5 Queues................................................................................................................571
7.19.6 What We’ve Learned ........................................................................................ 573
7.20 Lambda Expressions ......................................................................................................... 573
7.20.1 Anonymous Expressions ....................................................................................574
7.20.1.1 A Basic Example—Action Func and Expression ............................575
7.20.2 Lambda Expressions ......................................................................................... 577
7.20.3 A Queue of Lambdas ........................................................................................ 577
7.20.4 What We’ve Learned ........................................................................................ 578
7.21 Leveling Up ...................................................................................................................... 578
xvi Contents

8. Extended........................................................................................................................................ 579
8.1 What We’ll Be Covering in This Chapter ........................................................................ 579
8.2 Review .............................................................................................................................. 579
8.3 Readability .........................................................................................................................581
8.3.1 ?: Notation ..........................................................................................................581
8.3.2 If ........................................................................................................................ 582
8.3.3 Smell ................................................................................................................. 583
8.3.3.1 Comments ......................................................................................... 583
8.3.3.2 One Responsibility Rule ................................................................... 583
8.3.3.3 Duplicated Code ............................................................................... 584
8.3.3.4 Naming ............................................................................................. 584
8.3.4 What We’ve Learned ........................................................................................ 584
8.4 Git Revisited ..................................................................................................................... 584
8.4.1 Git Branch and Merge ....................................................................................... 585
8.4.2 Merge Conflicts ................................................................................................. 588
8.4.3 What We’ve Learned .........................................................................................591
8.5 Recursion ...........................................................................................................................591
8.5.1 A Basic Example ............................................................................................... 593
8.5.2 Understanding Recursion .................................................................................. 593
8.5.3 In Practice ......................................................................................................... 594
8.5.4 What We’ve Learned ........................................................................................ 601
8.6 LINQ ................................................................................................................................. 601
8.6.1 Lambdas and Arrays ......................................................................................... 601
8.6.1.1 A Basic Example .............................................................................. 601
8.6.2 Var ..................................................................................................................... 602
8.6.3 LINQ From ....................................................................................................... 603
8.6.4 Strange Behaviors in LINQ .............................................................................. 604
8.6.5 LINQ on Lists ................................................................................................... 605
8.6.6 Monsters Linq ................................................................................................... 608
8.6.7 What We’ve Learned .........................................................................................611
8.7 Reflection ...........................................................................................................................613
8.7.1 A Basic Example ................................................................................................615
8.7.2 Reflection MethodInfo .......................................................................................615
8.7.3 What We’ve Learned .........................................................................................618
8.8 Dynamic............................................................................................................................ 620
8.8.1 ExpandoObject ...................................................................................................621
8.8.2 Expando Reader ................................................................................................ 622
8.8.3 What We’ve Learned ........................................................................................ 623
8.9 Bitwise Operators ............................................................................................................. 624
8.9.1 Big Endian and Little Endian ........................................................................... 624
8.9.2 Signed or Unsigned ........................................................................................... 625
8.9.3 Bitwise Or | ....................................................................................................... 627
8.9.4 Enums and Numbers ......................................................................................... 627
8.9.5 Bitwise And & .................................................................................................. 628
8.9.6 Bitwise Exclusive Or ^ (xor) ............................................................................. 629
8.9.7 Setting Bitwise Flags ........................................................................................ 630
8.9.8 Bitwise Shortcuts | = and ^ = .............................................................................631
8.9.9 Bits in Numbers ................................................................................................ 632
8.9.10 Bit Shifting >> and << ..................................................................................... 632
8.9.11 What We’ve Learned ........................................................................................ 634
8.10 Attributes .......................................................................................................................... 635
8.10.1 A Basic Example ............................................................................................... 635
8.10.2 Custom Attributes ............................................................................................. 639
10 Learning C# Programming with Unity 3D

The combination of the central processing unit (CPU) and operating system is often called a platform.
Each platform requires a unique native machine code to execute or run. Building code for each platform
is called a target. Unity 3D converts the bytecode into a native machine code and can target Mac, PC,
Android, and iOS.
Native machine code is the set of instructions that directly talk to the CPU and operating system (think
of the holes punched into a card and fed to a machine). Unity 3D is a simple way to generate the complex
set of instructions for your computer to run. Code that talks directly to the hardware is referred to as
“low-level” programming.
There are layers of software between you and the computer’s hardware. When writing C# for games
using Unity 3D, your code is compiled by .NET. Unity 3D then takes the bytecode from .NET and com-
piles a target platform into a native machine code.
Both Unity 3D and .NET are the layers underneath your code and the computer’s hardware, putting
you on a higher level. This is often referred to as a computer layer abstraction. That is why C# is usually
considered a high-level programming language.
Programming at a lower level requires much more knowledge of memory management and a wide
variety of other APIs that might include graphic cards, physics, sound, and everything else that runs a
game. Writing for a layer involves an in-depth knowledge of both the layers below and above the one
you’re writing for.
The computer hardware, such as CPU, graphics card, memory, and storage, live on the lowest level.
Above them is the basic input/output system (BIOS) and software that starts the hardware when you
press the Power button. Above that is your computer’s operating system and drivers that talk to the hard-
ware. Finally, Unity 3D lives above the operating system and your code lives above Unity 3D.
That is why we’re going to use Unity 3D to write games and not start from scratch in raw C++.
Otherwise, we’ll have to spend a few years learning about physics, rendering, and assembly language or
the instruction set that your CPU uses to operate.

1.7 What We’ve Learned


We should have an idea of what C# is, and what is required of you to learn it. If you haven’t done so
already, download and install the required software.
Unity 3D is free; there are no fees or royalties that need to be paid to the Unity 3D developers so long
as you’re not building any commercial product. Git is also free to use and you’ll need that to follow the
chapters on Git.
You’re even allowed to deploy your projects on anything Unity can target. Until you’re ready for a
commercial launch, the free license is all you’ll need for learning and sharing your game with your peers
on your computer.
Optionally, you may also want to setup an account on the Unity 3D site if you want to download and
use any of the assets from their Asset Store. A Visual Studio Community account and a GitHub account
may also come in useful later.

1.8 Leveling Up: The Path Ahead


The computer systems in place today have evolved from machines with moving parts. Telling these
machines what to do involved setting knobs and switches. Today, we use text organized into statements
and logic.
It is difficult to imagine a time when computers were so slow that simple calculations involved waiting
for clattering switches to finish moving around to produce an answer. Today, computers complete many
billions of calculations in a single second. All too often, we take for granted that computers rely on the
same concepts and systems engineered nearly a century ago.
Understanding how computers work puts into context how we write software. It is possible to write
software without this understanding, but it is harder to appreciate how it works.
Another random document with
no related content on Scribd:
This eBook is for the use of anyone anywhere in the United
States and most other parts of the world at no cost and with
almost no restrictions whatsoever. You may copy it, give it away
or re-use it under the terms of the Project Gutenberg License
included with this eBook or online at www.gutenberg.org. If you
are not located in the United States, you will have to check the
laws of the country where you are located before using this
eBook.

1.E.2. If an individual Project Gutenberg™ electronic work is derived


from texts not protected by U.S. copyright law (does not contain a
notice indicating that it is posted with permission of the copyright
holder), the work can be copied and distributed to anyone in the
United States without paying any fees or charges. If you are
redistributing or providing access to a work with the phrase “Project
Gutenberg” associated with or appearing on the work, you must
comply either with the requirements of paragraphs 1.E.1 through
1.E.7 or obtain permission for the use of the work and the Project
Gutenberg™ trademark as set forth in paragraphs 1.E.8 or 1.E.9.

1.E.3. If an individual Project Gutenberg™ electronic work is posted


with the permission of the copyright holder, your use and distribution
must comply with both paragraphs 1.E.1 through 1.E.7 and any
additional terms imposed by the copyright holder. Additional terms
will be linked to the Project Gutenberg™ License for all works posted
with the permission of the copyright holder found at the beginning of
this work.

1.E.4. Do not unlink or detach or remove the full Project


Gutenberg™ License terms from this work, or any files containing a
part of this work or any other work associated with Project
Gutenberg™.

1.E.5. Do not copy, display, perform, distribute or redistribute this


electronic work, or any part of this electronic work, without
prominently displaying the sentence set forth in paragraph 1.E.1 with
active links or immediate access to the full terms of the Project
Gutenberg™ License.
1.E.6. You may convert to and distribute this work in any binary,
compressed, marked up, nonproprietary or proprietary form,
including any word processing or hypertext form. However, if you
provide access to or distribute copies of a Project Gutenberg™ work
in a format other than “Plain Vanilla ASCII” or other format used in
the official version posted on the official Project Gutenberg™ website
(www.gutenberg.org), you must, at no additional cost, fee or expense
to the user, provide a copy, a means of exporting a copy, or a means
of obtaining a copy upon request, of the work in its original “Plain
Vanilla ASCII” or other form. Any alternate format must include the
full Project Gutenberg™ License as specified in paragraph 1.E.1.

1.E.7. Do not charge a fee for access to, viewing, displaying,


performing, copying or distributing any Project Gutenberg™ works
unless you comply with paragraph 1.E.8 or 1.E.9.

1.E.8. You may charge a reasonable fee for copies of or providing


access to or distributing Project Gutenberg™ electronic works
provided that:

• You pay a royalty fee of 20% of the gross profits you derive from
the use of Project Gutenberg™ works calculated using the
method you already use to calculate your applicable taxes. The
fee is owed to the owner of the Project Gutenberg™ trademark,
but he has agreed to donate royalties under this paragraph to
the Project Gutenberg Literary Archive Foundation. Royalty
payments must be paid within 60 days following each date on
which you prepare (or are legally required to prepare) your
periodic tax returns. Royalty payments should be clearly marked
as such and sent to the Project Gutenberg Literary Archive
Foundation at the address specified in Section 4, “Information
about donations to the Project Gutenberg Literary Archive
Foundation.”

• You provide a full refund of any money paid by a user who


notifies you in writing (or by e-mail) within 30 days of receipt that
s/he does not agree to the terms of the full Project Gutenberg™
License. You must require such a user to return or destroy all
copies of the works possessed in a physical medium and
discontinue all use of and all access to other copies of Project
Gutenberg™ works.

• You provide, in accordance with paragraph 1.F.3, a full refund of


any money paid for a work or a replacement copy, if a defect in
the electronic work is discovered and reported to you within 90
days of receipt of the work.

• You comply with all other terms of this agreement for free
distribution of Project Gutenberg™ works.

1.E.9. If you wish to charge a fee or distribute a Project Gutenberg™


electronic work or group of works on different terms than are set
forth in this agreement, you must obtain permission in writing from
the Project Gutenberg Literary Archive Foundation, the manager of
the Project Gutenberg™ trademark. Contact the Foundation as set
forth in Section 3 below.

1.F.

1.F.1. Project Gutenberg volunteers and employees expend


considerable effort to identify, do copyright research on, transcribe
and proofread works not protected by U.S. copyright law in creating
the Project Gutenberg™ collection. Despite these efforts, Project
Gutenberg™ electronic works, and the medium on which they may
be stored, may contain “Defects,” such as, but not limited to,
incomplete, inaccurate or corrupt data, transcription errors, a
copyright or other intellectual property infringement, a defective or
damaged disk or other medium, a computer virus, or computer
codes that damage or cannot be read by your equipment.

1.F.2. LIMITED WARRANTY, DISCLAIMER OF DAMAGES - Except


for the “Right of Replacement or Refund” described in paragraph
1.F.3, the Project Gutenberg Literary Archive Foundation, the owner
of the Project Gutenberg™ trademark, and any other party
distributing a Project Gutenberg™ electronic work under this
agreement, disclaim all liability to you for damages, costs and
expenses, including legal fees. YOU AGREE THAT YOU HAVE NO
REMEDIES FOR NEGLIGENCE, STRICT LIABILITY, BREACH OF
WARRANTY OR BREACH OF CONTRACT EXCEPT THOSE
PROVIDED IN PARAGRAPH 1.F.3. YOU AGREE THAT THE
FOUNDATION, THE TRADEMARK OWNER, AND ANY
DISTRIBUTOR UNDER THIS AGREEMENT WILL NOT BE LIABLE
TO YOU FOR ACTUAL, DIRECT, INDIRECT, CONSEQUENTIAL,
PUNITIVE OR INCIDENTAL DAMAGES EVEN IF YOU GIVE
NOTICE OF THE POSSIBILITY OF SUCH DAMAGE.

1.F.3. LIMITED RIGHT OF REPLACEMENT OR REFUND - If you


discover a defect in this electronic work within 90 days of receiving it,
you can receive a refund of the money (if any) you paid for it by
sending a written explanation to the person you received the work
from. If you received the work on a physical medium, you must
return the medium with your written explanation. The person or entity
that provided you with the defective work may elect to provide a
replacement copy in lieu of a refund. If you received the work
electronically, the person or entity providing it to you may choose to
give you a second opportunity to receive the work electronically in
lieu of a refund. If the second copy is also defective, you may
demand a refund in writing without further opportunities to fix the
problem.

1.F.4. Except for the limited right of replacement or refund set forth in
paragraph 1.F.3, this work is provided to you ‘AS-IS’, WITH NO
OTHER WARRANTIES OF ANY KIND, EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.
1.F.6. INDEMNITY - You agree to indemnify and hold the
Foundation, the trademark owner, any agent or employee of the
Foundation, anyone providing copies of Project Gutenberg™
electronic works in accordance with this agreement, and any
volunteers associated with the production, promotion and distribution
of Project Gutenberg™ electronic works, harmless from all liability,
costs and expenses, including legal fees, that arise directly or
indirectly from any of the following which you do or cause to occur:
(a) distribution of this or any Project Gutenberg™ work, (b)
alteration, modification, or additions or deletions to any Project
Gutenberg™ work, and (c) any Defect you cause.

Section 2. Information about the Mission of


Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many small
donations ($1 to $5,000) are particularly important to maintaining tax
exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About Project


Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.

Project Gutenberg™ eBooks are often created from several printed


editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.

You might also like