Lesson 4 - Starting the First Project

Tutorial Series: Introduction to Unity with C# Series

Previous Article  |  Next Article


Transcript

Alright, so hopefully you had no trouble installing those tools that we installed in the previous video. Unity, Visual Studio and the tools extension for Unity. That being the case, let's go ahead and open up Unity for the first time. So here we are, we need to create a new Project so click here where it says "New Project." Let's call this "ProtoDemo." Make sure you click where it says '2D' because it is going to be a 2D project and 'Create project'. I know in the previous video I said we installed everything we need, but actually we have one more thing we need to do. We need to import the Unity tools as an import package into our Project. To do that go to 'Assests', 'Import Package' and 'Visual Studio 2015 Tools' and now click 'Import' to import it into your Project. So, you will have to do this every time you create a new Project. In the future, you won't have to do this at all and I am quite sure because it is all going to be wrapped up into one application installed; but for now we have to do that and that is done.

Now that that is done, let's create a C# script to make sure everything is connected with Visual Studio. We will double-click that and open it and see if everything is fine. In order to that right-click here where it says 'Assets' select 'Create' and then select 'C# Script'. We don't have to name this right now. Simply click off of it and then double-click it and Visual Studio will open up. It is going to be building the "Project Solution" as it is called, which is basically going to be all of the codes wrapped up in a bunch of folders. For the first time you do this, it will probably take a little while, so let it do its thing. Hopefully that didn't take too long and once it opens up, you will see here that our script that we didn't name in Unity but it is called "NewBehaviorScript" by default. It opens up with this bit of preset code here and you can find it in your solution by clicking on 'Assets' and there is your script. The next thing I want to do, as you can see here with this preset code is basically a template that whenever you create a new script this is what gets entered by default. There are a few problems I have with it that I would like to get passed, by creating a new template and replacing it. A few things are the braces here and the spacing between the parenthesis and the method names and a few other things that I like to clean up, not terribly important.

There is also another thing. There is a kind of a bug, where ... because the template was made on a system other than Windows, it might give you a nag screen that says something like "convert line ending." It might just nag you with that over and over again. In order to get past that, let's replace the template. In order to that, let's right-click where ... on the 'Assets' folder here. Click 'Add', 'New Item' and then scroll down to 'Text File' and you don't have to name it. So click 'Add' and then for the text file we are going to want to write everything exactly as you see here. Now, you don't have to do this, if you want you can just download this template from the lesson on the site with the lesson, but if you are going to do it right here, then make sure you type it exactly as I am doing right here. So type in exactly "using UnityEngine;” it is case sensitive so make sure you have the proper casing. "using System.Collections;" "public class #SCRIPTNAME# : MonoBehaviour", enter

A squiggly bracket. "{" Open squiggly bracket, which will also create automatically a "}" closing squiggly bracket. Then [Tab] that and type in "void Start()" enter. Another set of squiggly brackets and "void Update()" squiggly brackets and enter. That is pretty much what we need to do. Now, in order to replace the earlier template, let's right-click on… Sorry should say 'File', ‘Save,’ right here where it says 'Save Assets/Text File', Save As and we will navigate to C:, Program Files, Unity, Editor, Data, Resources, Script Templates and we are done, pretty much. We are going to want to save over this one here, but actually in order to save this you might have a little bit of trouble. What we might need to do is right-click and set the permissions for the script templates folder. Go to 'Security' after you right-click it and select your 'Users' account ... click 'Edit' and… On your User account allow 'Full control', hit 'Apply', click 'OK', go into the folder, right-click on this script preset, 'Properties' same thing. We will do it again.

Select your account and make sure it has full control, click 'OK' and now should let us save over it. Okay, excellent. Once again, this isn't necessary, it just helps declutter a little bit and make it a little bit more clean. If you can't do this don't worry about it, don't sweat it, just move forward. It is not going to make much of a difference. Right now, that is about all I want to do with that. Now we don't need any reference to any of these files, so just right-click and delete or says 'Remove'. Click, delete. There we go, we have no more scripts. Let's go back to Unity and there you go we shouldn't have any files. That's how it communicates, Unity and Visual Studio communicate together.

