一、簡介
MonoBehaviour是每個腳本派生類的基類,它定義了一個腳本文件從最初被加載到最終被銷毀的一個完整過程。
這個過程通過對應的方法體現出來,在不同的方法完成不同的功能,我們把這些方法稱為:腳本生命周期相關的方法。包含內容有:
Awake():當該腳本實例被載入時Awake被調用。(只會被調用一次)
OnEable():當對象被啟用並激活狀態時此函數被調用。
Start():Start僅在當第一次腳本啟用Update方法被調用之前調用。(只會被調用一次)
Update():當MonoBehaviour啟用時,其Update在每一幀被調用
FixedUpdate():(固定更新)會按照設置的時間固定頻率來循環更新
LateUpdate():LateUpdate是在所有Update函數調用后被調用。這可用於調整腳本執行順序。例如:當物體在Update里移動時,跟隨物體的相機可以在LateUpdate里實現。
OnGUI():渲染和處理GUI事件時調用。
OnDisable():當對象變為不可用或非激活狀態時此函數被調用。
OnEnable():當對象被啟用並激活狀態時此函數被調用。
二、常用屬性及方法介紹
Variables 變量
useGUILayout | Disabling this lets you skip the GUI layout phase. 禁用此項,將會跳過GUILayout布局。 |
Functions 函數
CancelInvoke | Cancels all Invoke calls on this MonoBehaviour. 在當前MonoBehaviour,取消所有Invoke調用 |
Invoke | Invokes the method methodName in time seconds. 在time秒后,延遲調用方法methodName。 |
InvokeRepeating | Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. 在time秒調用methodName方法,然后每repeatRate秒重復調用。 |
IsInvoking | Is any invoke on methodName pending? methodName方法是否在等候調用? |
StartCoroutine | Starts a coroutine. 開始協同程序。 |
StopAllCoroutines | Stops all coroutines running on this behaviour. 停止運行此behaviour上的所有協同程序。 |
StopCoroutine | Stops all coroutines named methodName running on this behaviour. 停止此behaviour上運行的所有名為methodName的協同程序。 |
Static Functions 靜態函數
Logs message to the Unity Console (identical to Debug.Log). 輸出日志消息到Unity控制台。等同Debug.Log。 |
Messages 消息
Awake | Awake is called when the script instance is being loaded. 當該腳本實例被載入時Awake被調用。 |
FixedUpdate | This function is called every fixed framerate frame, if the MonoBehaviour is enabled. 當MonoBehaviour啟用時,其 FixedUpdate 在每一幀被調用。 |
LateUpdate | LateUpdate is called every frame, if the Behaviour is enabled. 當Behaviour啟用時,其LateUpdate在每一幀被調用。 |
OnAnimatorIK | Callback for setting up animation IK (inverse kinematics). 設置動畫的IK回調(反向運動學)。 |
OnAnimatorMove | Callback for processing animation movements for modifying root motion. 用於修改根運動處理動畫移動的回調。 |
OnApplicationFocus | Sent to all game objects when the player gets or loses focus. 當玩家獲得或失去焦點時發送給所有游戲對象。 |
OnApplicationPause | Sent to all game objects when the player pauses. 當玩家暫停發送給所有游戲對象。 |
OnApplicationQuit | Sent to all game objects before the application is quit. 在應用退出之前發送給所有的游戲物體。 |
OnAudioFilterRead | If OnAudioFilterRead is implemented, Unity will insert a custom filter into the audio DSP chain. 如果OnAudioFilterRead被執行,Unity將插入一個自定義的過濾器到音頻DSP鏈。 |
OnBecameInvisible | OnBecameInvisible is called when the renderer is no longer visible by any camera. 當renderer(渲染器)在任何相機上都不可見時調用OnBecameInvisible。 |
OnBecameVisible | OnBecameVisible is called when the renderer became visible by any camera. 當renderer(渲染器)在任何相機上可見時調用OnBecameVisible。 |
OnCollisionEnter | OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. 當此collider/rigidbody觸發另一個rigidbody/collider時,OnCollisionEnter將被調用。 |
OnCollisionEnter2D | Sent when an incoming collider makes contact with this object's collider (2D physics only). 當進入的碰撞器與這個對象的碰撞器接觸,發送信息(僅限2D物理)。 |
OnCollisionExit | OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider. 當此collider/rigidbody停止觸發另一個rigidbody/collider時,OnCollisionExit將被調用。 |
OnCollisionExit2D | Sent when a collider on another object stops touching this object's collider (2D physics only). 當另一個對象的碰撞器停止接觸這個對象的碰撞器時,發送信息(僅限2D物理)。 |
OnCollisionStay | OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider. 當此collider/rigidbody觸發另一個rigidbody/collider時,OnCollisionStay將會在每一幀被調用。 |
OnCollisionStay2D | Sent each frame where a collider on another object is touching this object's collider (2D physics only). 當另一個對象的碰撞器每幀接觸這個對象的碰撞器時,發送信息(僅限2D物理)。 |
OnConnectedToServer | Called on the client when you have successfully connected to a server. 當成功連接到服務器時,在客戶端調用。 |
OnControllerColliderHit | OnControllerColliderHit is called when the controller hits a collider while performing a Move. 在移動的時,當controller碰撞到collider時OnControllerColliderHit被調用。 |
OnDestroy | This function is called when the MonoBehaviour will be destroyed. 當MonoBehaviour將被銷毀時,這個函數被調用。 |
OnDisable | This function is called when the behaviour becomes disabled () or inactive. 當對象變為不可用或非激活狀態時此函數被調用。 |
OnDisconnectedFromServer | Called on the client when the connection was lost or you disconnected from the server. 當失去連接或從服務器端斷開時在客戶端調用。 |
OnDrawGizmos | Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn. 如果你想繪制可被點選的gizmos,執行OnDrawGizmos。 |
OnDrawGizmosSelected | Implement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected. 如果你想在物體被選中時繪制gizmos,執行這個函數。 |
OnEnable | This function is called when the object becomes enabled and active. 當對象被啟用並激活狀態時此函數被調用。 |
OnFailedToConnect | Called on the client when a connection attempt fails for some reason. 當一個連接因為某些原因失敗時在客戶端調用。 |
OnFailedToConnectToMasterServer | Called on clients or servers when there is a problem connecting to the MasterServer. 當連接MasterServer出現問題時在客戶端或服務器端調用。 |
OnGUI | OnGUI is called for rendering and handling GUI events. 渲染和處理GUI事件時調用。 |
OnJointBreak | Called when a joint attached to the same game object broke. 當附在同一對象上的關節被斷開時調用。 |
OnLevelWasLoaded | This function is called after a new level was loaded. 當一個新關卡被載入時此函數被調用。 |
OnMasterServerEvent | Called on clients or servers when reporting events from the MasterServer. 當報告事件來自MasterServer時在客戶端或服務器端調用。 |
OnMouseDown | OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. 當用戶鼠標在GUIElement或Collider上點擊時OnMouseDown被調用。 |
OnMouseDrag | OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse. 當用戶鼠標在GUIElement或Collider上拖拽時OnMouseDrag被調用 。 |
OnMouseEnter | OnMouseEnter is called when the mouse entered the GUIElement or Collider. 當鼠標進入到GUIElement(GUI元素)或Collider(碰撞體)中時調用OnMouseEnter。 |
OnMouseExit | OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider. 當用戶鼠標不在GUIElement或Collider上時OnMouseExit被調用。 |
OnMouseOver | OnMouseOver is called every frame while the mouse is over the GUIElement or Collider. 當用戶鼠標在GUIElement或Collider上經過時OnMouseOver被調用。 |
OnMouseUp | OnMouseUp is called when the user has released the mouse button. 當用戶釋放鼠標按鈕時調用OnMouseUp。 |
OnMouseUpAsButton | OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed. OnMouseUpAsButton只有當鼠標在同一個GUIElement或Collider按下,在釋放時調用。 |
OnNetworkInstantiate | Called on objects which have been network instantiated with Network.Instantiate. 當對象使用Network.Instantiate進行網絡初始化時調用。 |
OnParticleCollision | OnParticleCollision is called when a particle hits a collider. 當粒子碰到碰撞器時,OnParticleCollision 被調用。 |
OnPlayerConnected | Called on the server whenever a new player has successfully connected. 當一個新玩家成功連接時在服務器上被調用。 |
OnPlayerDisconnected | Called on the server whenever a player disconnected from the server. 當一個玩家從服務器上斷開時在服務器端調用。 |
OnPostRender | OnPostRender is called after a camera finished rendering the scene. 在相機完成場景渲染之后被調用。 |
OnPreCull | OnPreCull is called before a camera culls the scene. 在相機消隱場景之前被調用。 |
OnPreRender | OnPreRender is called before a camera starts rendering the scene. 在相機渲染場景之前被調用。 |
OnRenderImage | OnRenderImage is called after all rendering is complete to render image. 當完成所有渲染圖像后被調用,用來渲染圖像后期效果。 |
OnRenderObject | OnRenderObject is called after camera has rendered the scene. 在相機場景渲染完成后被調用。 |
OnSerializeNetworkView | Used to customize synchronization of variables in a script watched by a network view. 在一個網絡視圖腳本中,用於自定義變量同步。 |
OnServerInitialized | Called on the server whenever a Network.InitializeServer was invoked and has completed. 當Network.InitializeServer被調用並完成時,在服務器上調用這個函數。 |
OnTriggerEnter | OnTriggerEnter is called when the Collider other enters the trigger. 當Collider(碰撞體)進入trigger(觸發器)時調用OnTriggerEnter。 |
OnTriggerEnter2D | Sent when another object enters a trigger collider attached to this object (2D physics only). 當另一個對象進入到這個對象附加的觸發碰撞器時發送消息(僅2D物理)。 |
OnTriggerExit | OnTriggerExit is called when the Collider other has stopped touching the trigger. 當Collider(碰撞體)停止觸發trigger(觸發器)時調用OnTriggerExit。 |
OnTriggerExit2D | Sent when another object leaves a trigger collider attached to this object (2D physics only). 當另一個對象離開附加在這個對象的觸發碰撞器時發送消息(僅2D物理)。 |
OnTriggerStay | OnTriggerStay is called once per frame for every Collider other that is touching the trigger. 當碰撞體接觸觸發器時,OnTriggerStay將在每一幀被調用。 |
OnTriggerStay2D | Sent each frame where another object is within a trigger collider attached to this object (2D physics only). 當另一個對象停留在這個對象附加的觸發碰撞器內時,每幀發送消息(僅2D物理)。 |
OnValidate | This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). 當該腳本被加載或檢視面板的值被修改時,此函數被調用(僅在編輯器被調用)。 |
OnWillRenderObject | OnWillRenderObject is called once for each camera if the object is visible. 如果該對象可見,OnWillRenderObject每次會被相機調用。 |
Reset | Reset to default values. 重設為默認值。 |
Start | Start is called on the frame when a script is enabled just before any of the Update methods is called the first time. Start僅在當第一次腳本啟用Update方法被調用之前調用。 |
Update | Update is called every frame, if the MonoBehaviour is enabled. 當MonoBehaviour啟用時,其Update在每一幀被調用。 |
Variables 變量
useGUILayout | Disabling this lets you skip the GUI layout phase. 禁用此項,將會跳過GUILayout布局。 |
Functions 函數
CancelInvoke | Cancels all Invoke calls on this MonoBehaviour 在當前 MonoBehaviour,取消所有Invoke調用 |
Invoke | Invokes the method methodName in time seconds. 在time秒后,延遲調用方法methodName。 |
InvokeRepeating | Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. 在time秒調用methodName方法,然后每repeatRate秒重復調用。 |
IsInvoking | Is any invoke on methodName pending? methodName方法是否在等候調用? |
StartCoroutine | Starts a coroutine. 開始協同程序。 |
StopAllCoroutines | Stops all coroutines running on this behaviour. 停止運行此behaviour上的所有協同程序。 |
StopCoroutine | Stops all coroutines named methodName running on this behaviour. 停止此behaviour上運行的所有名為methodName的協同程序。 |
Static Functions 靜態函數
Logs message to the Unity Console (identical to Debug.Log). 輸出日志消息到Unity控制台。等同Debug.Log。 |
Messages 消息
Awake | Awake is called when the script instance is being loaded. 當該腳本實例被載入時Awake被調用。 |
FixedUpdate | This function is called every fixed framerate frame, if the MonoBehaviour is enabled. 當 MonoBehaviour啟用時,其 FixedUpdate 在每一幀被調用。 |
LateUpdate | LateUpdate is called every frame, if the Behaviour is enabled. 當Behaviour啟用時,其LateUpdate在每一幀被調用。 |
OnAnimatorIK | Callback for setting up animation IK (inverse kinematics). 設置動畫的IK回調(反向運動學)。 |
OnAnimatorMove | Callback for processing animation movements for modifying root motion. 用於修改根運動處理動畫移動的回調。 |
OnApplicationFocus | Sent to all game objects when the player gets or loses focus. 當玩家獲得或失去焦點時發送給所有游戲對象。 |
OnApplicationPause | Sent to all game objects when the player pauses. 當玩家暫停發送給所有游戲對象。 |
OnApplicationQuit | Sent to all game objects before the application is quit. 在應用退出之前發送給所有的游戲物體。 |
OnAudioFilterRead | If OnAudioFilterRead is implemented, Unity will insert a custom filter into the audio DSP chain. 如果OnAudioFilterRead被執行,Unity將插入一個自定義的過濾器到音頻DSP鏈。 |
OnBecameInvisible | OnBecameInvisible is called when the renderer is no longer visible by any camera. 當renderer(渲染器)在任何相機上都不可見時調用OnBecameInvisible。 |
OnBecameVisible | OnBecameVisible is called when the renderer became visible by any camera. 當renderer(渲染器)在任何相機上可見時調用OnBecameVisible。 |
OnCollisionEnter | OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. 當此collider/rigidbody觸發另一個rigidbody/collider時,OnCollisionEnter將被調用。 |
OnCollisionEnter2D | Sent when an incoming collider makes contact with this object's collider (2D physics only). 當進入的碰撞器與這個對象的碰撞器接觸,發送信息(僅限2D物理)。 |
OnCollisionExit | OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider. 當此collider/rigidbody停止觸發另一個rigidbody/collider時,OnCollisionExit將被調用。 |
OnCollisionExit2D | Sent when a collider on another object stops touching this object's collider (2D physics only). 當另一個對象的碰撞器停止接觸這個對象的碰撞器時,發送信息(僅限2D物理)。 |
OnCollisionStay | OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider. 當此collider/rigidbody觸發另一個rigidbody/collider時,OnCollisionStay將會在每一幀被調用。 |
OnCollisionStay2D | Sent each frame where a collider on another object is touching this object's collider (2D physics only). 當另一個對象的碰撞器每幀接觸這個對象的碰撞器時,發送信息(僅限2D物理)。 |
OnConnectedToServer | Called on the client when you have successfully connected to a server. 當成功連接到服務器時,在客戶端調用。 |
OnControllerColliderHit | OnControllerColliderHit is called when the controller hits a collider while performing a Move. 在移動的時,當controller碰撞到collider時OnControllerColliderHit被調用。 |
OnDestroy | This function is called when the MonoBehaviour will be destroyed. 當 MonoBehaviour將被銷毀時,這個函數被調用。 |
OnDisable | This function is called when the behaviour becomes disabled () or inactive. 當對象變為不可用或非激活狀態時此函數被調用。 |
OnDisconnectedFromServer | Called on the client when the connection was lost or you disconnected from the server. 當失去連接或從服務器端斷開時在客戶端調用。 |
OnDrawGizmos | Implement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn. 如果你想繪制可被點選的gizmos,執行OnDrawGizmos。 |
OnDrawGizmosSelected | Implement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected. 如果你想在物體被選中時繪制gizmos,執行這個函數。 |
OnEnable | This function is called when the object becomes enabled and active. 當對象被啟用並激活狀態時此函數被調用。 |
OnFailedToConnect | Called on the client when a connection attempt fails for some reason. 當一個連接因為某些原因失敗時在客戶端調用。 |
OnFailedToConnectToMasterServer | Called on clients or servers when there is a problem connecting to the MasterServer. 當連接MasterServer出現問題時在客戶端或服務器端調用。 |
OnGUI | OnGUI is called for rendering and handling GUI events. 渲染和處理GUI事件時調用。 |
OnJointBreak | Called when a joint attached to the same game object broke. 當附在同一對象上的關節被斷開時調用。 |
OnLevelWasLoaded | This function is called after a new level was loaded. 當一個新關卡被載入時此函數被調用。 |
OnMasterServerEvent | Called on clients or servers when reporting events from the MasterServer. 當報告事件來自MasterServer時在客戶端或服務器端調用。 |
OnMouseDown | OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. 當用戶鼠標在GUIElement或Collider上點擊時OnMouseDown被調用。 |
OnMouseDrag | OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse. 當用戶鼠標在GUIElement或Collider上拖拽時OnMouseDrag被調用 。 |
OnMouseEnter | OnMouseEnter is called when the mouse entered the GUIElement or Collider. 當鼠標進入到GUIElement(GUI元素)或Collider(碰撞體)中時調用OnMouseEnter。 |
OnMouseExit | OnMouseExit is called when the mouse is not any longer over the GUIElement or Collider. 當用戶鼠標不在GUIElement或Collider上時OnMouseExit被調用。 |
OnMouseOver | OnMouseOver is called every frame while the mouse is over the GUIElement or Collider. 當用戶鼠標在GUIElement或Collider上經過時OnMouseOver被調用。 |
OnMouseUp | OnMouseUp is called when the user has released the mouse button. 當用戶釋放鼠標按鈕時調用OnMouseUp。 |
OnMouseUpAsButton | OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed. OnMouseUpAsButton只有當鼠標在同一個GUIElement或Collider按下,在釋放時調用。 |
OnNetworkInstantiate | Called on objects which have been network instantiated with Network.Instantiate. 當對象使用Network.Instantiate進行網絡初始化時調用。 |
OnParticleCollision | OnParticleCollision is called when a particle hits a collider. 當粒子碰到碰撞器時,OnParticleCollision 被調用。 |
OnPlayerConnected | Called on the server whenever a new player has successfully connected. 當一個新玩家成功連接時在服務器上被調用。 |
OnPlayerDisconnected | Called on the server whenever a player disconnected from the server. 當一個玩家從服務器上斷開時在服務器端調用。 |
OnPostRender | OnPostRender is called after a camera finished rendering the scene. 在相機完成場景渲染之后被調用。 |
OnPreCull | OnPreCull is called before a camera culls the scene. 在相機消隱場景之前被調用。 |
OnPreRender | OnPreRender is called before a camera starts rendering the scene. 在相機渲染場景之前被調用。 |
OnRenderImage | OnRenderImage is called after all rendering is complete to render image. 當完成所有渲染圖像后被調用,用來渲染圖像后期效果。 |
OnRenderObject | OnRenderObject is called after camera has rendered the scene. 在相機場景渲染完成后被調用。 |
OnSerializeNetworkView | Used to customize synchronization of variables in a script watched by a network view. 在一個網絡視圖腳本中,用於自定義變量同步。 |
OnServerInitialized | Called on the server whenever a Network.InitializeServer was invoked and has completed. 當Network.InitializeServer被調用並完成時,在服務器上調用這個函數。 |
OnTriggerEnter | OnTriggerEnter is called when the Collider other enters the trigger. 當Collider(碰撞體)進入trigger(觸發器)時調用OnTriggerEnter。 |
OnTriggerEnter2D | Sent when another object enters a trigger collider attached to this object (2D physics only). 當另一個對象進入到這個對象附加的觸發碰撞器時發送消息(僅2D物理)。 |
OnTriggerExit | OnTriggerExit is called when the Collider other has stopped touching the trigger. 當Collider(碰撞體)停止觸發trigger(觸發器)時調用OnTriggerExit。 |
OnTriggerExit2D | Sent when another object leaves a trigger collider attached to this object (2D physics only). 當另一個對象離開附加在這個對象的觸發碰撞器時發送消息(僅2D物理)。 |
OnTriggerStay | OnTriggerStay is called once per frame for every Collider other that is touching the trigger. 當碰撞體接觸觸發器時,OnTriggerStay將在每一幀被調用。 |
OnTriggerStay2D | Sent each frame where another object is within a trigger collider attached to this object (2D physics only). 當另一個對象停留在這個對象附加的觸發碰撞器內時,每幀發送消息(僅2D物理)。 |
OnValidate | This function is called when the script is loaded or a value is changed in the inspector (Called in the editor only). 當該腳本被加載或檢視面板的值被修改時,此函數被調用(僅在編輯器被調用)。 |
OnWillRenderObject | OnWillRenderObject is called once for each camera if the object is visible. 如果該對象可見,OnWillRenderObject每次會被相機調用。 |
Reset | Reset to default values. 重設為默認值。 |
Start | Start is called on the frame when a script is enabled just before any of the Update methods is called the first time. Start僅在當第一次腳本啟用Update方法被調用之前調用。 |
Update | Update is called every frame, if the MonoBehaviour is enabled. 當 MonoBehaviour啟用時,其Update在每一幀被調用。 |