Practical Game Development with Unity ® and Blender ™ Alan Thorn Cengage Learning PTR Australia•Brazil•Japan•Korea•Mexico•Singapore•Spain•UnitedKingdom•UnitedStates PracticalGameDevelopmentwith ©2015CengageLearningPTR. Unity®andBlender™ CENGAGEandCENGAGELEARNINGareregisteredtrademarksofCengage AlanThorn Learning,Inc.,withintheUnitedStatesandcertainotherjurisdictions. PublisherandGeneralManager, ALLRIGHTSRESERVED.Nopartofthisworkcoveredbythecopyright CengageLearningPTR:StacyL.Hiquet hereinmaybereproduced,transmitted,stored,orusedinanyformorbyany AssociateDirectorofMarketing: meansgraphic,electronic,ormechanical,includingbutnotlimitedto SarahPanella photocopying,recording,scanning,digitizing,taping,Webdistribution, informationnetworks,orinformationstorageandretrievalsystems,except ManagerofEditorialServices: aspermittedunderSection107or108ofthe1976UnitedStatesCopyright HeatherTalbot Act,withoutthepriorwrittenpermissionofthepublisher. SeniorMarketingManager: MarkHughes Forproductinformationandtechnologyassistance,contactusat SeniorProductManager:EmiSmith CengageLearningCustomer&SalesSupport,1-800-354-9706. ProjectEditor:KateShoup Forpermissiontousematerialfromthistextorproduct,submit TechnicalReviewer:MichaelDuggan allrequestsonlineatcengage.com/permissions. Copyeditor:KateShoup Furtherpermissionsquestionscanbeemailedto [email protected]. InteriorLayoutTech:MPSLimited CoverDesigner:MikeTanamachi UnityisaregisteredtrademarkofUnityTechnologies.Blenderisa Indexer:KellyTalbotEditingServices trademarkofBlender.Allothertrademarksarethepropertyoftheir Proofreader:KellyTalbotEditing respectiveowners. Services Allimages©CengageLearningunlessotherwisenoted. LibraryofCongressControlNumber:2014937090 ISBN-13:978-1-305-07470-5 ISBN-10:1-305-07470-X eISBN-10: 1-305-07471-8 CengageLearningPTR 20ChannelCenterStreet Boston,MA02210 USA CengageLearningisaleadingproviderofcustomizedlearningsolutions withofficelocationsaroundtheglobe,includingSingapore,theUnited Kingdom,Australia,Mexico,Brazil,andJapan.Locateyourlocalofficeat: international.cengage.com/region. CengageLearningproductsarerepresentedinCanadabyNelson Education,Ltd. Foryourlifelonglearningsolutions,visitcengageptr.com. Visitourcorporatewebsiteatcengage.com. PrintedintheUnitedStatesofAmerica 1234567161514 This book is dedicated to everybody that helps make so many wonderful tools and software programs freely available to so many game developers worldwide. Acknowledgments Many people, events, and scenarios conspired to make this book what it is, and to ensure ’ its quality. Among those people, Id like to thank Emi Smith for helping to arrange the book and managing the process generally; Kate Shoup for her editorial skills and recom- mendations; Michael Duggan for his technical reviewing; and all the other people who ’ contributed in some way. There are simply too many to list here separately. Oh, and Id also like to thank you, the reader, for taking an interest in game development and for choosing this book to help you along. I hope it proves useful to you. Alan Thorn, 2014. London. iv About the Author Alan Thorn is a freelance game developer and author with more than 12 years of industry experience. He is the founder of London-based game studio Wax Lyrical Games and is the creator of the award-winning adventure game Baron Wittard: Nemesis of Ragnarok. He has worked freelance on more than 500 projects worldwide including games, simulators, kiosks, and augmented reality software for game studios, museums, and theme parks. He has spoken on game development at venues worldwide and is the author of more than 10 books on game development, including Teach Yourself Games Programming, Unity 4 Fundamentals, and UDK Game Development. Alan Thorn is currently working on an upcoming game, Mega Bad Code. For more information on Alan Thorn and Wax Lyrical Games, visit www.alanthorn.net and www.waxlyricalgames.com v Contents Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . xi Chapter 1 The 10-Stage Workflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1 Introducingthe10-StageWorkflow ...................................... 2 Stage1:Brainstorming................................................. 4 Stage2:InitialDesign.................................................. 7 GameOverview ....................................................9 GameDetails.......................................................9 References........................................................10 Stage3:Prototyping.................................................. 10 Stage4:RefiningDesign .............................................. 11 ProjectManagement ................................................. 12 IdentifyResources..................................................13 CompressSpace ...................................................13 ScheduleWork ....................................................14 Stage5:AssetCreation ............................................... 14 Stage6:ImportingAssets.............................................. 15 Stage7:LevelDesign................................................. 17 Stage8:Scripting .................................................... 18 Stage9:Testing ..................................................... 20 Stage10:Building.................................................... 21 RecommendationsforWorkingPractice.................................. 22 Summary........................................................... 24 vi Contents vii Chapter 2 Blender-to-Unity Workflow. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .25 ConfiguringtheBlenderGUI........................................... 26 DarkThemes......................................................27 DisablePythonTooltips .............................................29 MayaControls.....................................................31 TheClose-Without-Saving“Bug” ....................................... 34 ExportingBlenderModelstoUnity...................................... 35 .BlendFiles .......................................................36 Tutorial:ExportingManuallytoFBX...................................37 ExploringFBXFiles................................................... 55 Tutorial:ImportingFBXFilesintoUnity .................................. 56 LightmapUVs.....................................................57 ScaleFactor.......................................................59 Summary........................................................... 61 Chapter 3 Modular Environments and Static Meshes. . . . . . . . . . . . . . . . . . . . . .63 AdvantagesoftheModularMethod..................................... 65 GettingStartedwithModularEnvironmentsinBlender ..................... 66 ExtendingfromtheBaseTile........................................... 70 ModularEnvironmentBlenderWorkflow................................. 71 FlippingNormals...................................................71 BackfaceCulling...................................................72 VertexandIncrementalSnapping.....................................73 N-gons...........................................................77 SearchingforN-gons ...............................................79 UndoingandRemovingDoubles......................................81 Mirroring.........................................................82 UsingVertexGroups................................................84 MeshDisplaySettings...............................................87 UVMappingandTextureCreation...................................... 89 MarkingSeams,UVMapping,andModeling............................89 AtlasTexturesandUVOverlapping....................................92 EstablishTexelDensity..............................................94 ImportingandConfiguringEnvironmentsinUnity ......................... 97 UsingPrefabs ....................................................... 99 StaticBatching ..................................................... 102 Summary.......................................................... 103 Chapter 4 Terrain. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .105 CreatingTerraininUnity............................................. 106 TerrainSettings...................................................107 viii Contents SculptingTerrain .................................................109 Texture-PaintingTerrain ...........................................110 EvaluatingUnityTerrains............................................. 113 BlenderTerrainModeling ............................................ 115 TheProportionalEditingMethod ....................................117 TheDisplacement-TextureMethod...................................120 TheSculptingMethod .............................................124 TerrainResolution .................................................. 128 Texture-PaintingTerrain ............................................. 129 UVMappingTerrains..............................................130 GeneratingaTextureforPainting....................................131 PaintingfromtheUVImageEditor...................................132 Paintingfromthe3DView .........................................137 PaintingwithTextures.............................................139 WorkingwithRoadsandPaths ........................................ 143 CreatingRoads...................................................144 Summary.......................................................... 149 Chapter 5 Animation Workflows . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .151 AnimationUnits:TheKeyframe ....................................... 152 PreparingforAnimationinBlender .................................... 153 UseaDedicatedAnimationLayout...................................154 BewareofAuto-Key...............................................154 InsertSingleKeyframes ............................................156 AnimationLength ................................................157 ExportingAnimationstoFBX........................................159 WorkingwithMultipleAnimations...................................160 KeyframeAnimationsfromBlendertoUnity............................. 160 Follow-PathAnimationsandAnimationBaking........................... 171 BlendShapesandShapeKeys ......................................... 179 BonesandRigging .................................................. 186 AlwaysNameBones...............................................186 UseX-AxisMirrorforCharacterRigs..................................187 ForwardandInverseKinematics .....................................189 DeformandControlBones .........................................192 ExportingRiggedCharacters........................................193 ImportingRiggedMeshesintoUnity..................................195 Summary.......................................................... 197 Contents ix Chapter 6 Objects, Dependencies, and Event-Driven Programming . . . . . . . . .199 Hard-CodedDependencies............................................ 200 SolvingDI:Component-BasedDesignandMessages ....................... 202 Component-BasedDesign ..........................................202 Messages........................................................204 TakingMessagesFurther:BroadcastMessageandHierarchies ............... 208 SendingMessagestoSelectedObjects .................................. 209 SendingMessagestoParents.......................................... 210 NotificationSystem ................................................. 210 NotificationsManagerIn-Depth ...................................... 214 Singletons......................................................... 216 MessagesandActiveObjects.......................................... 217 TraversingGameObjectHierarchies .................................... 218 Summary.......................................................... 219 Chapter 7 Retopologizing. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .221 High-PolyMeshesandSubdivisionSurfaces .............................. 222 High-PolyMeshesandReal-TimeGames................................. 224 RetopologizinginPractice............................................ 225 Step1:UseBoxModelingtoCreateaLow-PolyStart ....................225 Step2:Subdivide .................................................229 Step3:SculptandSubdivide ........................................231 Step4:Retopologize ..............................................236 UsingDecimate..................................................... 245 Summary.......................................................... 247 Chapter 8 Saved Games and Persistent Data. . . . . . . . . . . . . . . . . . . . . . . . . . . .249 PersistentData ..................................................... 250 PlayerPreferences .................................................. 251 PlayerPreferences:GoingFurther...................................... 252 CustomizingPersistentData .......................................... 254 XMLFiles—orJSONorBinary?......................................... 254 JSON ...........................................................255 Binary ..........................................................256 ClassSerialization................................................... 257 GettingStartedwithXMLSerialization ................................. 257 SavingDatatoanXMLFile ........................................... 260 ReadDatafromanXMLFile .......................................... 261 WorkingwiththeSaveStateClass ..................................... 264 Summary.......................................................... 265