日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

Using Scripts 使用腳本

 有趣的永 2015-12-24

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 Scripts

Unlike 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)容:

function Update () {
} 

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)容修改腳本:

function Update () {
    print("Hello World");
} 

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ū)ο?。讓我們修改腳本添加一些功能。

function Update () {
    transform.Rotate(0, 5*Time.deltaTime, 0);
} 

If you're new to scripting, it's okay if this looks confusing. These are the important concepts to understand:

如果你是腳本新手,這些代碼看起來頭疼是很正常的。以下是一些需要理解的重要概念:

  1. function Update () {} is a container for code that Unity executes multiple times per second (once per frame).
    function Update(){} 是一個(gè)用來存放Unity需要每秒執(zhí)行多次代碼的容器(每幀執(zhí)行一次)。
  2. transform is a reference to the GameObject's Transform Component.
    transform 是游戲?qū)ο罄锏?a href="http://game./Manual/../Components/class-Transform.html">Transform組件的引用。
  3. Rotate() is a function contained in the Transform Component.
    Rotate()是一個(gè)Transform組件的一個(gè)函數(shù)。
  4. The numbers in-between the commas represent the degrees of rotation around each axis of 3D space: X, Y, and Z.
    在逗號(hào)分割數(shù)字分別代表三維空間中的X,Y,Z軸的旋轉(zhuǎn)角度。
  5. Time.deltaTime is a member of the Time class that evens out movement over one second, so the cube will rotate at the same speed no matter how many frames per second your machine is rendering. Therefore, 5 * Time.deltaTime means 5 degrees per second.
    Time.deltaTime 是Time類的一個(gè)成員。用來指示事件一秒鐘變化的量。所以無論你的機(jī)器一秒鐘播放多少幀,立方體都會(huì)以相同的速度旋轉(zhuǎn)。因此,5*Time.deltaTime的意思是5度/秒。

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。像下面代碼這樣修改并保存:

var speed = 5.0;

function Update () {
    transform.Rotate(0, speed*Time.deltaTime, 0);
}

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ū)ο蟮某蓡T

You 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()
使用GetComponent()

There are many Components which are not referenced directly as members of the GameObject class. So you cannot access them implicitly, you have to access them explicitly. You do this by calling the GetComponent("component name") and storing a reference to the result. This is most common when you want to make a reference to another script attached to the GameObject.

有不少組件沒作為游戲?qū)ο箢惖某蓡T被直接引用。所以你不能直接訪問到他們,你需要明確的訪問他們。你可以調(diào)用GetComponent("組件名稱")來做這件事并把一個(gè)引用存放到結(jié)果中。當(dāng)你想用獲取另外一個(gè)腳本附屬的游戲?qū)ο蟮囊脮r(shí),這是一種常規(guī)做法。

Pretend you are writing Script B and you want to make a reference to Script A, which is attached to the same GameObject. You would have to use GetComponent() to make this reference. In Script B, you would simply write:

假設(shè)你現(xiàn)在正在寫B(tài)腳本這時(shí)你想做一個(gè)A腳本的引用(該腳本附加到同一游戲?qū)ο螅P枰褂肎etCompnonet()來實(shí)現(xiàn)這個(gè)引用。在B腳本中,你這樣寫:

scriptA = GetComponent("ScriptA");

For more help with using GetComponent(), take a look at the GetComponent() Script Reference page.

關(guān)于GetComponent()用法的更多幫助,請查閱 腳本參考手冊 里 GetComponent()的頁面。

Accessing variables in other script Components 訪問其他腳本組件中的變量

All scripts attached to your GameObjects are Components. Therefore to get access to a public variable (and methods) in a script you make use of the GetComponent method. For example:

所有被附加到你的游戲?qū)ο笊系哪_本都是組件。因此,你可以在一個(gè)腳本中使用GetComponent()的方法來獲取對公共變量(和方法)。例如:

function Start () {
   // Print the position of the transform component, for the gameObject this script is attached to
   Debug.Log(gameObject.GetComponent<Transform>.().position);
}

In the previous example the GetComponent<T>. function is used to access the position property of the Transform component. The same technique can be used to access a variable in a custom script Component:

在當(dāng)前這個(gè)例子中GetComponent<T>函數(shù)是用來反問Transform組件中的position屬性的。同樣的技術(shù)可以用來訪問一個(gè)自定義腳本組件中的變量。

(MyClass.js)
public var speed : float = 3.14159;

(MyOtherClass.js)
function Start () {
   // Print the speed variable from the MyClass script Component attached to the gameObject
   Debug.Log(gameObject.GetComponent<MyClass>.().speed);
}

Accessing a variable defined in C# from Javascript
從Javascript訪問C#中定義的變量

