Learning C# Programming with Unity 3D

Page 1


, Learning # ' , Programming with nity 0 Alex Okita

Esta obra es propiedad del SIBDI - VCR

CRC Pressis an imprint of the Taylor & Francis Group, an informa

business


IJ

Sislema de Bibliolecas - UCR

FSC www.fsc.org

111111111111111111111111111111111111111

MIX Paper from responsible sources

rsc- C014174

726121(2)2

726002

3 1 MAY 2017 CRC Press Taylor & Francis Group 6000 Broken Sound Parkway NW, Suite 300 Boca Raton, FL 33487-2742 Š 2015 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 Version Date: 20140707 International Standard Book Number-l3: 978-1-4665-8652-9 (Paperback) 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 tion and explanation without intent to infringe. Library of Congress

or registered trademarks,

Cataloging-in-Publication

and are used only for identifica-

Data

Okita, Alex. Learning C# programming with Unity 3D / Alex Okita. pages ern Summary: "Ideal for game programmers with no prior experience with programming languages, this book covers the basics of programming and explains how C# is used to make a game in Unity3D. Interactive examples give C# code meaning. As more complex aspects of C# are explained the interactivity of example games gains depth. Common programming tasks are taught by way of making a game. The final result is a reader who is capable of understanding how to read and apply C# in Unity3D and apply that knowledge to other development environments that use C#"-- Provided by publisher. Includes bibliographical references and index. ISBN 978-1-4665-8652-9 (paperback) 1. Computer games--Programming. 2. C# (Computer program language) 3. Uniq. (Electronic resource) 4. Three-dimensional display systems. 1. Title. QA76.76.C6720432014 794.8'1526--dc23

Visit the Taylor & Francis Web site at http://www.taylorandfrancis.com and the CRC Press Web site at http://www.crcpress.com

2014006696


Contents Acknowledgments 1. Introduction: What This Book Is About 1.1 Why Read a Book: Why This Book Mayor May Not Be for you 1.1.1 Do I Need to Know Math? 1.1.2 Programming as a Form of Expression 1.1.3 Games as a Stage with Lights 1.2 Personal Information 1.3 A Brief History of Computer Programming: How Programming Came to Be 1.3.1 Mechanical Computers 1.3.2 Logic 1.3.3 Computer Science 1.3.4 Software 1.3.5 Modern Computer Language 1.3.6 The Future of Computer Languages 1.4 C#: A Flexible Programming Language 1.4.] C# Paradigm 1.4.2 Unity 3D: A Brief History of Game Engines 1.4.3 Why Use Unity 3D to Learn? 1.4.4 How Does Unity 3D Use C#? 1.4.4.1 How to Tell Unity 3D What to Do 1.5 What Is Programming? 1.5.1 What Does C# Look Like? 1.5.2 Learning to Copy and Paste 1.6 Compiling: Turning Words into Computer Instruction 1.7 What We've Learned 1.8 Leveling Up 2. Before 2.1 2.2 2.3

You Begin What Will Be Covered in This Chapter Downloading and Installing: It Is Free Unity 3D Overview: What We're Looking At... 2.3.1 The Main Panel 2.3.1.1 Toolbar 2.3.1.2 Hierarchy Panel 2.3.1.3 Scene and Game View 2.3.1.4 Inspector Panel 2.3.1.5 Project and Console Panel 2.3.2 Creating a New Project 2.3.3 A New Unity 3D Project 2.3.3.1 Assets Directory 2.3.3.2 Library Directory 2.3.3.3 Project Settings Directory 2.3.3.4 Temp Directory 2.3.4 Summary

xix 1 1 2 2 2 2 3 3 3 3 4 4 4 4 4 5 6 6 6 7 7 8 9 9 9

11 11 13 14 15 15 16 16 17 17 19 20 20 21 22 22 . 23

v


vi

Contents

2.4

2.5

2.6 2.7

Sample Code 2.4.1 Code Fragments 2.4.2 Are Errors Bad? 2.4.2.1 Compile-Time and Run-Time Errors 2.4.3 How to Follow Along 2.4.4 Summary Working with C#: Game Engines 2.5.1 Getting Project Files 2.5.2 Creating and Assigning a New C# File: Now It's Your Turn 2.5.3 Naming Your New File 2.5.4 Using Your New File 2.5.5 Unity 3D Tools 2.5.6 Running Live Code 2.5.7 Saving a Scene 2.5.8 Opening a Scene 2.5.9 Summary What We've Learned Leveling Up

