Unity3D C#指令碼開發學習

快看一隻熊發表於2014-02-11

1. Inherit from 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建立指令碼,系統模版已經包含了必要的定義.

C#

public class NewBehaviourScript : MonoBehaviour {...}

Boo

class NewBehaviourScript (MonoBehaviour):...

2. Use the Awake or Start function to do initialisation.What you would put outside any functions in Javascript, you put inside Awake or Start function in C# or Boo.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函式初始化.JavaScript中放在函式之外的程式碼,在C#或Boo中必須置於Awake或Start函式裡.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#. Coroutines have to have a return type of IEnumerator and you yield using yield return ... ; instead of just yield ... ;.

C#中協同程式有不同的句法規則,Coroutines必須是IEnumerator返回型別,並且yield用yield return替代.

 1 using System.Collections;
 2  using UnityEngine;
 3  public class NewBehaviourScript : MonoBehaviour {
 4  // C# coroutine // C# 協同程式
 5 IEnumerator SomeCoroutine () {
 6  // Wait for one frame // 等一幀
 7 yield return 0;
 8 
 9 // Wait for two seconds // 等兩秒
10 yield return new WaitForSeconds (2);
11  }
12  }

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的類的建構函式中寫任何程式碼.

只能理解一部分,剛開始學習,後面開始仔細研究。

相關文章