Unity自带函数

 

首先要明确的是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

useGUILayoutDisabling this lets you skip the GUI layout phase.

Public Functions

CancelInvokeCancels all Invoke calls on this MonoBehaviour.
InvokeInvokes the method methodName in time seconds.
InvokeRepeatingInvokes the method methodName in time seconds, then repeatedly every repeatRate seconds.
IsInvokingIs any invoke on methodName pending?
StartCoroutineStarts a coroutine.
StopAllCoroutinesStops all coroutines running on this behaviour.
StopCoroutineStops the first coroutine named methodName, or the coroutine stored in routine running on this behaviour.

Static Functions

printLogs message to the Unity Console (identical to Debug.Log).

Messages

AwakeAwake is called when the script instance is being loaded.
FixedUpdateThis function is called every fixed framerate frame, if the MonoBehaviour is enabled.
LateUpdateLateUpdate is called every frame, if the Behaviour is enabled.
OnAnimatorIKCallback for setting up animation IK (inverse kinematics).
OnAnimatorMoveCallback for processing animation movements for modifying root motion.
OnApplicationFocusSent to all game objects when the player gets or loses focus.
OnApplicationPauseSent to all game objects when the player pauses.
OnApplicationQuitSent to all game objects before the application is quit.
OnAudioFilterReadIf OnAudioFilterRead is implemented, Unity will insert a custom filter into the audio DSP chain.
OnBecameInvisibleOnBecameInvisible is called when the renderer is no longer visible by any camera.
OnBecameVisibleOnBecameVisible is called when the renderer became visible by any camera.
OnCollisionEnterOnCollisionEnter is called when this collider/rigidbody has begun touching another rigidbody/collider.
OnCollisionEnter2DSent when an incoming collider makes contact with this object's collider (2D physics only).
OnCollisionExitOnCollisionExit is called when this collider/rigidbody has stopped touching another rigidbody/collider.
OnCollisionExit2DSent when a collider on another object stops touching this object's collider (2D physics only).
OnCollisionStayOnCollisionStay is called once per frame for every collider/rigidbody that is touching rigidbody/collider.
OnCollisionStay2DSent each frame where a collider on another object is touching this object's collider (2D physics only).
OnConnectedToServerCalled on the client when you have successfully connected to a server.
OnControllerColliderHitOnControllerColliderHit is called when the controller hits a collider while performing a Move.
OnDestroyThis function is called when the MonoBehaviour will be destroyed.
OnDisableThis function is called when the behaviour becomes disabled () or inactive.
OnDisconnectedFromServerCalled on the client when the connection was lost or you disconnected from the server.
OnDrawGizmosImplement OnDrawGizmos if you want to draw gizmos that are also pickable and always drawn.
OnDrawGizmosSelectedImplement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected.
OnEnableThis function is called when the object becomes enabled and active.
OnFailedToConnectCalled on the client when a connection attempt fails for some reason.
OnFailedToConnectToMasterServerCalled on clients or servers when there is a problem connecting to the MasterServer.
OnGUIOnGUI is called for rendering and handling GUI events.
OnJointBreakCalled when a joint attached to the same game object broke.
OnLevelWasLoadedThis function is called after a new level was loaded.
OnMasterServerEventCalled on clients or servers when reporting events from the MasterServer.
OnMouseDownOnMouseDown is called when the user has pressed the mouse button while over the GUIElement or Collider.
OnMouseDragOnMouseDrag is called when the user has clicked on a GUIElement or Collider and is still holding down the mouse.
OnMouseEnterCalled when the mouse enters the GUIElement or Collider.
OnMouseExitCalled when the mouse is not any longer over the GUIElement or Collider.
OnMouseOverCalled every frame while the mouse is over the GUIElement or Collider.
OnMouseUpOnMouseUp is called when the user has released the mouse button.
OnMouseUpAsButtonOnMouseUpAsButton is only called when the mouse is released over the same GUIElement or Collider as it was pressed.
OnNetworkInstantiateCalled on objects which have been network instantiated with Network.Instantiate.
OnParticleCollisionOnParticleCollision is called when a particle hits a collider.
OnPlayerConnectedCalled on the server whenever a new player has successfully connected.
OnPlayerDisconnectedCalled on the server whenever a player disconnected from the server.
OnPostRenderOnPostRender is called after a camera finished rendering the scene.
OnPreCullOnPreCull is called before a camera culls the scene.
OnPreRenderOnPreRender is called before a camera starts rendering the scene.
OnRenderImageOnRenderImage is called after all rendering is complete to render image.
OnRenderObjectOnRenderObject is called after camera has rendered the scene.
OnSerializeNetworkViewUsed to customize synchronization of variables in a script watched by a network view.
OnServerInitializedCalled on the server whenever a Network.InitializeServer was invoked and has completed.
OnTransformChildrenChangedThis function is called when the list of children of the transform of the GameObject has changed.
OnTransformParentChangedThis function is called when the parent property of the transform of the GameObject has changed.
OnTriggerEnterOnTriggerEnter is called when the Collider other enters the trigger.
OnTriggerEnter2DSent when another object enters a trigger collider attached to this object (2D physics only).
OnTriggerExitOnTriggerExit is called when the Collider other has stopped touching the trigger.
OnTriggerExit2DSent when another object leaves a trigger collider attached to this object (2D physics only).
OnTriggerStayOnTriggerStay is called once per frame for every Collider other that is touching the trigger.
OnTriggerStay2DSent each frame where another object is within a trigger collider attached to this object (2D physics only).
OnValidateThis function is called when the script is loaded or a value is changed in the inspector (Called in the editor only).
OnWillRenderObjectOnWillRenderObject is called once for each camera if the object is visible.
ResetReset to default values.
StartStart is called on the frame when a script is enabled just before any of the Update methods is called the first time.
UpdateUpdate is called every frame, if the MonoBehaviour is enabled.

Inherited members

Variables

enabledEnabled Behaviours are Updated, disabled Behaviours are not.
isActiveAndEnabledHas the Behaviour had enabled called.
gameObjectThe game object this component is attached to. A component is always attached to a game object.
tagThe tag of this game object.
transformThe Transform attached to this GameObject (null if there is none attached).
hideFlagsShould the object be hidden, saved with the scene or modifiable by the user?
nameThe name of the object.

Public Functions

BroadcastMessageCalls the method named methodName on every MonoBehaviour in this game object or any of its children.
CompareTagIs this game object tagged with tag ?
GetComponentReturns the component of Type type if the game object has one attached, null if it doesn't.
GetComponentInChildrenReturns the component of Type type in the GameObject or any of its children using depth first search.
GetComponentInParentReturns the component of Type type in the GameObject or any of its parents.
GetComponentsReturns all components of Type type in the GameObject.
GetComponentsInChildrenReturns all components of Type type in the GameObject or any of its children.
GetComponentsInParentReturns all components of Type type in the GameObject or any of its parents.
SendMessageCalls the method named methodName on every MonoBehaviour in this game object.
SendMessageUpwardsCalls the method named methodName on every MonoBehaviour in this game object and on every ancestor of the behaviour.
GetInstanceIDReturns the instance id of the object.
ToStringReturns the name of the game object.

Static Functions

DestroyRemoves a gameobject, component or asset.
DestroyImmediateDestroys the object obj immediately. You are strongly recommended to use Destroy instead.
DontDestroyOnLoadMakes the object target not be destroyed automatically when loading a new scene.
FindObjectOfTypeReturns the first active loaded object of Type type.
FindObjectsOfTypeReturns a list of all active loaded objects of Type type.
InstantiateClones the object original and returns the clone.

Operators

boolDoes 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

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值