3. First Steps 3.1 What Will Be Covered in This Chapter 3.2 Review 3.3 Tokens 3.3.1 Writing C# 3.3.2 Separator Tokens 3.3.3 Operator Tokens 3.3.4 Other Operator Tokens 3.3.5 Literals 3.3.6 Transitive and Non-Transitive Operations 3.3.7 Putting It All Together 3.3.8 What We've Learned 3.4 Statements and Expressions 3.4.1 Expressions 3.4.2 How Code Is Executed 3.4.3 Thinking in Algorithms 3.4.3.1 Wash, Rinse, Repeat 3.4.4 What We've Learned 3.5 Keywords 3.5.1 Class 3.5.2 What We've Learned 3.6 White Space 3.6.1 Pick a Flavor 3.6.2 What We've Learned 3.7 Code Blocks 3.7.1 What We've Learned 3.8 Classes 3.8.1 Objects 3.8.2 What We've Learned 3.9 Variables 3.9.1 Identifiers 3.9.2 Data

23 23 24 24 25 25 25 26 27 30 .31 36 38 39 39 39 40 40 .41 41 .41 42 42 43 44 44 44 44 45 46 46 46 47 47 49 49 49 50 51 51 55 56 56 57 57 58 61 61 62 62


Contents

vii

3.9.3

3.10

3.11

3.12

3.13

3.14

3.15

Declaring a Variable 3.9.3.1 A Basic Example 3.9.4 Dynamic Initialization 3.9.5 What We've Learned Variable Names 3.lO.1 UsingVariables in Unity 3D 3.10.2 Variable Assignment 3.10.3 Putting It Together 3.10.4 What We've Learned Types: A First Look 3.11.1 Value and Reference Types 3.11.2 What We've Learned Strong Typing 3.12.1 Dynamic Typing 3.12.2 What We've Learned Type Casting, Numbers 3.13.1 Explicit versus Implicit Casting 3.13.1.1 A Basic Example 3.13.2 What We've Learned Comments 3.14.1 Line Numbers 3.14.2 Code Folding 3.14.3 Surnrnary Comments 3.14.4 Navigating in Code 3.14.5 What We've Learned Leveling Up: Moving On to Basics

4. Basics: The Building Blocks of Code 4.1 What Will Be Covered in This Chapter 4.2 Review 4.3 Building Up a Game Idea: Working with What You Know 4.3.1 Design from Experience 4.3.1.1 Know yourself 4.3.2 Primary Activity 4.3.3 Moment to Moment.. 4.3.4 Actions to Functions 4.3.5 Compromise 4.3.6 Starting with Controls 4.3.7 What We've Learned 4.4 Creating a Class 4.4.1 Class Declaration 4.4.1.1 A Basic Example 4.4.1.2 Value and Reference Types 4.4.2 Adding Data Fields 4.4.3 Access Modifiers and the Dot Operator 4.4.4 Class Scope 4.4.5 Class Members 4.4.6 What We've Learned 4.5 Directives 4.5.1 Libraries 4.5.2 Ambiguous NameSpaces 4.5.3 What We've Learned

62 64 65 65 66 68 69 71 73 74 74 75 75 76 76 77 78 78 80 81 83 84 85 86 87 87 89 89 89 90 90 90 91 91 91 92 92 93 93 94 95 96 96 96 98 99 99 102 103 108 108


viii

Contents

4.6

4.7

4.8

4.9

4.10

4.11

4.12

