低調の美國生活

各種疑難雜症

Unity C# Scripting基本觀念

(Source: Unity官網)

Object and Component

  • active an object
    • prefab is an object
  • enable a component
    • any script under an object is a component
    • renderer is a component
  • GameObject.Find不能找被隱藏的物件/setActive(false)

 


變數在不同區域(scope)的優先權

[codebox 1]

由上例,最後myInt為11

另外如果沒有在Start( )做初始化/指定數值,而且myInt又是宣告成public的情形下,那會以Inspector的數值為優先

結論:

A: 在宣告區

B: 在Start( )

C: 在Inspector (在runtime之前/在遊戲開始之前)

優先權 B > C > A


Awake( )與Start( )

說明

[codebox 2]

範例

[codebox 3]

Update( ) vs FixedUpdate( )

Update( )

  • call every frame
  • but interval time between frames is vary
  • used for regular updates:
    • non-physics objects movement
    • simple timers
    • receive input

FixedUpdate( )

  • call every physics step (called in regular timeline)
  • called interval time are constant
  • used for adjusting physics (RB) objects

發佈於 12月1日2015年