unity中脚本自带函数调用顺序

所有介绍来自官方文档,原文更具有竞争力

先后顺序已经排好

1.Awake

Description

Awake is called when the script instance is being loaded.

Awake is used to initialize any variables or game state before the game starts. Awake is called only once during the lifetime of the script instance. Awake is called after all objects are initialized so you can safely speak to other objects or query them using eg. GameObject.FindWithTag. Each GameObject's Awake is called in a random order between objects. Because of this, you should use Awake to set up references between scripts, and use Start to pass any information back and forth. Awake is always called before any Start functions. This allows you to order initialization of scripts. Awake can not act as a coroutine.Note for C# and Boo users: use Awake instead of the constructor for initialization, as the serialized state of the component is undefined at construction time. Awake is called once, just like the constructor.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { private GameObject target; void Awake() { target = GameObject.FindWithTag("Player"); } }


2.Start

Description

Start is called on the frame when a script is enabled just before any of the Update methods is called the first time.

Like the Awake function, Start is called exactly once in the lifetime of the script. However, Awake is called when the script object is initialised, regardless of whether or not the script is enabled. Start may not be called on the same frame as Awake if the script is not enabled at initialisation time.The Awake function is called on all objects in the scene before any object's Start function is called. This fact is useful in cases where object A's initialisation code needs to rely on object B's already being initialised; B's initialisation should be done in Awake while A's should be done in Start.Where objects are instantiated during gameplay, their Awake function will naturally be called after the Start functions of scene objects have already completed.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { private GameObject target; void Start() { target = GameObject.FindWithTag("Player"); } }


3.FixedUpdate

Description

This function is called every fixed framerate frame, if the MonoBehaviour is enabled.

FixedUpdate should be used instead of Update when dealing withRigidbody. For example when adding a force to a rigidbody, you have to apply the force every fixed frame inside FixedUpdate instead of every frame inside Update.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { public Rigidbody rb;
void Start() { rb = GetComponent<Rigidbody>(); }
void FixedUpdate() { rb.AddForce(10.0f * Vector3.up); } }

   

4.Update

Description

Update is called every frame, if the MonoBehaviour is enabled.

Update is the most commonly used function to implement any kind of game behaviour.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { void Update() { transform.Translate(0, 0, Time.deltaTime * 1); } }

    

     

5.LateUpdate

Description

LateUpdate is called every frame, if theBehaviouris enabled.

LateUpdate is called after all Update functions have been called. This is useful to order script execution. For example a follow camera should always be implemented in LateUpdate because it tracks objects that might have moved inside Update.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { void LateUpdate() { transform.Translate(0, Time.deltaTime, 0); } }

    

6.OnGUI

Description

OnGUI is called for rendering and handling GUI events.

This means that your OnGUI implementation might be called several times per frame (one call per event). For more information on GUI events see theEventreference. If the MonoBehaviour's enabled property is set to false, OnGUI() will not be called.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { void OnGUI() { if (GUI.Button(new Rect(10, 10, 150, 100), "I am a button")) print("You clicked the button!"); } }

 

7.Reset

Description

Reset to default values.

Reset is called when the user hits the Reset button in the Inspector's context menu or when adding the component the first time. This function is only called in editor mode. Reset is most commonly used to give good default values in the inspector.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { public GameObject target; void Reset() { if (!target) target = GameObject.FindWithTag("Player"); } }

   

8.OnDisable

   

Description

This function is called when the behaviour becomes disabled () or inactive.

This is also called when the object is destroyed and can be used for any cleanup code. When scripts are reloaded after compilation has finished, OnDisable will be called, followed by an OnEnable after the script has been loaded.

using UnityEngine;
using System.Collections;
public class ExampleClass : MonoBehaviour { void OnDisable() { print("script was removed"); } }

Another example:

// Implement OnDisable and OnEnable script functions.
// These functions will be called when the attached GameObject
// is toggled.
// This example also supports the Editor.  The Update function
// will be called, for example, when the position of the
// GameObject is changed.
using UnityEngine;
[ExecuteInEditMode] public class PrintOnOff : MonoBehaviour { void OnDisable() { Debug.Log("PrintOnDisable: script was disabled"); }
void OnEnable() { Debug.Log("PrintOnEnable: script was enabled"); }
void Update() { #if UNITY_EDITOR Debug.Log("Editor causes this Update"); #endif } }

 

 

posted @ 2018-01-21 20:05  Cyberhan  阅读(565)  评论(0编辑  收藏  举报