10
Jan

4. Introduction to Unity interface – Writing you first code on unity engine in visual studio


Welcome again, are you excited to make your
hand dirty with c sharp code! No more waiting! start directly by opening
unity hub from the desktop. Let’s begin by creating new project for our
game. Click on new, and choose the unity version,
in which you want to develop your game. If you want to install different unity version,
then you can install it from Install Tab. Unity provides lot’s of template, to start-up
your project. For now we, will not mesh up the settings,
and leave it to default 3D template. Because we are starting with the console based
game. Now, you have to provide suitable project
name, or the name of game, from here. I will give my project name as First Code,
and it’s sufficient for the purpose of this lecture. If you want to change, the physical location
of your project then, you can change it from here. Arranging your files and folder is most important.
It helps us to speed up our work. I will create a new folder and rename it to
unity games, open the newly create folder, and select it. That’s it, we have created a new location
to save our project files. It’s time to create our project. Unity will take time to load packages, scripts,
and other necessary resources to start your project. After the loading is completed, you will end-up
with the unity windows, like this. Before starting or meshing up, let’s have
a quick look over the interface of unity. In unity, the main editor window is, made
up of tabbed windows, which can be rearranged, grouped, detached and docked. This means that, the look of the editor can
be different from one project to the another, and one developer to the another, depending
on personal preference, and what type of work you are doing. There are 5 most common and useful windows
in unity and the default position is like this. The first one is Toolbar. It is located at the top most part of the
unity window. It contain the selection tools, and play pause
buttons for playing or testing games. The toolbar is not a window, and is the only
part of the Unity interface that you can’t rearrange. The second one is Hierarchy window. it is located at the left part of unity window,
and it contain all the game object containing in the scene view. The third one is Project window. It is located at the button part of unity
window. The Project Window displays your library of
assets, that are available to use in your project. When you import assets into your project,
they appear here. So it contain all our assets, files and folder,
materials that we have created, c sharp script files, and much more. It is used to arranging our project files
and folder in systematic manner. At the right side of project windows we will
find console tab. From where we can debug our code or game. The next one is Scene view. The Scene View allows you to visually navigate
and edit your scene. The scene view can show a 3D or 2D perspective, depending on the type
of project you are working on. Next to the scene view you will find Game
view. When you run your game for testing purpose, then you can test it from game view. And the last one is inspector panel. The Inspector Window allows you to, view and
edit all the properties of the currently selected object. Because different types of objects
have different sets of properties, the layout and contents of the inspector window will
vary. You can drag and resize any windows from mouse. You can also separate the windows, by dragging
the tab. And can re-arrange it anywhere, you feel better. Do mesh it up or play around it to have a
better experience. If you feel you layout become bad, and if
you want it to be as before. Then click on windows menu. At layout, select default. It will restore the default layout of unity
windows. Let us create our first C sharp script. You can create script from project windows. Right click on any empty space in project
windows. From create menu, choose C Sharp script. Remember to change the suitable name of your
script before you hit enter, or create that file. Note, please don’t give spaces in your file
name. I have named my script file as first code. I have provided spaces between first and code,
but it will create problem later on. We will solve out the problem after a few
movement. Now, when you select the script, the preview
will be displayed at the inspector panel. You can open your script by right clicking
on it and click on open. Or you can directly open it by double clicking
on your script file. Then, your script file will open in visual
studio. If it is the first time starting up visual
studio, then you have to setup few thing. Here if you have Microsoft account then use
it to either sign up or login. But for a moment I will skip it. Here you can choose the development setting
and themes you like. Visual studio will take some time to load
project. Now, it is ready to use. In visual studio, we can see some of the code
already written, like void start method, and void update method. We will learn it deep on another lecture. But for now, to demonstrate how it is going
to work, we will write a piece of code. Debug dot log, Welcome to the course. Just forget about all things, like what is
debug dot log, or what is this bracket used for. We will learn all this in our upcoming videos. After you have made changes to your script
or file. The asterisk sign will appear at the title of your file name. Which means that your file has not been saved. You can save your file from here. Or you can
directly press Control + S from keyboard. After saving the code jump back to unity. To run the script, it must be attached to
any of the game object in the scene. Game object are nothing but all the objects
that is appear in your scene view or the game. Hierarchy contain all the game object in our
project. Currently we have two game object, they are
main camera, and the directional light. Now, we have to attach our script to any of
these two game object. You can attach script to game object, by dragging
script to game object in Hierarchy windows. Or you can select the game object, and drag
the script to the inspector panel, where current properties of the selected game object is
displayed. We got a problem here. We can’t add our script to the game object. It is because, while naming our script, there
is space between two words. So, for any naming in script, we can’t use
white space. And let’s have a look on our code. Our class
with name First Code doesn’t contain spaces. But we have spaces in the file. So we can resolve it by removing spaces from
our file, Like this. Now, I will attach the script to main camera. And the error doesn’t occur. We can view our attached script in inspector
panel by selecting the game object. If you have mistakenly attached script to
wrong game object, then you can remove it from here, and re-attach again. It’s time to run our code. From the toolbar click on play icon to run
your game. Here, scene view is automatically changed
to game view. But it seems like nothing is happened. It’s because we have written a piece of code
that run on console. We will move to console tab. Here we can see the output that we have written
in a code. After then, you can stop your game, by clicking
on the same play button at the toolbar. You can clear the output of console by clicking
clear button here. Clear on play button will clear all the output,
displayed before while playing the game, and provide the new output only. Have you done upto here, then great! You have return your first code in unity. if you have any difficulties then comment
below. And please don’t forgot to like share and
subscribe code flow to get more interesting content in future.

Tags: , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , ,

There are no comments yet

Why not be the first

Leave a Reply

Your email address will not be published. Required fields are marked *