The next thing I would like to do is sort of get familiar with Unity by setting a scene, creating a scene, which is basically, you can think of it as level. Also we will set the camera and resolution so that we can start prototyping a game in the next video. The next thing I want you to do ... in Unity is create a game object. A game object is basically the most basic building block to anything that appears in your game. Even if it is invisible. Basically anything you can think of that appears in a game, it can be a score, a health meter, your player, one character, enemies background, whatever it may be. In order to create a game object, is very simple. All you do is click on 'Game Object' here and you can create an empty object and it will insert it in the left hand side here in the hierarchy pane.

Right now we have the game object that we just created as well as the camera game object, that is also a game object. When I click on the game object, you will notice that right here in the 'Inspector' that these will be the components that are attached to the game object. By default, you get a 'Transform' component, which I will show you what that is all about in a minute. For our game idea, we will want to have some sort of dummy character or dummy asset that we can use just a ...Put something that will be visible on our screen. Actually let's get rid of this game object we won't need that. Just right-click and delete. Instead, what I want to do is go to: Game Object, 3D Object. Even though it is 2D, we can still import 3D objects. We will just be seeing it on a 2D plane. So '3D Object' and select 'Cube'. As you can see here we got our cube, which will look like a square because it is visible from a 2D plane in our game world, in our scene. There you can switch between "Scene" and "Game". Game would be a preview of where you will be at when you press play and enter the game as it were, and scene would be sort of like an overview of your entire level. Here you can click on "2D" and "3D" to see your game in two dimensional perspective or 3D if you want for...Perhaps might be useful for arranging things but, we will be sticking with 2D. When we click on the "Cube" game object here.

Look at all of its attached components. You don't have to know what all this is. Eventually you will know what it is, but this is basically what a preset cube comes with it. Comes with a transform, which now click on "Game" here to show you. The transform determines its place in the game world, at least over here in the inspector that transform determines its place before the... A script or the game is played, it started and you can determine its position simply by clicking and dragging the position X, position Y coordinates. You will notice there is also the Z position, which is the depth plane, but since we are in a two dimensional game world, that doesn't really translate as depth. Instead you can use it as a way of layering objects in front or behind others, but actually there is a much better way of doing this and probably won't have to use it. Don't worry about the Z plane pretty much at all.

You can also rotate your objects, which probably has not much meaning in this case either, right now with our little square. You can scale it. You can scale the X and the Y independently which gives the effect of re-sizing the square. You may notice also that right now we have a sort of navy blue background. "Where is that coming from?" Well, it is a part of the camera. You can determine the background here if you want to and get a different color. Let's stick with ...I don't know bluish hue ... something like we started off with. Alright. The background doesn't really matter when it is set on the camera. Chances are, you will be using an Asset to represent the background, but in this case when there is...In absence of any Asset for the background, the camera color is what shows through.

Right now what we want to do also is set the resolution of our game. Right now it is in 'Free Aspect', basically what that means is that it will expand and contract depending on the monitor size or the viewport of whatever device you end up publishing this on, right? But to make things a lot easier and to keep track of everything in our game world, we should make a fixed screen resolution so a popular screen resolution is '1280x720'. So let's use that and call it fixed and call it ...label it "HD". That is now our resolution for our game world. Next thing we will want to do is set our camera size to 3.6. I will explain more about what this represents, but it is kind of important in setting your camera relative to your resolution. So with that set, go back to our cube. You see you can send the cube off the screen. That is perfectly possible because really what is happening is it is going outside of the bounds of our camera. When we press play in our game, you will notice that the cube is cut-off because it is outside of the bounds of the camera. Also, if we hit maximize on play, we will see that the game runs at the full 1280x720P resolution. If we were running the actual build, it should stretch to fill out the entirety of the screen. So that is why it is important to set our resolution to start with. Now that we have kind of set our scene for our little prototype game project, it is important to save the scene. So we will save the scene as "ProtoScene". Save that, and then we will also need to save the Project separately. Click File and Save Project and we are done. Alright, so that is about it for this video.

