This is a short introduction on how to create and utilize scripts in a project. For detailed information about the Scripting API, please view the Scripting Reference. For detailed information about creating game play through scripting, please view the Creating Gameplay page of this manual. 本篇是一份關(guān)于如何在項(xiàng)目中創(chuàng)建和使用腳本的簡介。要了解關(guān)于腳本的詳細(xì)信息請查看 腳本手冊 。對于通過腳本創(chuàng)建游戲的詳細(xì)資料請查看手冊里的 創(chuàng)建游戲 的頁面。 Scripting inside Unity is done by writing simple behaviour scripts in JavaScript, C#, or Boo. You can use one or all scripting languages in a single project, there is no penalty for using more than one. Unless stated otherwise, all the scripting examples are in JavaScript. Unity中編寫簡單的行為腳本可以通過Javascript、C#或Boo來完成。你可以在一個(gè)項(xiàng)目中使用一種或全部的腳本語言,同時(shí)使用一種或多種腳本語言也是沒有問題的。除非特別指定,所有的有關(guān)腳本的例子都是用Javascript來寫的。(Javascript是官方推薦的腳本,當(dāng)然你使用C#,Boo也是沒有任何問題的。) Creating New ScriptsUnlike other assets like Meshes or Textures, Script files can be created from within Unity. To create a new script, open the Assets->Create->JavaScript (or Assets->Create->C Sharp Script or Assets->Create->Boo Script) from the main menu. This will create a new script called NewBehaviourScript and place it in the selected folder in Project View. If no folder is selected in Project View, the script will be created at the root level. 腳本文件不像網(wǎng)格或紋理資源那樣,可以在Unity中創(chuàng)建。創(chuàng)建一個(gè)新的腳本,你需要從主菜單欄打開 Assets->Create->JavaScript (或Assets->Create->C Sharp Script 或 Assets->Create->Boo Script)。這樣就創(chuàng)建了一個(gè)叫NewBehaviourScript的腳本。這個(gè)文件被放置在項(xiàng)目視圖面板里被選中的文件夾中。如果在項(xiàng)目視圖里沒有選中的文件夾,腳本就被創(chuàng)建在根層級。 You can edit the script by double-clicking on it in the Project View. This will launch your default selected editor in Unity's preferences. You do all your scripting in an external text editor, and not in Unity directly. To set the default script editor, change the drop-down item in Unity->Preferences->External Script editor. 你可以雙擊項(xiàng)目視圖面板的腳本來編輯。這將優(yōu)先啟動(dòng)你的默認(rèn)編輯器。你的所有腳本都在一個(gè)外部編輯器中來做而不是直接在Unity里進(jìn)行。對于默認(rèn)腳本編輯器的設(shè)置,通過下拉菜單Unity->Preferences->External Script editor進(jìn)行。 These are the contents of a new, empty behaviour script: 以下是一個(gè)新創(chuàng)建的空腳本里的內(nèi)容:
A new, empty script does not do a lot on its own, so let's add some functionality. Change the script to read the following: 新建的空腳本本身還不能執(zhí)行許多功能,所以我們需要添加一些功能語句。閱讀下面的內(nèi)容修改腳本:
When executed, this code will print "Hello World" to the console. But there is nothing that causes the code to be executed yet. We have to attach the script to an active GameObject in the Scene before it will be executed. 當(dāng)運(yùn)行程序,這段代碼會(huì)在控制臺(tái)中打印"Hello World"。但這里還沒有任何東西觸發(fā)代碼的執(zhí)行。我們需要在其執(zhí)行前先把這個(gè)代碼附加到場景里的一個(gè)激活的游戲?qū)ο笊稀?/p> Attaching scripts to objects 附加腳本到對象上Save the above script and create a new object in the Scene by selecting GameObject->Create Other->Cube. This will create a new GameObject called "Cube" in the current Scene. 保存上述腳本并在場景中通過選擇GameObject->Create Other->Cube 來創(chuàng)建一個(gè)新對象。這將在當(dāng)前場景里創(chuàng)建一個(gè)叫立方體(Cube)的游戲?qū)ο蟆?/p> Now drag the script from the Project View to the Cube (in the Scene or Hierarchy View, it doesn't matter). You can also select the Cube and choose Component->Scripts->New Behaviour Script. Either of these methods will attach the script to the Cube. Every script you create will appear in the Component->Scripts menu. If you have changed the name of your script, you will that name instead. 現(xiàn)在從項(xiàng)目視圖面板中拖動(dòng)之前的腳本到這個(gè)立方體上(在場景面板或?qū)蛹壝姘宀僮鞫际强梢缘牡模D阋部梢赃x中立方體,然后選擇Component->Scripts->New Behaviour Script。所有這些方法都可以將腳本附加到立方體Cube上。你創(chuàng)建的每一個(gè)腳本都會(huì)出現(xiàn)在Component->Scripts菜單中。如果你改變了腳本的名稱,菜單中的名字也將隨之改變。 If you select the Cube and look at the Inspector, you will see that the script is now visible. This means it has been attached. 這時(shí)你選中立方體Cube并觀察檢視面板,你將會(huì)發(fā)現(xiàn)里面出現(xiàn)了這個(gè)腳本。這就意味著腳本已經(jīng)被附加上了。 Press Play to test your creation. You should see the text "Hello World" appear beside the Play/Pause/Step buttons. Exit play mode when you see it. 按下播放按鈕測試你的場景。你會(huì)在 播放/暫停/單步按鈕旁邊看到"Hello World"。當(dāng)你看到這個(gè)之后退出播放模式。 Manipulating the GameObject 操縱游戲?qū)ο?/h2>A print() statement can be very handy when debugging your script, but it does not manipulate the GameObject it is attached to. Let's change the script to add some functionality: print()語句在你調(diào)試腳本的時(shí)候非常方便。但是這個(gè)腳本還不能操縱附加他的游戲?qū)ο?。讓我們修改腳本添加一些功能。
If you're new to scripting, it's okay if this looks confusing. These are the important concepts to understand: 如果你是腳本新手,這些代碼看起來頭疼是很正常的。以下是一些需要理解的重要概念:
With all this in mind, we can read this code as "every frame, rotate this GameObject's Transform component a small amount so that it will equal five degrees around the Y axis each second." 大腦中有了這些概念后,我們能夠詮釋這段代碼為"每一幀,讓游戲?qū)ο蟮腡ransform組件旋轉(zhuǎn)一個(gè)小的角度,這樣保持這個(gè)對象沿著Y軸每秒旋轉(zhuǎn)5°"。 You can access lots of different Components the same way as we accessed transform already. You have to add Components to the GameObject using the Component menu. All the Components you can easily access are listed under Variables on the GameObject Scripting Reference Page. 你可以使用我們訪問transform成員相同的方式來訪問更多不同的組件。你需要使用組件Component菜單來添加組件到游戲?qū)ο笾?。所有組件你都可以通過游戲?qū)ο竽_本參考頁中的列出的變量列表訪問到。 For more information about the relationship between GameObjects, Scripts, and Components, please jump ahead to the GameObjects page or Using Components page of this manual. 關(guān)于游戲?qū)ο?、腳本和組件之間的關(guān)系的更多信息,請?zhí)D(zhuǎn)到這本手冊里的 游戲?qū)ο?/a> 頁或 使用組件 頁 The Power of Variables 變量的能力Our script so far will always rotate the Cube 5 degrees each second. We might want it to rotate a different number of degrees per second. We could change the number and save, but then we have to wait for the script to be recompiled and we have to enter Play mode before we see the results. There is a much faster way to do it. We can experiment with the speed of rotation in real-time during Play mode, and it's easy to do. 到目前為止我們的腳本讓立方體Cube保持每秒旋轉(zhuǎn)5度。我們希望它能夠每秒旋轉(zhuǎn)不同的角度值。我們可以改變數(shù)值并保持。但是之后我們需要等待腳本被重新編譯。然后我們還必須進(jìn)入到播放模式才能夠看到結(jié)果?,F(xiàn)在有一種更快的方式來實(shí)現(xiàn)。我們可以在播放模式下實(shí)時(shí)試驗(yàn)旋轉(zhuǎn)速度,這將會(huì)很容易做到。 Instead of typing 5 into the Rotate() function, we will declare a speed variable and use that in the function. Change the script to the following code and save it: 我們將要聲明一個(gè)速度speed變量并在函數(shù)中使用這個(gè)變量,而不是在Rotate()函數(shù)中使用5。像下面代碼這樣修改并保存:
Note that this is an example in Javascript. C# users should replace the keyword var with the type int. Now select the Cube and look at the Inspector. Notice how our speed variable appears. 注意這個(gè)例子是Javascript的寫法。C#的用戶需要把var關(guān)鍵字換成int?,F(xiàn)在選中立方體Cube觀察檢視面板。注意看我們的speed變量是如何顯示的。 This variable can now be modified directly in the Inspector, just like renaming a file in the file explorer. Select it, press Return and change the value. You can also right- or option-click on the value and drag the mouse up or down. You can change the variable at any time, even while the game is running. 這個(gè)變量現(xiàn)在可以在檢視面板中直接被修改掉,就像在資源管理器里重命名一個(gè)文件一樣。選中它,按下退格件修改值。你也可以在數(shù)值上右擊或Option點(diǎn)擊并拖動(dòng)鼠標(biāo)向上或向下。你可以再任何時(shí)刻改變變量甚至于游戲運(yùn)行時(shí)。 Hit Play and try modifying the speed value. The Cube's rotation speed will change instantly. When you exit Play mode, you'll see that your changes are reverted back to their value before entering Play mode. This way you can play, adjust, and experiment to find the best value, then apply that value permanently. 點(diǎn)擊播放按鈕并試著修改speed的值。立方體的旋轉(zhuǎn)速度將會(huì)立馬變化。當(dāng)你退出播放模式后,你會(huì)看到改變的值又恢復(fù)到你進(jìn)入播放模式前的值上。這樣你可以播放,調(diào)整和測試來找到最佳的值,然后一直使用這個(gè)值。 Using this method of changing a variable's value in the Inspector means that you can re-use one script on many objects, each with a different variable value. If you attach the script to multiple Cubes, and change the speed of each cube, they will all rotate at different speeds even though they use the same script. 使用這種方式來改變檢視面板里一個(gè)變量的值,這就意味著你能夠重復(fù)使用一個(gè)腳本到許多對象上。每一個(gè)對象都使用不同的變量值。如果你附加腳本到多個(gè)立方體上。然后修改每一個(gè)立方體的speed變量,即便他們使用同一個(gè)腳本,他們會(huì)以不同的速度旋轉(zhuǎn)。 Accessing Other Components 訪問其他組件When writing a script Component, you can access other components on the GameObject from within that script. 當(dāng)編寫一個(gè)腳本組件,你可以在這個(gè)游戲?qū)ο蟮哪_本中訪問其他的組件。 Using the GameObject members 使用游戲?qū)ο蟮某蓡TYou can directly access any member of the GameObject class. You can see a list of all the GameObject class members here. If any of the indicated classes are attached to the GameObject as a Component, you can access that Component directly through the script by simply typing the member name. For example, typing transform is equivalent to gameObject.transform. The gameObject is assumed by the compiler, unless you specifically reference a different GameObject. 你可以直接訪問游戲?qū)ο箢愔械娜魏纬蓡T。在這里你能夠訪問到游戲?qū)ο箢愔械娜砍蓡T的一個(gè)列表。如果任何指定的類作物組件附加到游戲?qū)ο?,你都可以通過在腳本中簡單的輸入成員名稱來直接訪問這個(gè)組件。例如,鍵入transform等價(jià)于gameObject.transform。游戲?qū)ο笫峭ㄟ^編譯器來確認(rèn)的,除非你特別聲明一個(gè)不同的游戲?qū)ο蟮囊谩?/p> Typing this will be accessing the script Component that you are writing. Typing this.gameObject is referring to the GameObject that the script is attached to. You can access the same GameObject by simply typing gameObject. Logically, typing this.transform is the same as typing transform. If you want to access a Component that is not included as a GameObject member, you have to use gameObject.GetComponent() which is explained on the next page. 鍵入this可以運(yùn)行你寫的腳本訪問組件。鍵入this.gameObject獲取對腳本附加的游戲?qū)ο蟮囊?。你也可以通過簡單鍵入gameObject來訪問這個(gè)游戲?qū)ο?。同理,使用this.transform和輸入transform效果一樣。如果你想訪問一個(gè)非游戲?qū)ο蟪蓡T的組件,你需要使用gameObject.GetComponent()關(guān)于這個(gè)的展開討論在下一頁。 There are many Components that can be directly accessed in any script. For example, if you want to access the Translate function of the Transform component, you can just write transform.Translate() or gameObject.transform.Translate(). This works because all scripts are attached to a GameObject. So when you write transform you are implicitly accessing the Transform Component of the GameObject that is being scripted. To be explicit, you write gameObject.transform. There is no advantage in one method over the other, it's all a matter of preference for the scripter. 很多組件在任何腳本中都能夠直接被訪問到。例如:你想要訪問Transform組件的Translate函數(shù),你可以輸入transform.Translate()或者gameObject.transform.Translate()。這樣之所有有效是因?yàn)樗械哪_本被附加到一個(gè)游戲?qū)ο笊?。所以?dāng)你輸入transform你能夠隱性訪問到被腳本化的游戲?qū)ο笾械腡ransform組件。需要明確的是,你要輸入gameObject.transform。不存在一種寫法比其他寫法更有優(yōu)勢,這全憑腳本編寫人員的喜好決定。 To see a list of all the Components you can access implicitly, take a look at the GameObject page in the Scripting Reference. 要了解你能夠隱性訪問的全部組件的列表你可以立刻訪問腳本手冊里的游戲?qū)ο?/a>頁。 Using GetComponent() |
|