Functions 4.6.1 What Are Functions? 4.6.2 Unity 3D Entry Points 4.6.3 Writing a Function 4.6.3.1 A Basic Example 4.6.4 More on White Space and Tabs 4.6.5 What We've Learned Order of Operation: What Is Calculated and When 4.7.1 .Evaluating Numbers 4.7.1.1 Math 4.7.1.2 Operator Evaluation 4.7.2 What We've Learned Scope: A First Look 4.8.1 Class Scope 4.8.2 Function Scope 4.8.3 What We've Learned This 4.9.1 A Basic Example 4.9.2 When This Is Necessary 4.9.3 Awkward Names 4.9.4 What We've Learned Turning Ideas into Code-Part 1 4.10.1 Mouse Input 4.10.2 GameObject 4.10.3 What We've Learned Logic and Operators 4.11.1 Booleans 4.11.2 Equality Operators 4.11.2.1 A Basic Example 4.11.3 If and Branching 4.11.3.1 Not! 4.11.4 Flowcharts 4.11.5 Relational Operators 4.11.5.1 Else 4.11.5.2 Else If 4.11.6 Rearranging Logic 4.11.6.1 Flawed Logic 4.11.6.2 Unreachable Code 4.11.7 Another Look at Scope 4.11.8 What We've Learned Loops 4.12.1 Unary Operators 4.12.2 While 4.12.3 For 4.12.4 Do-While 4.12.5 Postfix and Prefix Notation 4.12.6 Using Loops 4.12.7 Loops within Loops 4.12.8 Runaway Loops 4.12.9 Breakpoints: A First Look 4.12.10 What We've Learned

109 109 110 1]2 112 ] 13 115 115 116 116 118 120 120 121 124 126 126 127 128 128 129 129 131 131 133 133 134 134 135 135 137 137 138 139 140 140 143 144 145 147 148 148 149 149 151 152 153 155 156 157 160


Contents

4.13

4.14

4.15

ix

Scope, Again 4.13.1 Visibility or Accessibility 4.13.1.1 A Basic Example 4.l3.2 Global Scope 4.13.3 What We've Learned Warnings versus Errors 4.14.1 Warnings 4.14.2 Errors 4.14.3 Understanding the Debugger 4.14.4 What We've Learned Leveling Up: Fundamentals 4.15.1 Style Guides

S. Fundamentals 5.1 What Will Be Covered in This Chapter 5.2 Review 5.2.1 Modular Code 5.3 Inheritance: A First Look 5.3.1 Class Members 5.3.2 Instancing 5.3.2.1 A Basic Example 5.3.3 Parent and Child 5.3.4 Object 5.3.4.1 A Type Is Not an Object 5.3.5 != null 5.3.6 What We've Learned 5.4 Instancing 5.4.1 Class Initialization 5.4.2 New 5.4.3 Constructors 5.4.4 What We've Learned 5.5 Static 5.5.1 A Basic Example 5.5.2 Static Variables 5.5.2.1 A Basic Example 5.5.3 Static Functions 5.5.4 Putting It All Together 5.5.5 What We've Learned 5.6 Turning Ideas into Code-Part 2 5.6.1 Input Manager 5.6.2 What We've Learned 5.7 Jump Statements 5.7.1 Return 5.7.1.1 A Basic Example 5.7.2 Returning Objects 5.7.3 A Class Is a Type 5.7.4 Null Is Not Void 5.7.5 What We've Learned 5.8 Operators and Conditions 5.8.1 Conditional Operators && and II 5.8.1.1 A Basic Example 5.8.2 What We've Learned

160 160 161 161 l64 164 165 165 165 168 168 169

0 •••••••••••••••••••••••••

171 171 171 173 175 176 178 180 180 182 183 184 185 185 186 187 187 190 190 191 192 193 195 197 201 201 206 214 214 215 215 216 217 219 219 219 220 220 223


,

---

Contents

x

5.9

5.10

5.11

5.12

5.13

5.14

5.15

5.16

5.17

••

Arrays: A First Look 5.9.1 Fixed-Sized Arrays 5.9.1.1 A Basic Example 5.9.2 Foreach 5.9.2.1 A Basic Example 5.9.3 Dynamic Initialization 5.9.4 Using the While Loop with Arrays 5.9:4.1 Setting Array Values 5.9.4.2 Getting Array Values 5.9.5 What We've Learned Jump Statements: Break and Continue 5.10.1 A Basic Example 5.10.1.1 Continue 5.10.2 ZombieData 5.10.3 Foreach-Again 5.10.4 What We've Learned Multidimensional Arrays 5.11.1 Columns and Rows 5.11.1.1 A Basic Example 5.11.2 A Puzzle Board 5.11.3 Checking Range 5.11.4 What We've Learned Array List 5.12.1 A Basic Example 5.12.2 ArrayList.ContainsO 5.12.3 Remove 5.12.4 Sort and Reverse 5.12.5 What We've Learned Strings 5.13.1 Declaring a String 5.13.1.1 A Basic Example 5.13.2 Escape Sequences 5.13.3 Verbatim Strings: @ 5.13.4 String Format 5.13.5 What We've Learned Combining What We've Learned 5.14.1 Timers 5.14.2 Adding in Classes 5.14.3 What We've Learned Source Version Control 5.15.1 Modern Version Control 5.15.2 The Repository 5.15.3 GitHub 5.15.4 What We've Learned 5.15.5 Project Files Setting Up a Repository 5.16.1 Push 5.16.2 Gitignore 5.16.3 Pull 5.16.4 Contributors 5.16.5 What We've Learned Leveling Up: On to the Cool Stuff...

