Overview : Writing Scripts in C# 使用C#書寫指令碼

來源:互聯網
上載者:User

標籤:style   http   java   color   使用   檔案   

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中,必須顯示註明.如果你通過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的類的建構函式中寫任何代碼.

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.