C#寫unity3d的腳本需要注意

轉自:http://tank2308635.iteye.com/blog/1254431

 

Writing Scripts in C#
   使用C#寫腳本

Apart from syntax, there are some differences when writing scripts in C# or Boo. Most notable are:
除了語法外,使用C#或Boo會有一些差別,最明顯的是:

1. Inherit from MonoBehaviour
繼承之MonoBehaviour類

All behaviour scripts must inherit from MonoBehaviour (directly or indirectly). This happens automatically in Javascript, but must be explicitly explicitly inside C# or Boo scripts. If you create your script inside Unity through the Asset -> Create -> C Sharp/Boo Script menu, the created template will already contain the necessary definition.
所有的行爲腳本代碼必須繼承之MonoBehaviour類(直接或間接)。如果使用的是javascript的話會自動(隱性)的繼承,如果使用的是 C#或Boo就必須明確地指定其繼承於MonoBehaviour。如果你是在u3d中通過“Asset->Create->C Sharp Script/Boo Script”來創建了腳本代碼文件的話,u3d的腳本創建模板將會提前將相關繼承語句定義在腳本代碼文件中。

public class NewBehaviourScript : MonoBehaviour {...}     // C#

class NewBehaviourScript (MonoBehaviour): ...    # Boo


2. Use the Awake or Start function to do initialisation.
使用Awake或Start方法進行初始化。

What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.
,你(需要)在C#或Boo在使用Awake或Start方法。

The difference between Awake and Start is that Awake is run when a scene is loaded and Start is called just before the first call to an Update or a FixedUpdate function. All Awake functions are called before any Start functions are called.
Awake和Start之間的區別在於:Awake是當一個場景調入過程完成後會自動運行,而Start則是會在Update或FixedUpdate方法被第一次調用之前被運行。所有的Awake方法運行的優先級會高於任意的Start方法。



3. The class name must match the file name.
(文件中)主類名必須與文件名相同。

In Javascript, the class name is implicitly set to the file name of the script (minus the file extension). This must be done manually in C# and Boo.
在javascript腳本文件中,u3d雖然沒有明確地定義主類,但事實上,u3d已經隱性地自動定義了主類,並將類名設置爲等於腳本文件名(不包括擴展名)。
如果使用的是C#a或Boo腳本,那就必須得手動的將主類名設置爲與文件同名。

4. Coroutines have a different syntax in C#.
使用C#實現協同,在語法上會有一處不同。

Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield
(U3D中的)協同會(同時)用一個屬於IEnumerator接口類型(枚舉)的返回值和你使用的yield 返回值...;來替代yield......;
如下面代碼:

using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine
IEnumerator SomeCoroutine () {
// Wait for one frame
yield return 0;

// Wait for two seconds
yield return new WaitForSeconds (2);
}
}

5. Don't use namespaces.
不要使用命名空間。
Unity doesn't support placing your scripts inside of a namespace at the moment. This requirement will be removed in a future version.
U3D目前不支持你在腳本中使用命名空間,這個需求會在未來的版本中實現。


6. Only member variables are serialized and are shown in the Inspector.
只有(public公有的)成員變量是可以在U3D程序的Inspector欄中會被以序列形式顯示出來

Private and protected member variables are shown only in Expert Mode. Properties are not serialized or shown in the inspector.
私有類型(private)和成員類型(protected)變量只能在專家模式(Expert Mode)下可見,(而且)屬性(Properties)

7. Avoid using the constructor.
避免使用構造函數

Never initialize any values in the constructor. Instead use Awake or Start for this purpose. Unity automatically invokes the constructor even when in edit mode. This usually happens directly after compilation of a script, because the constructor needs to be invoked in order to retrieve default values of a script. Not only will the constructor be called at unforeseen times, it might also be called for prefabs or inactive game objects.
不要通過構造函數來初始化變量。這些工作可以使用第2條中的Awake方法和Start方法來替代(換句話來說就是在u3d中,Awake方法和 Start方法是每個腳本文件類中默認的構造函數)。U3D甚至可以在標準編輯模式下就調用它們。它們通常是直接彙編在腳本中,因爲構造函數需要檢索默認 腳本變量用於引用。(u3d)在任意的時候不光可以調用構造函數,還可能會調用預設(物體)或未被喚醒的遊戲物體。

In the case of eg. a singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.
實例化(C#腳本文件)時,單腳本文件狀態下使用(自定義的)構造函數(可能)會導致嚴重的後果,並且會產生引用爲空的異常。

So if you want to implement eg. a singleton pattern do not use the the constructor, instead use Awake. Actually there is no reason why you should ever have any code in a constructor for a class that inherits from MonoBehaviour.

所以,如果你實例化C#腳本文件(即運行C#腳本文件。這是c#程序運行的基本方式,詳細內容可以從C#專門的教材中瞭解),單腳文件不要使用(自定義 的)的構造函數,直接使用Awake方法替代即可,實在沒有理由爲一個繼承之MonoBehaviour的(文件)類寫任何的(構造函數)代碼。

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章