224 224 224 229 229 229 230 230 231 231 232 232 232 232 234 235 235 236 236 238 242 244 245 246 248 249 250 251 251 251 251 253 254 255 255 255 255 261 262 262 263 263 264 264 264 264 271 271 273 274 275 276


Contents

6. Intermediate 6.1 What Will Be Covered in This Chapter 6.2 Review 6.3 Pseudocode 6.3.1 Thinking It Through 6.3.2 Class Members 6.3.2.1 A Basic Example 6.3.2.2 Thinking like a Programmer. 6.3.3 Return 6.3.3.1 A Basic Example 6.3.4 Arguments aka Args (Not Related to Pirates) 6.3.4.1 The Basic Example 6.3.4.2 Multiple Args 6.3.4.3 Using Args 6.3.5 Assignment Operators 6.3.5.1 A Basic Example 6.3.6 What We've Learned 6.4 Class Constructors 6.4.1 A Basic Example 6.4.2 What We've Learned 6.4.3 What We've Learned 6.5 Arrays Revisited 6.5.1 Using Arrays in Unity 3D 6.5.1.1 Starting with 0 6.5.1.2 Mathf 6.5.].3 Time 6.5.2 Instancing with AddComponenu): 6.5.3 Type Casting Unity 3D Objects 6.5.4 What We've Learned 6.6 Enums 6.6.1 Using Enums 6.6.2 Combining What We've Learned 6.6.3 What We've Learned 6.7 Switch 6.7.1 A Basic Example 6.7.2 Default 6.7.3 What We've Learned 6.7.4 Fall Through 6.7.5 goto Case 6.7.6 Limitations 6.7.7 What We've Learned 6.8 Structs 6.8.1 Structs 6.8.2 Struct versus Class 6.8.3 Without Structs 6.8.4 Handling Structs 6.8.5 Accessing Structs 6.8.6 Global Access 6.8.7 What We've Learned 6.9 Class Data 6.9.] Character Base Class 6.9.2 Const

xi

279 279 279 280 280 281 281 282 285 285 286 286 287 288 290 291 292 292 293 294 296 296 297 298 301 30] 302 303 305 305 306 308 3]0 310 311 313 3]5 319 320 320 .321 321 322 322 324 324 326 327 328 328 329 330


xii

Contents

6.10

6.11

6.12

6.13

6.14

6.15

6.16

6.9.3 Readonly 6.9.4 What We've Learned Namespaces 6.10.1 A Basic Example 6.10.2 Directives in Namespaces 6.10.3 Ambiguous References 6.10.4 Alias Directives 6.lO.5 Putting Namespaces to Work 6.10.6 Extending Namespaces 6.10.7 What We've Learned Functions Again 6.11.1 Parameter Lists 6.11.1.1 A Basic Example 6.11.2 Side Effects 6.11.3 Multiple Arguments 6.11.4 Useful Parameters 6.11.4.1 The Rule of Three 6.11.5 Foreach versus For 6.11.6 What We've Learned Unity 3D Execution Order 6.12.1 A Basic Example 6.12.2 Component Execution Order 6.12.3 What We've Learned Inheritance Again 6.13.1 Function Overrides 6.13.1.1 A Basic Example 6.13.2 Class Inheritance 6.13.2.1 Sharing Common Attributes 6.13.3 Object 6.13.4 What We've Learned Type Casting Again 6.14.1 ÂŤTypeÂť versus "as" 6.14.2 User-Defined Type Conversion 6.14.3 Implicit versus Explicit Type Conversion 6.14.4 Break 6.14.5 What We've Learned Working with Vectors 6.15.1 Vectors Are Objects 6.15.2 Stepping through MonsterGenerator 6.15.3 Gizmos 6.15.3.1 A Basic Example 6.15.4 Using Gizmos 6.15.4.1 Building Up Parameters 6.15.5 Optimizing 6.15.6 What We've Learned goto Labels 6.16.1 A Basic Example 6.16.2 Zombie State Machine 6.16.3 This as a Reference to Yourself 6.16.4 HumanState Based on ZombieS tate 6.16.5 The Is Keyword 6.16.6 What We've Learned

