1.5 Learn Unity game development from 0 - create the first object

Before officially starting to write code, we need to understand the basic workflow of Unity development. Unlike other tutorials, I will focus more on how to achieve our goal, so we are not limited to writing code, we also need to do game planning. Otherwise, just learning the code will not be able to understand the whole picture of the whole thing.

give yourself a goal

First set yourself a desired content as a goal. Since we are beginners, we hope to display something in the game. Anything is fine, and there is no need to support any player control.

create scene

Although our new project automatically includes a Scene, in order for us to understand the ins and outs of the whole thing, we don't use it. Before doing it, we need to understand Unity's division of the game world.

In the process of game development, our game content may be more and more, such as N maps, N types of gameplay, etc. The simple value thinks that these need to be divided by a concept, like each person is responsible for different The development of the map, or the game only loads the resources of one map at a time.

Based on such a concept, Unity has a built-in concept of game scene. Scene is the translation of Scene. A map mentioned above can be a Scene, and a gameplay can be a Scene. For programmers, Scene is actually a class, and some new members can be added to this class to carry content. If it is an early game engine, we can of course write code to achieve these functions.

We know that we need to create a new Scene instance in the code. In the classic C++, a new Scene is created. But from here, the operation of Unity will be different. Creating a new Scene depends on creating a resource file of type Scene. The resource file is obviously managed by the Project window, so we also right-click any directory in the Project window and select Create->Scene

Create a resource of type Scene

I created it directly in the Assets directory, it is called Demo

It can be seen that what we create is a file that actually exists on the disk, and this kind of resource creation instead of writing code will be flooded inside the engine in the future, which is also one of the characteristics of the modern engine workflow.

open scene

After the creation is complete, we can directly double-click it to open it (of course, double-click under the Project window of the engine), and then Unity will open it (if you changed the default Scene content of the project before and did not save it, then open the Demo Scene You will be prompted whether to save or not, you can choose whatever you want), in the Hierarchy window, we can see that the name of the root of this tree node has changed to Demo, and the icon is very similar to the icon of the Scene resource file .

Here you can confirm that the scene you are currently opening is a demo.

That is to say, the Hierarchy window actually represents the content of the scene itself.

The scene comes with its own file by default

It can be found that there are Main Camera and Directional Light under this Demo.

This is the first unspoken rule we encountered under Unity, that is, the scene is created. Because we chose a 3D game project, Unity defaults that these two things are basically what we need, and basically they are. in this way.

But let’s ignore the role of these two things first. Here we simply say that the unspoken rules inside the engine are everywhere. It may be uncomfortable when you start learning, but you can accept it after you understand it.

Create the first thing in the scene

Remember our goal, we need to create any object that can be seen to be displayed, the reason for creating a scene in Unity is that all the content that needs to be displayed is based on the objects contained in the scene you are currently displaying to deal with.

So we need to create our own objects in the Demo scene.

Although it has been said above, our scene comes with two guys by default after it is created, but ignore them first.

Similar to creating resource project-level resources in the Project window, if we need to create resources belonging to this scene in the scene, then we also need to right-click on a window, as mentioned above, the Hierarchy window represents the content of the scene itself, Then we naturally need to right-click in the Hierarchy window. Of course, you can also choose to right-click on any tree node. If you right-click in the blank space, it will directly belong to the child node of the root node. The child nodes of this node.

Here we choose to right-click on the blank space of the Hierarchy window and select 3D Object->Cube. You may ask why there is no Create? I guess it may be because this operation will be used very frequently in the future, and one layer of menus can be saved to speed up the development efficiency to a certain extent.

After the creation is complete, you can actually understand that this Cube is indeed a real Cube, and we can also notice that it is displayed on the right.

It seems that our task for this article has been completed? Clearly there are still many questions to be answered.

Scene window

I also said earlier that the Hierarchy window is actually the data structure we use to see the objects inside the Scene, then the Scene window is the effect of the images rendered by these objects, which are updated in real time, that is to say, you are in the Hierarchy window The addition and deletion operations in it will be fed back to the Scene window in real time for display.

Wait, isn't the Game window also the content of the Scene?

If you are playing with the editor, when you switch to the Game tab, you can see what appears to be a side view of the block

It is true that the Game window also displays the rendered effect of the content of the current Scene, but the viewing angle is different from that of the Scene, so why is the viewing angle different?

in-game camera

Here we need to involve the concept of the camera. Obviously, in a 3D game, or even a 2D game, the game perspective will not remain unchanged, and the perspective is directly related to which game content is finally displayed on the screen. Objects that are outside the line of sight will not be displayed.

Therefore, any screen display effect in the game must be based on a certain camera. It is nonsense to talk about the final rendering result without a camera.

So what in the scene represents what we call the camera? By the way, it is one of the two objects brought by default after the aforementioned scene is created: Main Camera.

This English translation means the main camera, and this camera determines the perspective of the scene you observe under the Game window .

Wait, what about the Scene window?

The Scene window is not subject to the setting of the camera object in the scene. It is a separate built-in camera. After switching to the Scene window, press and hold the right mouse button inside the window, then slide the mouse, or press wasd, and you will find that you can directly control The perspective of the Scene window is the same as playing a first-person shooter game!

next chapter

This chapter has been written for a long time, so digest it first, and you may have a big question here:

Why is the camera in the Scene window not the Main Camera inside the Scene itself? Why design two perspectives of Game and Scene?

In the next chapter, we will explain in detail the standard configuration of modern game engines around this issue: the distinction between editing time and runtime.

Want to write code? It's still early (laughs)

Guess you like

Origin blog.csdn.net/z175269158/article/details/129832145