UnityAPI学习之 事件函数(生命周期函数)的整体调用流程及细节

 事件函数(生命周期函数)的整体调用流程

Reset()

这个函数会在用户首次添加该组件时或单击 Reset 按钮时被调用,且只在编辑器生效,可用于保存默认值

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    private void Reset()
    {
        Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");
    }
}

如图可发现在程序运行之前就已经调用了Reset();

Awake()

此函数在脚本实例被载入时调用,仅被调用一次

调用时机:1. 任何有Awake()函数的脚本在场景加载的时候调用

                  2. 挂在拥有Awake()函数的脚本的物体从睡眠到显现的瞬间调用

                  

                  3. 在使用instantiate函数创建拥有Awake()函数的脚本的物体时调用

调用生存周期:脚本实际生存的期间

注:当有多个组件拥有Awake()函数时,每个组件调用Awake()函数的顺序不一定,但可以通过人为的编写确定调用顺序

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    private void Reset()
    {
        Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");
    }
    private void Awake()
    {
        Debug.Log("Awake");
    }
}

OnEnable()

当脚本被启用时调用一次,当游戏对象被禁用时,仅监听响应此函数

调用时机:1. 任何有OnEnable()函数的脚本在场景加载的时候调用

                  2. 挂在拥有OnEnable()函数的脚本的物体从睡眠到显现的瞬间调用

注:与Awake()不同的是,OnEnable()可被调用多次

        Awake()与OnEnable()可以相互配合进行某些值的初始化(如怪物初始位置)

private void Awake()
    {
        monsterInitPos = monsterTrans.localPosition;
        lunaInitPos = lunaTrans.localPosition;
    }

    private void OnEnable()
    {
        monsterSr.DOFade(1,0.01f);
        lunaSr.DOFade(1,0.01f);
        lunaTrans.localPosition = lunaInitPos;
        monsterTrans.localPosition = monsterInitPos;
    }

示例:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    private void Reset()
    {
        Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");
    }
    private void Awake()
    {
        Debug.Log("Awake");
    }
    private void OnEnable()
    {
        Debug.Log("OnEnable");
    }
}

现在加载场景并多次对该物体进行激活与失活操作

Start()

 调用时机:只会在游戏物体或者脚本组件首次被激活时被调用一次

调用顺序:Awake()、OnEnable()之后,Update()之前

注:Awake()和Start()进行UI的实例化中,可以用Awake()加载资源组,Start()实例化资源组

关于变量赋值顺序的探究

如下列代码所示,创建int类型变量a1赋值100,因为其修饰符为public,所以我们可以在外部脚本或者Unity中对其进行重新赋值,因此探究其赋值顺序对以后的游戏开发很有必要

变量赋值顺序(左边最先赋值,右边最后赋值):

变量声明并直接赋值>检视面板赋值>Awake>OnEnable>外部赋值>Start

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    public int a1 = 100;
    private void Reset()
    {
        Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");
    }
    private void Awake()
    {
        Debug.Log("Awake");
    }
    private void OnEnable()
    {
        Debug.Log("OnEnable");
    }
}

现在进行类内部的变量赋值覆盖的实验

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    public int a1 = 100;
    private void Reset()
    {
        /*Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");*/
    }
    private void Awake()
    {
        /*Debug.Log("Awake");*/
        a1 = 90;
        Debug.Log(a1);
    }
    private void OnEnable()
    {
        /*Debug.Log("OnEnable");*/
        a1 = 80;
        Debug.Log(a1);
    }
    private void Start()
    {
        a1 = 70;
        Debug.Log(a1);
    }
}

由运行结果可知,类内部变量赋值的顺序按照代码的执行顺序进行

现在进行外部调用改变变量值的实验

创建外部脚本NO2_External对变量进行改变

using System.Collections;
using System.Collections.Generic;
using UnityEngine;
//用于外部赋值的探索
public class NO2_External : MonoBehaviour
{
    public NO2_EventFunction ev;
    // Start is called before the first frame update
    void Start()
    {
        ev.a1 = 60;
        Debug.Log(ev.a1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

而当重启Unity再次进行场景激活时会出现以下情况

此时的赋值顺序为

变量声明并直接赋值>检视面板赋值>Awake>OnEnable>Start>外部赋值

对于变量赋值的不同情况的详解

有些情况在脚本激活时,会出现外部脚本赋值的变量在最后的情况,那是因为外部脚本和内部脚本放在同一个目录下,再场景激活时,两个脚本是一起被激活的,这时候外部脚本中的Start和内部脚本Start的赋值情况不可控

现在重新创建外部脚本NO2_External02,并重新创建对象ev02改变内部脚本的a1的值

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_EventFunction : MonoBehaviour
{
    public int a1 = 100;
    private void Reset()
    {
        /*Debug.Log("调用了Reset方法");
        Debug.Log("攻击重置为默认值");*/
    }
    private void Awake()
    {
        /*Debug.Log("Awake");*/
        a1 = 90;
        Debug.Log(a1);
    }
    private void OnEnable()
    {
        /*Debug.Log("OnEnable");*/
        a1 = 80;
        Debug.Log(a1);
    }
    private void Start()
    {
        a1 = 70;
        Debug.Log(a1);
    }
}
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NO2_External02 : MonoBehaviour
{
    // Start is called before the first frame update
    public NO2_EventFunction ev02;
    void Start()
    {
        ev02.a1 = 10;
        Debug.Log(ev02.a1);
    }

    // Update is called once per frame
    void Update()
    {
        
    }
}

结果如下 

在Unity重启后还会发生以上情况

在实际的脚本编写过程中,为了规避发生以上上情况对我们的游戏数据造成混乱,我们一般对一个对象赋值,或者说想要从外部对一个对象的属性或者成员变量的值进行更改的前提是,这个对象已经有了(意味着start方法已经调用),所以外部赋值会后于start方法进行调用

以本博主的理解就是改变脚本的激活顺序

  • 28
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值