,

330 331 331 331 .332 334 336 337 338 339 339 339 340 340 341 342 342 343 345 345 346 349 352 352 352 352 355 .355 358 359 360 361 362 363 363 365 365 365 366 368 369 371 375 376 377 377 378 379 385 388 389 392


Contents

6.17

6.18

6.19

6.20

6.21

6.22

6.23

6.24

xiii

More on Arrays 6.17.1 Length and Count.. 6.17.1.1 A Basic Example 6.17.2 Foreach: A Reminder 6.17.3 Discovery 6.17.4 Putting It Together 6.17.5 What We've Learned Out Parameter 6.18.1 A Basic Example 6.18.2 Simple Sort (Bubble Sort) 6.18.3 Simple Sort Proof. 6.18.4 What We've Learned Ref Parameter 6.19.1 A Basic Example 6.19.2 Code Portability Side Effects 6.19.3 What We've Learned Type Casting Numbers 6.20.1 Number Types 6.20.2 Integers 6.20.2.1 Signed Numbers 6.20.3 Floating Point 6.20.4 What We've Learned Types and Operators 6.21.1 GetTypeO 6.21.2 More Type Casting 6.21.3 Type Aliasing 6.21.4 Boxing and Unboxing Operator Overloading 6.22.1 A Basic Example 6.22.2 Overloading * 6.22.3 Overloading < 6.22.4 What We've Learned Controlling Inheritance 6.23.1 Sealed 6.23.1.1 A Basic Example 6.23.2 Abstract 6.23.2.1 A Basic Example 6.23.3 Abstract: Abstract 6.23.4 Putting This to Use 6.23.5 What We've Learned Leveling Up

7. Advanced 7.1 What Will Be Covered in This Chapter 7.2 Review 7.2.1 Moving Forward 7.3 MonoDevelop 7.3.1 Find in Files 7.3.2 Word Processors 7.3.3 Mono History

392 392 392 393 397 397 405 405 406 407 .410 .410 .413 414 .415 .415 417 419 419 .419 420 420 420 .421 422 425 425 426 426 428 429 430 430 .431 .431 .432 .432 436 438 441 442 443 443 444 444 445 445 447 448


xiv

Contents

7.4

7.5

7.6

7.7

7.8

7.9

7.10

Function Overloading 7.4.1 A Closer Look at Functions 7.4.1.1 A Basic Example 7.4.2 Function Signature 7.4.3 Different Signatures 7.4.4 Putting It Together 7.4.5 Not Quite Recursion 7.4.6 Drawword 7.4.7 What We've Learned Accessors (or Properties) 7.5.1 Value 7.5.1.1 A Basic Example 7.5.2 Set Event. 7.5.3 Read-Only Accessor 7.5.4 Simplification 7.5.5 What We've Learned Base Classes: Another Look 7.6.1 Generalization-Base Classes 7.6.2 Specialization 7.6.3 Base 7.6.4 Partial 7.6.5 Protected, Private, and Public 7.6.6 What We've Learned Optional Parameters 7.7.1 Using Optionals 7.7.1.1 A Basic Example 7.7.2 Optional Arguments 7.7.3 Named Parameters 7.7.3.1 A Basic Example 7.7.4 Combining What We've Learned 7.7.5 What We've Learned Delegate Functions 7.8.1 Delegates 7.8.1.1 A Basic Example 7.8.2 Delegate Signatures 7.8.3 Stacking Delegates 7.8.4 Using Delegates 7.8.5 What We've Learned Interface 7.9.1 Early Planning 7.9.1.1 A Basic Example 7.9.1.2 Using Accessors 7.9.2 Interface Methods 7.9.2.1 Breaking a Fixing 7.9.3 Multiple Interfaces 7.9.4 IComparer 7.9.5 Using IComparer 7.9.6 What We've Learned Class Constructors Revisited 7.10.1 A Basic Example 7.10.2 When to Create a New Class 7.10.2.1 Add in a Private zposition Offset