To access variables defined in C# scripts the compiled Assembly containing the C# code must exist when the Javascript code is compiled. Unity performs the compilation in different stages as described in the Script Compilation section in the Scripting Reference. If you want to create a Javascript that uses classes or variables from a C# script just place the C# script in the "Standard Assets", "Pro Standard Assets" or "Plugins" folder and the Javascript outside of these folders. The code inside the "Standard Assets", "Pro Standard Assets" or "Plugins" is compiled first and the code outside is compiled in a later step making the Types defined in the compilation step (your C# script) available to later compilation steps (your Javascript script).

要訪問C#腳本中定義的變量,那么在Javascript代碼被編譯之前,編譯器中就必須已經(jīng)存在C#的代碼。Unity在不同的階段執(zhí)行編譯,如在腳本參考Script Compilation部分所述。如果你想創(chuàng)建一個(gè)Javascript能夠使用一個(gè)C#中的類或者變量。請把這個(gè)C#腳本放置到"Standard Assets"標(biāo)準(zhǔn)組件,"Pro Standard Assets"專業(yè)版標(biāo)準(zhǔn)組件或者"Plugins"插件文件夾中,并把Javascript文件放到這些文件夾以外的其他地方。在"Pro Standard Assets"專業(yè)版標(biāo)準(zhǔn)組件或者"Plugins"插件文件夾的代碼會(huì)先編譯,其他地方的代碼之后編譯。確保你的C#腳本中的定義先被編譯好,在下一階段Javascript腳本編譯時(shí)已經(jīng)被定義好了。

In general the code inside the "Standard Assets", "Pro Standard Assets" or "Plugins" folders, regardless of the language (C#, Javascript or Boo), will be compiled first and available to scripts in subsequent compilation steps.

通常在"Pro Standard Assets"專業(yè)版標(biāo)準(zhǔn)組件或者"Plugins"插件文件夾中的代碼,不管是(C#,Javascript還是Boo),都會(huì)先被編譯,這樣確保后續(xù)的編譯腳本中能夠使用。

Optimizing variable access 優(yōu)化變量訪問

In some circumstances you may be using GetComponent multiple times in your code, or multiple times per frame. Every call to GetComponent does a few extra steps internally to get the reference to the component you require. A more efficient approach is to store the reference to the component for example in your Start() function. As you will be storing the reference and not retrieving directly it is always good practice to check for null references:

有些情況下,你可能會(huì)你的代碼中多次使用GetComponent,或者每一幀調(diào)用幾次。每一次調(diào)用GetDComponent函數(shù)內(nèi)部都會(huì)做一些額外的步驟來獲取你需要的組件。一個(gè)更為有效的做法是例如在你的start()函數(shù)中,存儲(chǔ)組件的引用。你這樣存儲(chǔ)引用且不是直接訪問它,這對于檢查空引用也是一個(gè)好的做法。

(MyClass.js)
public var speed : float = 3.14159;

(MyOtherClass.js)
private var myClass : MyClass;
function Start () {
   // Get a reference to the MyClass script Component attached to the gameObject
   myClass = gameObject.GetComponent<MyClass>.();
}
function Update () {
   // Verify that the reference is still valid and print the speed variable
   if(myClass != null)
      Debug.Log (myClass.speed);
}

Static Variables 靜態(tài)變量

It is also possible to declare variables in your classes as static. There will exist one and only one instance of a static variable for a specific class and it can be modified without the need of an instance of a class object:

在你的類中也可以定義靜態(tài)變量。這樣對于這個(gè)特定的類里的靜態(tài)變量就只會(huì)存在一份實(shí)例。這個(gè)靜態(tài)變量可以被修改而不需要實(shí)例化一個(gè)類對象:

(MyClass.js)
static public var speed : float = 3.14159;

(MyOtherClass.js)
function Start () {
   Debug.Log (MyClass.speed);
}

It is recommended to not use static variables for object references to make sure unused objects are removed from memory.

建議在對象引用中不要使用靜態(tài)變量以確保未使用的對象能夠從內(nèi)存中刪除。

Where to go from here 下一步怎么走

This was just a short introduction on how to use scripts inside the Editor. For more examples, check out the Tutorials that come with Unity. You should also read through Scripting Overview inside the Script Reference, which contains a more thorough introduction into scripting with Unity plus pointers into the reference itself for in-depth information. If you're really stuck, be sure to visit the Unity Answers or Unity Forums and ask questions there. Someone is always willing to help.

這僅僅是對于如何在編輯器中使用腳本的一個(gè)簡短教程。更多的例子,請參考來自Unity的教程。你也可以通過閱讀腳本手冊里的 腳本概覽,那里面包含了更多Unity腳本的更深層次的介紹。如果你還是沒找到的話,請?jiān)L問 Unity問答Unity論壇,在哪里提問??傆腥藭?huì)愿意幫你解答。

頁面最后更新: 2011-10-20

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評論

    發(fā)表

    請遵守用戶 評論公約

    類似文章 更多