官术网_书友最值得收藏!

Create a sample script

Like I have said before, scripting, an important part of Unity projects, is done in C#. I want to add an empty script here, just to show you how its done and what happens if you do so.

In the pane at the bottom, named Assets, right click and create a new folder named Scripts. Although this is not required, it is always a good idea to organize your project in logical folders. This way you can always find your components when you need them. Double-click on that folder to open it and see its contents. It should be empty. Right-click again and select Create -> C# Script. Name it PointerHelper. Unity will add the .cs extension to the file, so you should not do that.

Once you have added this empty script, take a look at the inspector pane on the right side. You will see it is not empty at all--the script contains a class named PointerHelper, derived from MonoBehaviour with two methods in it--Start and Update.

We will dive into this later on, but for now I will tell you that this is common with most scripts. The Start() method is called when the script is first loaded, and the Update() method is called each frame. We will discuss frames later on.

When you have created this script, we need to attach it to an object or an asset in our project. This particular script will later on allow us to create an object that shows the user where they are looking. Therefore, it makes sense to attach it to the camera. To do this, drag the script upward to the hierarchy pane and hover right above Main Camera. If you do this right, you will see that the Camera is selected in a light blue color. If it is a darker shade of blue, you will also see a line underneath the Camera object, stating that this script will be a child item of Camera instead of being a part of it. It needs to be a part of it, not a child.

You can always check whether you attached it correctly by selecting the camera and looking at the inspector pane. At the bottom of that pane, you will see the PointerHelper (Script) component being added to the cameras properties.

We will come back to this script later on.

主站蜘蛛池模板: 木兰县| 和林格尔县| 瑞安市| 邵东县| 寻乌县| 易门县| 延津县| 蓬溪县| 东宁县| 灵宝市| 博客| 武陟县| 石首市| 商洛市| 赤峰市| 锦州市| 民勤县| 威宁| 和林格尔县| 龙南县| 祁东县| 襄樊市| 祁阳县| 五大连池市| 界首市| 突泉县| 农安县| 清流县| 宁都县| 阿荣旗| 天峻县| 泰来县| 高邑县| 桃园县| 巨鹿县| 景泰县| 天门市| 天津市| 汉源县| 兖州市| 海林市|