448 448 449 450 450 .451 .452 .452 456 456 457 457 458 459 460 460 460 461 463 464 466 .471 472 472 .474 .474 .475 .476 477 ..478 .478 479 479 479 480 481 482 484 484 485 485 486 489 .491 .491 492 493 494 494 495 496 499


Contents

xv

7.10.3

7.11

7.12

7.13

7.14

7.15

7.16

7.17

7.18

Static Functions 7.10.3.1 Using a Delegate Function 7.10.4 What We've Learned Preprocessor Directives 7.11.1 A Basic Example 7.11.2 UNITy_EDITOR 7.11.3 Warning 7.11.4 Organizing 7.11.5 What We've Learned Exceptions 7.12.1 A Basic Example 7.12.2 Exception Messages 7.12.3 Custom Exceptions 7.12.4 Finally 7.12.5 Try-Catch and Finally in Use 7.12.6 What We've Learned IEnumerator 7.13.1 Enumeration 7.13.1.1 A Basic Example 7.13.1.2 What Doesn't Work 7.13.2 Implementing IEnumerator 7.13.3 What We've Learned Generics 7.14.1 Generic Functions 7.14.1.1 A Basic Example 7.14.1.2 Why T? 7.14.2 Making Use of Generic Functions 7.14.3 Generic Types 7.14.4 Var 7.14.5 Multiple Generic Values 7.14.6 What We've Learned Events 7.15.1 A Basic Example 7.15.2 A Proper Event 7.15.3 EventArgs 7.15.4 Update 0 to Event 7.15.5 Generic EventArg 7.15.6 What We've Learned Unity-Friendly Classes 7.16.1 Extensions 7.16.2 A Basic Example 7.16.3 Inheriting from Object 7.16.4 OnUpdate 7.16.5 What We've Learned Destructors 7.17.1 A Basic Example 7.17.2 Clearing Out Objects 7.17.3 What We've Learned Concurrency or Coroutines 7.18.1 yield 7.18.1.1 A Basic Example 7.18.2 Setting Up Timers

500 501 503 504 504 505 507 507 509 509 509 510 512 514 514 515 516 516 516 518 519 522 523 523 523 524 525 527 528 530 531 532 532 535 536 537 538 544 546 546 546 549 549 550 551 551 552 556 556 556 557 559


xvi

Contents

7.19

7.20

7.21

7.22

7.18.3 Random Decisions with Logic 7.18.4 Stopping a Coroutine 7.18.5 What We've Learned Dictionary, Stacks, and Queues 7.19.1 A Basic Example 7.19.2 ContainsKey 7.19.3 Stacks 7.19.3.1 . A Basic Example 7.19.4 Queues 7.19.5 What We've Learned Callbacks 7.20.1 A Basic Example 7.20.2 Dynamic Callback Assignment 7.20.3 WWW 7.20.4 What We've Learned Lambda Expressions 7.21.l Anonymous Expressions 7.21.1.1 A Basic Example 7.21.2 Lambda Expressions 7.21.2.1 A Basic Example 7.21.3 When Lambdas Are Useful 7.21.4 Lambda Statements 7.21.5 What We've Learned Leveling Up

8. Extended 8.1 What We'll Be Covering in This Chapter. 8.2 Review 8.3 Readability Optimizations and Idioms 8.3.1 ?: Notation 8.3.2 If 8.3.3 Smell 8.3.3.1 Comments 8.3.3.2 One Responsibility Rule 8.3.3.3 Duplicated Code 8.3.3.4 Long Names 8.3.4 What We've Learned 8.4 Source Control Revisited 8.4.1 Diff and Merge 8.4.2 KDiff3 8.4.3 Avoiding Conflicts 8.4.4 What We've Learned 8.5 Debugging 8.5.1 Debugging Accessors 8.5.2 Locals 8.5.3 Call Stack 8.5.4 Watch List 8.5.5 What We've Learned 8.6 Recursion 8.6.1 A Basic Example 8.6.2 Understanding Recursion 8.6.3 In Practice

