首先要明确的是MonoBehaviour是每个脚本的基类.每个Javascript脚本自动继承MonoBehaviour.使用C#或Boo时,需要显式继承MonoBehaviour.
以下是一些常用的函数调用顺序的说明:
Awake:当一个脚本实例被载入时Awake被调用,要先于Start。建议少用,此刻物体可能还没有实例化出来,会影响程序执行顺序。需要注意的是,假设实例化一个物件:
GameObject go = new GameObject();
go.GetComponent().Test();
这个Test()的调用顺序会在Awake()之后,在Start()之前
Start:Start仅在Update函数第一次被调用前调用。物体实例化完成后调用(估计,没确认)。
Update:当MonoBehaviour启用时,其Update在每一帧被调用
FixedUpdate:这个函数会在每个固定的物理时间片被调用一次.这是放置游戏基本物理行为代码的地方。UPDATE之后调用。
Reset:Reset是在用户点击检视面板的Reset按钮或者首次添加该组件时被调用.此函数只在编辑模式下被调用.Reset最常用于在检视面板中给定一个最常用的默认值.
OnDestory:物体被删除时调用。
OnEnable:物体启用时被调用。
OnDisable:物体被禁用时调用。
OnGui:这个函数会每帧调用好几次(每个事件一次),GUI显示函数只能在OnGui中调用。备注:这个各大翻译都直接翻译成每帧调用一次了。
MonoBehaviour
class in UnityEngine
/
Inherits from: Behaviour
Description
MonoBehaviour is the base class every script derives from.
Using Javascript every script automatically derives from MonoBehaviour. When using C# or Boo you have to explicitly derive from MonoBehaviour.
Note: The checkbox for disabling a MonoBehavior (on the editor) will only prevent Start(), Awake(), Update(), FixedUpdate(), and OnGUI() from executing. If none of these functions are present, the checkbox is not displayed.
See Also: The chapter on scripting in the manual.
Variables
useGUILayout | Disabling this lets you skip the GUI layout phase. |
Public Functions
CancelInvoke | Cancels all Invoke calls on this MonoBehaviour. |
Invoke | Invokes the method methodName in time seconds. |
InvokeRepeating | Invokes the method methodName in time seconds, then repeatedly every repeatRate seconds. |
IsInvoking | Is any invoke on methodName pending? |
StartCoroutine | Starts a coroutine. |
StopAllCoroutines | Stops all coroutines running on this behaviour. |
StopCoroutine | Stops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour. |
Static Functions
Logs message to the Unity Console (identical to Debug.Log). |
Messages
Awake | Awake is called when the script instance is being loaded. |
FixedUpdate | This function is called every fixed framerate frame, if the MonoBehaviour is enabled. |
LateUpdate | LateUpdate is called every frame, if the Behaviour is enabled. |
OnAnimatorIK | Callback for setting up animation IK (inverse kinematics). |
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. |
OnBecameInvisible | OnBecameInvisible is called when the renderer is no longer visible by any camera. |
OnBecameVisible | OnBecameVisible is called when the renderer became visible by any camera. |
OnCollisionEnter | OnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider. |
OnCollisionEnter2D | Sent when an incoming collider makes contact with this object's collider (2D physics only). |
OnCollisionExit | OnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider. |
OnCollisionExit2D | Sent when a collider on another object stops touching this object's collider (2D physics only). |
OnCollisionStay | OnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider. |
OnCollisionStay2D | Sent each frame where a collider on another object is touching this object's collider (2D physics only). |
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. |
OnDestroy | This function is called when the MonoBehaviour will be destroyed. |
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. |
OnDrawGizmosSelected | Implement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected. |
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. |
OnGUI | OnGUI is called for rendering and handling GUI events. |
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. |
OnMouseDown | OnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider. |
OnMouseDrag | OnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse. |
OnMouseEnter | Called when the mouse enters the GUIElement or Collider. |
OnMouseExit | Called when the mouse is not any longer over the GUIElement or Collider. |
OnMouseOver | Called every frame while the mouse is over the GUIElement or Collider. |
OnMouseUp | OnMouseUp is called when the user has released the mouse button. |
OnMouseUpAsButton | OnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed. |
OnNetworkInstantiate | Called on objects which have been network instantiated with Network.Instantiate. |
OnParticleCollision | OnParticleCollision is called when a particle hits a collider. |
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. |
OnTransformChildrenChanged | This function is called when the list of children of the transform of the GameObject has changed. |
OnTransformParentChanged | This function is called when the parent property of the transform of the GameObject has changed. |
OnTriggerEnter | OnTriggerEnter is called when the Collider other enters the trigger. |
OnTriggerEnter2D | Sent when another object enters a trigger collider attached to this object (2D physics only). |
OnTriggerExit | OnTriggerExit is called when the Collider other has stopped touching the trigger. |
OnTriggerExit2D | Sent when another object leaves a trigger collider attached to this object (2D physics only). |
OnTriggerStay | OnTriggerStay is called once per frame for every Collider other that is touching the trigger. |
OnTriggerStay2D | Sent each frame where another object is within a trigger collider attached to this object (2D physics only). |
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. |
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. |
Update | Update is called every frame, if the MonoBehaviour is enabled. |
Inherited members
Variables
enabled | Enabled Behaviours are Updated, disabled Behaviours are not. |
isActiveAndEnabled | Has the Behaviour had enabled called. |
gameObject | The game object this component is attached to. A component is always attached to a game object. |
tag | The tag of this game object. |
transform | The Transform attached to this GameObject (null if there is none attached). |
hideFlags | Should the object be hidden, saved with the scene or modifiable by the user? |
name | The name of the object. |
Public Functions
BroadcastMessage | Calls the method named methodName on every MonoBehaviour in this game object or any of its children. |
CompareTag | Is this game object tagged with tag ? |
GetComponent | Returns the component of Type type if the game object has one attached, null if it doesn't. |
GetComponentInChildren | Returns the component of Type type in the GameObject or any of its children using depth first search. |
GetComponentInParent | Returns the component of Type type in the GameObject or any of its parents. |
GetComponents | Returns all components of Type type in the GameObject. |
GetComponentsInChildren | Returns all components of Type type in the GameObject or any of its children. |
GetComponentsInParent | Returns all components of Type type in the GameObject or any of its parents. |
SendMessage | Calls the method named methodName on every MonoBehaviour in this game object. |
SendMessageUpwards | Calls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour. |
GetInstanceID | Returns the instance id of the object. |
ToString | Returns the name of the game object. |
Static Functions
Destroy | Removes a gameobject, component or asset. |
DestroyImmediate | Destroys the object obj immediately. You are strongly recommended to use Destroy instead. |
DontDestroyOnLoad | Makes the object target not be destroyed automatically when loading a new scene. |
FindObjectOfType | Returns the first active loaded object of Type type. |
FindObjectsOfType | Returns a list of all active loaded objects of Type type. |
Instantiate | Clones the object original and returns the clone. |
Operators
bool | Does the object exist? |
operator != | Compares if two objects refer to a different object. |
operator == | Compares two object references to see if they refer to the same object. |
2018.07.30补充
除了我们最常使用的Awake,Start ,Updata等生命周期函数之外,Unity还有很多的自带函数,,,总结如下:
OnApplicationFocus
OnApplicationPause
OnApplicationQuit
应用程序失去焦点,应用程序暂停,应用程序退出时候发送这些消息。
OnBecameInvisible
OnBecameVisible
当脚本宿主(不)被任何摄像机显示时候发送此消息。
OnCollisionEnter
OnCollisionExit
OnCollisionStay
当其他碰撞或者刚体(collider/rigidbody )和参数的碰撞或者刚体(collider/rigidbody )重叠、退出时发送前两个。而当他们保持重叠状态时每帧都会发送一个Stay消息。
OnConnectedToServer
OnDisconnectedFromServer
OnFailedToConnect
OnFailedToConnectToMasterServer
前两个 当客户端成功连接到服务器或者断开服务器时发送此消息。后两个当连接失败时候触发。
OnMasterServerEvent
当Master服务器发送报告时候触发。
OnNetworkInstantiate
当物体被Network.Instantiate时触发。
OnPlayerConnected
OnPlayerDisconnected
在服务端当玩家成功连接/离线时候触发。
OnControllerColliderHit
当控制者和参数ControllerColliderHit碰撞时候触发此消息。官方举例可以用于角色移动一个物体,当角色碰到这个参数物体时候,你可以在这函数里操作移动此物体的动作。
OnParticleCollision
当粒子撞到碰撞体(collider)时触发。
OnDisable
OnEnable
当脚本宿主被启用或者禁用时候触发。
OnDrawGizmos
OnDrawGizmosSelected
编辑器状态时绘制Gizmos和Gizmos被选取时候触发。
OnGUI
绘制GUI时候触发。一般在这个函数里绘制GUI菜单。
OnJointBreak
OnLevelWasLoaded
当新的level(Unity包)读取完毕时候触发。
OnMouseDown
OnMouseDrag
OnMouseEnter
OnMouseExit
OnMouseOver
OnMouseUp
鼠标事件,都是当鼠标和gui或者碰撞体(Collider)交互时候触发。需要说明的是drag其实就是鼠标down后up之前持续每帧都会发送此消息。
OnPostRender
这个函数仅用于宿主为摄像机的脚本。当此摄像机范围内所有渲染都完成时候触发此消息。
OnPreCull
这个函数仅用于宿主为摄像机的脚本。当此摄像机剔除了某个渲染场景时候触发此消息。
OnPreRender
这个函数仅用于宿主为摄像机的脚本。当此摄像机开始渲染某个场景时候触发此消息。
OnRenderImage
当所有渲染完成image的postprocessing effects(只有pro版支持)后触发。
OnRenderObject
这个函数仅用于宿主为摄像机的脚本。当使用Graphics.DrawMeshNow 或者其他函数绘制自己建立的物体渲染完毕时触发。
OnSerializeNetworkView
OnServerInitialized
当 Network.InitializeServer完成时触发。
OnTriggerEnter
OnTriggerExit
OnTriggerStay
当碰撞体(collier)接触触发区域(trigger)时候的一系列消息。
如果有些地方叙述不清楚大家可以查看Unity 圣典API中搜索MonoBehavior
文章参考自: http://www.manew.com/4936.html