Learning C# by Developing Games with Unity C # P R O G R A M M I N G F O R U N I T Y G A M E D E V E L O P M E N T by Emma William For information contact : (alabamamond@gmail.com, memlnc) http://www.com 2 nd Edition: 2020 Learning C# by Developing Games with Unity Copyright © 2020 by Emma William “The “joy of discovery” is one of the fundamental joys of play itself. Not just the joy of discovering secrets within the game, but also the joy of uncovering the creator’s vision. It’s that “Aha!” moment where it all makes sense, and behind the world the player can feel the touch of another creative mind. In order for it to be truly joyful, however, it must remain hidden from plain view—not carved as commandments into stone tablets but revealed, piece by piece, through the player’s exploration of the game’s rules.” ― Derek Yu, Spelunky.
Who this book is for? If you don't know anything about programming in general, writing code, writing scripts, or have no idea where to even begin, then this book is perfect for you. If you want to make games and need to learn how to write C# scripts or code, then this book is ideal for you. memlnc Who this book is for?.3 INTRODUCTION___________________________________________________13 2D OR 3D PROJECTS………………………………………………………………………………………….16 2D gameplay with 3D graphics………………………………………………………………………….17 Why do we use game engines? ………………………………………………………………………… 19 Quick steps to get started with Unity Engine…………………………………………………….20 CHAPTER 1_______________________________________________________25 The language C #………………………………………………………………………………………………26 syntax…………………………………………………………………………………………………… ………….28 Comments……………………………………………………………………………………………… …………28 variables………………………………………………………………………………………………… …………29 Naming conventions…………………………………………………………………………………………29 Data types…………………………………………………………………………………………………… ….30 Variable declaration with initialization…………………………………………………………….32 Boolean variable………………………………………………………………………………………………33 Keyword var…………………………………………………………………………………………………….33 Data fields / array…………………………………………………………………………………………….33 Create arrays…………………………………………………………………………………………………… 34 Access to an array element………………………………………………………………………………35 Determine the number of all array items…………………………………………………………35 Multidimensional arrays………………………………………………………………………………….36 Constants……………………………………………………………………………………………… …………37 Enumeration…………………………………………………………………………………………… ………38 Type conversion……………………………………………………………………………………………… 38 Ramifications…………………………………………………………………………………………… …….40 if statements……………………………………………………………………………………………… ……40 switch statement……………………………………………………………………………………………… 41 grind…………………………………………………………………………………………………… ……………43 for loop…………………………………………………………………………………………………… ………43 Negative step size……………………………………………………………………………………………44 Break…………………………………………………………………………………………………… ………….45 Foreach loop…………………………………………………………………………………………………… 46 while loop…………………………………………………………………………………………………… ….47 do loop…………………………………………………………………………………………………… ………48 Classes………………………………………………………………………………………………… ………….49 Assign components by code…………………………………………………………………………… 50 Instantiation of non-components…………………………………………………………………….52 Don't repeat yourself………………………………………………………………………………………54 Value types and reference types…………………………………………………………………….56 Local and global variables……………………………………………………………………………… 56 Prevent confusion with this…………………………………………………………………………… 57 Access and visibility ……………………………………………………………………………………….57 Static classes and class members………………………………………………………………….59 Parameter modifier out / ref…………………………………………………………………………61 Array passing with params…………………………………………………………………………… 63 Properties and property methods…………………………………………………………………65 Inheritance……………………………………………………………………………………………… ……68 Base class and derived classes………………………………………………………………………………69 Inheritance and visibility……………………………………………………………………………………….70 Override inherited method……………………………………………………………………………………72 Access to the base class…………………………………………………………………………………………73 Seal classes………………………………………………………………………………………………… …………74 Polymorphism………………………………………………………………………………………… …………….75 Interfaces……………………………………………………………………………………………… ………………76 Define interface………………………………………………………………………………………………… ….77 Implement interfaces……………………………………………………………………………………………78 Support from MonoDevelop………………………………………………………………………………….80 Access via an interface………………………………………………………………………………………….81 Generic classes and methods……………………………………………………………………………….84 Sort list objects………………………………………………………………………………………………… ….87 CHAPTER 2________________________________________________________90 Script programming…………………………………………………………………………………………….90 MonoDevelop………………………………………………………………………………………… ……………91 Help in MonoDevelop…………………………………………………………………………………………. 92 Syntax error…………………………………………………………………………………………………… ……92 Forwarding of error messages…………………………………………………………………………….93 Usable programming languages………………………………………………………………………….94 Unity's inheritance structure………………………………………………………………………………94 Object…………………………………………………………………………………………………… ………………….97 Transform……………………………………………………………………………………………… …………………97 Behavior………………………………………………………………………………………………… …………………97 MonoBehaviour………………………………………………………………………………………… …………….98 Create scripts…………………………………………………………………………………………………… ………98 Rename scripts…………………………………………………………………………………………………… ……100 Rename scripts…………………………………………………………………………………………………… ……100 The script framework……………………………………………………………………………………………….
101 Unity's event methods…………………………………………………………………………………………….102 Update………………………………………………………………………………………………… …………………103 FixedUpdate…………………………………………………………………………………………… ………………104 Change time interval……………………………………………………………………………………………….107 Component programming………………………………………………………………………………………108 Access GameObjects…………………………………………………………………………………………… …109 FindWithTag…………………………………………………………………………………………… …………….112 Activate and deactivate GameObjects………………………………………………………………….113 Destroy GameObjects…………………………………………………………………………………………… 113 Create GameObjects…………………………………………………………………………………………… …….116 Reduce traffic…………………………………………………………………………………………………… ………117 SendMessage…………………………………………………………………………………………… ………………117 Variable access…………………………………………………………………………………………………… ……119 Add components…………………………………………………………………………………………… …………120 Remove components…………………………………………………………………………………………… ….120 Activate and deactivate components……………………………………………………………………….121 Random values…………………………………………………………………………………………………… ……121 Execute code in parallel……………………………………………………………………………………………124 WaitForSeconds………………………………………………………………………………………… …………….126 Delayed and repeating function calls with Invoke…………………………………………………….127 Invoke………………………………………………………………………………………………… ……………………127 InvokeRepeating, IsInvoking and CancelInvoke……………………………………………………….128 Save and load data………………………………………………………………………………………………….131 save data……………………………………………………………………………………………………… …………131 Check the key……………………………………………………………………………………………………… ….133 Save…………………………………………………………………………………………………… ……………………134 Cross-scene data…………………………………………………………………………………………………….1 34 Passing values with PlayerPrefs……………………………………………………………………………….135 Use start menus for initialization…………………………………………………………………………….139 DontDestroyOnLoad as a singleton…………………………………………………………………………141 Debug class…………………………………………………………………………………………………… ……………142 Compilation order…………………………………………………………………………………………………… ….145 CHAPTER______________________________________________________________147 Create a simple 3d game…………………………………………………………………………………………….147 Level design…………………………………………………………………………………………………… ……………149 General import settings……………………………………………………………………………………………….155 Create floor…………………………………………………………………………………………………… ……………155 Create walls…………………………………………………………………………………………………… ……………156 Create ceiling………………………………………………………………………………………………… ……………157 Add fire and light…………………………………………………………………………………………………… ….158 Create decoration……………………………………………………………………………………………… ………159 Create an inventory system…………………………………………………………………………………………162 Management logic…………………………………………………………………………………………………… …162 Interface of the inventory system………………………………………………………………………………171 HoverEffects…………………………………………………………………………………………… …………………174 Default cursor…………………………………………………………………………………………………… ……….177 Water tank…………………………………………………………………………………………………… ……………178 Game controller……………………………………………………………………………………………… ……….182 Create players………………………………………………………………………………………………… …………182 Life management…………………………………………………………………………………………… …………184 MessageText…………………………………………………………………………………………… ……………….198 Develop throwing stone…………………………………………………………………………………………210 Configure bursting stone……………………………………………………………………………………….218 Manage experience points…………………………………………………………………………………….219 Create quest givers……………………………………………………………………………………………….221 Quest giver configuration………………………………………………………………………………………222 Create sub- quest………………………………………………………………………………………………….23 2 Place and configure objects………………………………………………………………………………….237 Create Animator Controller………………………………………………………………………………….241 Create a NavMesh……………………………………………………………………………………………… …244 Recognize surroundings and enemies………………………………………………………………….246 Manage health status………………………………………………………………………………………….251 Develop artificial intelligence……………………………………………………………………………….264 Create a starting scene……………………………………………………………………………………….265 Create start menu……………………………………………………………………………………………….266 Start menu with OnGUI………………………………………………………………………………………266 Web player adjustments…………………………………………………………………………………….271 Change web player template………………………………………………………………………………272 Changeover to uGUI………………………………………………………………………………………….2 72 CHAPTER 4_______________________________________________________274 Create a simple 2d game……………………………………………………………………………………275 Special attacks for projectiles………………………………………………………………………….299 INTRODUCTION Unity is a cross-platform development platform initially created for developing games but is now used for a wide range of things such as: architecture, art, children's apps, information management, education, entertainment, marketing, medical, military, physical installations, simulations, training, and many more.Unity takes a lot of the complexities of developing games and similar interactive experiences and looks after them behind the scenes so people can get on with designing and developing their games. These complexities include graphics rendering, world physics and compiling. More advanced users can interact and adapt them as needed but for beginners they need not worry about it.Games in Unity are developed in two halves; the first half -within the Unity editor, and the second half -using code, specifically C#. Unity is bundled with MonoDevelopor Visual Studio 2015 Communityfor writing C#.
2D OR 3D PROJECTS Unity is equally suited to creating both 2D and 3D games. But what’s the difference? When you create a new project in Unity, you have the choice to start in 2D or 3D mode. You may already know what you want to build, but there are a few subtle points that may affect which mode you choose.The choice between starting in 2D or 3D mode determines some settings for the Unity Editor -such as whether images are imported as textures or sprites. Don’t worry about making the wrong choice though, you can swap between 2D or 3D mode at any time regardless of the mode you set when you created your project.
Here are some guidelines which should help you choose. Full 3D 3D games usually make use of three-dimensional geometry, with materials and textures rendered on the surface of these objects to make them appear as solid environments, characters and objects that make up your game world. The camera can move in andaround the scene freely, with light and shadows cast around the world in a realistic way. 3D games usually render the scene using perspective, so objects appear larger on screen as they get closer to the camera.
For all games that fit this description, start in3Dmode. Orthographic 3D Sometimes games use 3D geometry, but use an orthographic camera instead of perspective. This is a common technique used in games which give you a bird’s-eye view of the action, and is sometimes called “2. If you’re making a game like this, you should also use the editor in3Dmode, because even though there is noperspective, you will still be working with 3D models and assets.
You’ll need to switch yourcameraandscene viewtoOrthographicthough.(scenes above fromSynty StudiosandBITGEM) Full 2D Many 2D games use flat graphics, sometimes called sprites, which have no three-dimensional geometry at all. They are drawn to the screen as flat images, and the game’s camera has no perspective. For this type of game, you should start the editor in 2D mode. 2D gameplay with 3D graphics Some 2D games use 3D geometry for the environment and characters, but restrict thegameplayto two dimensions.
For example, the camera may show a “side scrolling view” and the player can only move intwo dimensions, but game still uses 3D models for the obstacles and a 3D perspective for the camera. For these games, the 3D effect may serve a stylistic rather than functional purpose. This type of game isalsosometimes referred to as “2. Although the gameplay is 2D, you will mostly be manipulating 3D models to build the game so you should start the editor in 3D mode.
Perhaps the gaming industry is one of the most difficult industries in this era, and that is in many ways that start with technical challenges, passing through an audience that is difficult to satisfy and ruthless even for the major companies if their products are not at the required level, and not an end to fierce competition and high failure rates and the difficulty of achieving profits that cover high production costs. On the other hand, there are features of this industry that make survival in it possible. On the technical side, for example, the vast majority of games are not free of similar functions and repetitive patterns of data processing, which makes the reuse of the software modules of previous games in order to create new games possible. This, in turn, contributes to overcoming technical obstacles and shortening time and effort.
When you talk about making a game, you are here to mention the big process that involves dozens and possibly hundreds of tasks to accomplish in many areas. Making a game means producing, marketing, and publishing it, and all the administrative, technical, technical, financial, and legal procedures and steps involved in these operations. However, what is important for us in this series of lessons is the technical aspect which is game development, which is the process of building the final software product with all its components. This process does not necessarily include game design, as the design process has a broader perspective and focuses on such things as the story, the general characteristic of the game, the shapes of the stages and the nature of the opponents, as well as the rules of the game, its goals and terms of winning and losing.
Returning to the game development process, we find that many specializations and skills contribute to this process. There are painters, model designers, animation technicians, sound engineers, and director, in addition to - of course - programmers. This comprehensive view is important to know that the programmer's role in producing the game is only an integral role for the roles of other team members, though this image is beginning to change with the emergence of independent developers Indie Developers who perform many tasks besides programming. Why do we use game engines? If we wanted to talk in more detail about the role of programmers in the games industry, we will find that even at the level of programming itself there are several roles that must be taken: there are graphics programming and there are input systems, resource import systems, artificial intelligence, physics simulation and others such as sound libraries and aids.
All of these tasks can be accomplished in the form of reusable software modules as I mentioned earlier, and therefore these units together constitute what is known as the Game Engine. By using the engine and software libraries that compose it, you are reducing yourself to the effort needed to build an I / O system, simulate physics, and even a portion of artificial intelligence. What remains is to write the logic of your own game and create what distinguishes it from other games. This last point is what the next series of lessons will revolve around, and although the task seems very small compared to developing the entire game, it is on its smallness that requires considerable effort in design and implementation as we will see.
Quick steps to get started with Unity Engine If you did not have previous experience with this engine, you can read this quick introduction, and you can skip it if you have dealt with this engine previously. I will not elaborate on these steps since there are many lessons, whether in Arabic or English, that you take, but here we are to make sure that each series reader has the same degree of initial knowledge before starting. The first step: download and install the engine To download the latest version of the engine, which is 19, go directly to the website http://unity3d.com and then download the appropriate version for the operating system that you are using, knowing that the free version of the engine has great potential and it meets the purpose for our project in this series of lessons.