561 563 564 564 565 566 568 569 573 574 .574 575 .576 577 579 579 580 580 581 581 583 583 583 584 585 585 585 587 587 588 588 588 588 589 589 589 589 590 594 594 595 595 595 598 600 600 603 604 605 605 606


Contents

8.7

8.8

8.9

8.10

8.11

8.12

8.13

xvii

8.6.4 Recursion Types 8.6.5 What We've Learned Reflection 8.7.1 A Basic Example 8.7.2 Reflection MethodInfo 8.7.3 What We've Learned LINQ 8.8.1 Lambdas and Arrays 8.8.1.1 A Basic Example 8.8.2 Var 8.8.3 LINQ From 8.8.4 Strange Behaviors in LINQ 8.8.5 Greedy Operator 8.8.6 What We've Learned Bitwise Operators 8.9.1 Big Endian and Little Endian 8.9.2 Signed or Unsigned 8.9.3 Bitwise Or I 8.9.4 Enums and Numbers 8.9.5 Bitwise And & 8.9.6 Bitwise Exclusive Or 1\ (xor) 8.9.7 Setting Bitwise Flags 8.9.8 Bitwise Shortcuts I = and 1\ = 8.9.9 Bits in Numbers 8.9.10 Bit ShiftingÂť and ÂŤ 8.9.11 What We've Learned Bitwise Math 8.10.1 Two's Complement 8.10.2 Unary Operator 8.10.3 Bitwise Addition and Subtraction 8.10.4 Bitwise Multiplication 8.10.5 Bitwise Tricks 8.10.6 What We've Learned Attributes 8.11.1 A Basic Example 8.11.2 Custom Attributes 8.11.3 Finding Custom Attributes 8.11.4 Attribute Constructor 8.11.5 Multiple Attributes 8.11.6 Putting Attributes to Work 8.11.7 Attribute Flags 8.11.8 What We've Learned Architectures and Organization 8.12.1 Planning Structure 8.12.2 Fixing Namespaces 8.12.3 Namespace and Directory Structure 8.12.4 Using Partial 8.12.5 Refactoring 8.12.6 What We've Learned Design Patterns 8.13.1 Creational Design Patterns 8.13.1.1 Singleton Pattern

611 611 612 612 613 615 616 616 616 617 618 619 619 620 620 620 621 622 623 623 624 625 626 626 626 627 627 628 628 628 630 631 632 633 633 635 635 636 637 638 641 642 642 643 643 646 648 650 652 653 653 653


8.14

8.13.2 Structural Design Patterns 8.13.3 Behavioral Design Patterns 8.13.4 What We've Learned Continuing on Your Own

9. Stuff We Couldn't Cover 9.1 The Extern and Unsafe Keywords 9.2 Dynamic 9.2.1 Dynamic 'versus Var

654 654 654 655 657 657 657 657

10. Good Luck

661

Index

663


Learning C# Programming with Unity 3D Designed to give you enough familiarity in a programming language to be immediately productive, Learning C# Programming with Unity 3D provides the basics of programming and brings you quickly up to speed. Organized into easy-to-follow lessons, the book covers how C# is used to make a game in Unity 3D. After reading this book, you will be armed with the knowledge required to feel confident in learning more. You'll have what it takes to at least look at code without your head spinning. Writing a massive multiplayer online role-playing game is quite hard, of course, but learning how to write a simple behavior isn't. Like drawing, you start off with the basics such as spheres and cubes. After plenty of practice, you'll be able to create a real work of art. This applies to writing code-you start off with basic calculations, then move on to the logic that drives a complex game. By the end of this book, you will have the skills to be a capable programmer, or at least know what is involved with how to read and write code. Although you could go online and find videos and tutorials, there is a distinct advantage when it comes to learning things in order and in one place. Most online tutorials for C# are scattered, disordered, and incohesive. It's difficult to find a good starting point and even more difficult to find a continuous list of tutorials to bring you to any clear understanding of the C# programming language. This book not only gives you a strong foundation, but puts you on the path to game development.


Turn static files into dynamic content formats.

Create a flipbook
Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.