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中,必須顯示注明.如果你通過Asset -> Create -> C Sharp/Boo Script創建腳本,系統模版已經包含了必要的定義.
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.
JavaScript中放在函數之外的代碼,在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中類名被隱式的設置為腳本的文件名(不包含文件擴展名).在C#和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 ... ;.
Coroutines必須是IEnumerator返回類型,並且yield用yield return替代.
using System.Collections;
using UnityEngine;
public class NewBehaviourScript : MonoBehaviour {
// C# coroutine // C# 協同程序
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.
目前Unity暫不支持命名空間.或許未來版本會有.
6. Only member variables are serialized and are shown in the Inspector.
只有序列化的成員變量才能顯示在檢視面板
Private and protected member variables are shown only in Expert Mode. Properties are not serialized or shown in the inspector.
私有和保護變量只能在專家模式中顯示.屬性不被序列化或顯示在檢視面板.
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.
不要在構造函數中初始化任何變量.要用Awake或Start函數來實現.即便是在編輯模式,Unity仍會自動調用構造函數.這通常是在一個腳本編譯之後,因為需要調用腳本的構造函數來取回腳本的默認值.我們無法預計何時調用構造函數,它或許會被預置體或未激活的游戲對象所調用.
In the case of eg. a singleton pattern using the constructor this can have severe consequences and lead to seemingly random null reference exceptions.
單一模式使用構造函數可能會導致嚴重後果,會帶來類似隨機的空參數異常.
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 .
因此,如果你想實現單一模式不要用構造函數,要用Awake函數.事實上,你沒必要在繼承自MonoBehaviour的類的構造函數中寫任何代碼.