Not a whole lot in this video but in the next one we will go right in to our game prototype. It will be a lot of fun, I look forward to seeing you there.


Related Articles in this Tutorial:

Lesson 1 - Who This Course is For

Lesson 2 - What to Expect from this Course

Lesson 3 - Installation and Getting Started

Lesson 4 - Starting the First Project

Lesson 5 - Prototype Workflow

Lesson 6 - Basic Code Review

Lesson 7 - Game Loop Primer

Lesson 8 - Prototyping Continued

Lesson 9 - C# Fundamentals and Hello World

Lesson 10 - Variables and Operations

Lesson 11 - Variables and Operations Continued

Lesson 12 - Floats, Bools and Casting

Lesson 13 - If Statement Conditionals

Lesson 14 - If Statements Continued

Lesson 15 - Complex Evaluations and States

Lesson 16 - Code Syntax vs. Style

Lesson 17 - Variable Scope

Lesson 18 - Object-Oriented Programming Intro

Lesson 19 - OOP, Access Modifiers, Instantiation

Lesson 20 - Object Containment and Method Returns

Lesson 21 - "Has-A" Object Containment

Lesson 22 - "Is-A" Inheritance Containment

Lesson 23 - Static Fields and Methods

Lesson 24 - Method Inputs and Returns

Lesson 25 - Reference vs. Value Types

Lesson 26 - Introduction to Polymorphism

Lesson 27 - Navigating the Unity API

Lesson 28 - Applying What You've Learned and Refactoring

Lesson 29 - Constructors, Local Variables in the Update Method

Lesson 30 - Collecting Collectibles, Items and Powerups

Lesson 31 - Spawning and Managing Prefab Powerups

Lesson 32 - Implementing Powerup State Logic

Lesson 33 - Displaying Text, OnGUI, Method Overloading

Lesson 34 - Referencing Instantiated GameObjects, Parenting

Lesson 35 - Understanding the Lerp Method

Lesson 36 - Creating Pseudo Animations in Code

Lesson 37 - Understanding Generic Classes and Methods

Lesson 38 - Animations Using SpriteSheets and Animator

Lesson 39 - Working with Arrays and Loops

Lesson 40 - Debugging Unity Projects with Visual Studio

Lesson 41 - Camera Movement and LateUpdate

Lesson 42 - Playing Audio Clips

Lesson 43 - Routing Audio, Mixers and Effects

Lesson 44 - Adding Scoring Mechanics and Enhancements

Lesson 45 - Scene Loading and Game Over Manager

Lesson 46 - Understanding Properties

Lesson 47 - Controller Mapping and Input Manager

Lesson 48 - Understanding Enums

Lesson 49 - Dealing with Null References

Lesson 50 - Handling Variable Framerates with time.DeltaTime

Lesson 51 - Preparing the Project for Final Build

Lesson 52 - Final Build and Project Settings

Lesson 53 - Introduction to the Unity Physics Engine

Lesson 54 - Understanding FixedUpdate vs. Update

Lesson 55 - Movement Using Physics

Lesson 56 - Attack Script and Collision Events with OnCollisionEnter2D

Lesson 57 - Projectiles and Stomping Attack

Lesson 58 - Parallax Background and Scrolling Camera

Lesson 59 - Infinitely Tiling Background Sprites

Lesson 60 - OOP Enemy Classes

Lesson 61 - OOP Enemy Classes Continued

Lesson 62 - Trigger Colliders and Causing Damage

Lesson 63 - Multi-Dimensional Arrays and Procedural Platforms

Lesson 64 - Finishing Touches

Lesson 65 - Series Wrap


Comments

Please login or register to add a comment