1.官方脚本生命周期(地址)
一下周期顺序以2019 版为标准周期(来源于官网图片地址)
这里上面一张图是生命周期
一一讲解一下用法(做一个脚本体现一下)
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class test : MonoBehaviour
{
private void Awake()
{
Debug.Log("Awake="+this.name);
}
private void OnEnable()
{
Debug.Log("OnEnable=" + this.name);
}
private void Reset()
{
Debug.Log("Reset=" + this.name);
}
private void Start()
{
Debug.Log("Start=" + this.name);
}
private void FixedUpdate()
{
Debug.Log("FixedUpdate=" + this.name);
}
private void Update()
{
Debug.Log("Update=" + this.name);
}
private void OnDrawGizmos()
{
Debug.Log("OnDrawGizmos=" + this.name);
}
private void OnGUI()
{
Debug.Log("OnGUI =" + this.name);
}
private void OnApplicationPause(bool pause)
{
Debug.Log("pause == "+ pause+" * " + this.name);
}
private void OnApplicationQuit()
{
Debug.Log("OnApplicationQuit == " + this.name);
}
private void OnDisable()
{
Debug.Log("OnDisable == " + this.name);
}
private void OnDestroy()
{
Debug.Log("OnDestroy == " + this.name);
}
}
上面是测试代码,避开了来的API 生命周期Game和Event两个流程
测试出来结果(启动到销毁)
以上可以看出来
(来自官方解读–附上解释)
Awake: Called when the Editor is woken up.(销毁后,当场景重新加载都会执行)
上面的只跑一次的只要销毁跳转页面都会走一个完整的流程–创建 --到销毁
这个与场景加载方式和模式有关
模式:LoadSceneMode.Additive (原有的基础添加场景),LoadSceneMode.Single(销毁当前跳转新的)
加载方式:四种
上面测试使用的方法时这个: SceneManager.LoadScene(“xx场景名称”,LoadSceneMode.Single);
补充一:
使用页面跳转需要添加包 :using UnityEngine.SceneManagement;
中间有几个方法是一直执行的所有比如有什么操作可以在这些内部循环操作的地方,实现常用操作,但不能使用耗时操作,耗时可以开启协程等操作