总结1_1:常用周期函数

1.周期函数调用顺序(PS:吐槽一句,不知道是不是转发的缘故,百度上好多写错的,把FixedUpdate->Update写反了):

Awake->OnEnable->Start->FixedUpdate->Update->LateUpdate->OnGUI->OnDisable->OnDestroy

1)挂载脚本:Reset
①Reset是脚本被挂上的时候执行一次,其他时候不执行,严格来说不算周期函数

2)物体(脚本所挂的游戏对象)加载:Awake->OnEnable->Start
①Awake:加载的时候第一个运行的方法,适合用来构建单例,还可以用来进行一些初始化操作。不知道Awake和OnEnable的顺序是不是被unity存在栈里了,最后挂的最先执行。

//单例
//然后在其他类里调这个类的实例:ClassA.instance.DebugCtr();
public class ClassA{
	public static ClassA instance;
	private void Awake()
	{
		if(instance == null)
		{
			instance = this;
		}
	}
	public void DebugCtr()
	{
		Debug.Log("This is ClassA instance");
	}
}

②OnEnable:物体显示时被调用
③Start:如果不在Awake里初始化,就可以放在在Start里;Start里面可以用来开启协程

3)物体加载完成:FixedUpdate->Update->LateUpdate->OnGUI
①FixedUpdate:每固定帧率(默认是0.02s,即1/50 s)调用一次,一般用作物理更新。自定义可以在Edit->Project Settings->Time里修改Fixed Timestep的数值
《总结1_1:常用周期函数》
②Update:每帧调用一次(一般趋近0.02s,跟硬件性能和项目内容有关),用来检测事件;
③LateUpdate:与Update同步,紧随其后。摄像机的跟随可以放在里面。
④OnGUI:unity原生的GUI就需要在这里面写

	protected void OnGUI()
    {
        //DebugCtr(ref isOnGUI, "OnGUI()");
        GUILayout.BeginArea(new Rect(10,1,200,150));
        GUI.Box(new Rect(10, 1, 100, 100),"This is Unity GUI");
        GUILayout.EndArea();
    }

《总结1_1:常用周期函数》

4)物体销毁:OnDisable->OnDestroy
①OnDisable:物体隐藏时调用,可以用来注销事件
②OnDestroy:物体被销毁时调用,可以用来注销事件

5)看到这里的童鞋有没有想过多个脚本之间,周期函数的规律?
(有脚本A、B,A先挂,B后挂)

B:Awake->B:OnEnable ->A:Awake->A:OnEnable->
B:Start->A:Start->B:FixedUpdate->A:FixedUpdate->B:Update->A:Update->B:LateUpdate->A:LateUpdate->B:OnGUI->A:OnGUI
A:OnDisable->B:OnDisable ->A:OnDistroy->B:OnDistroy
(注意,我分成三段了!!! )
总结:
①Awake和OnEnable先后一起出现;
②物体加载的时候,后挂的先执行;物体销毁的时候,先挂的先执行。

《总结1_1:常用周期函数》

—————最后,挂上测试的代码 0.0————————————————-

//**基类**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NewBehaviourScriptTestBase : MonoBehaviour {
    protected bool isStart, isAwake, isOnEnable, isNewBehaviourScriptTest,
        isUpdate, isLateUpdate, isFixedUpdate, isOnDisable, isOndestroy, isOnGUI,isReset;

    protected virtual void  DebugCtr(ref bool isDebug, string msg)
    {

    }
    protected void Reset()
    {
        DebugCtr(ref isReset, "Reset()");
    }
    protected void Awake()
    {
        DebugCtr(ref isAwake, "Awake()");
    }
    protected void Start()
    {
        DebugCtr(ref isStart, "Start ()");
    }
    protected void Update()
    {
        DebugCtr(ref isUpdate, "Update ()");
    }
    protected void LateUpdate()
    {
        DebugCtr(ref isLateUpdate, "LateUpdate()");
    }
    protected void FixedUpdate()
    {
        DebugCtr(ref isFixedUpdate, "FixedUpdate()");
    }
    protected void OnGUI()
    {
        DebugCtr(ref isOnGUI, "OnGUI()");
    }
    protected void OnDestroy()
    {
        DebugCtr(ref isOndestroy, "OnDestroy()");
    }
    protected void OnDisable()
    {
        DebugCtr(ref isOnDisable, "OnDisable()");
    }
    protected void OnEnable()
    {
        DebugCtr(ref isOnEnable, "OnEnable()");
    }
}

//**子类A**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClassA : NewBehaviourScriptTestBase {

    protected override void DebugCtr(ref bool isDebug, string msg)
    {
        if (!isDebug)
        {
            Debug.Log("A: " + msg);
            isDebug = true;
        }
    }
}
//**子类B**
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class ClassB : NewBehaviourScriptTestBase {

    protected override void DebugCtr(ref bool isDebug, string msg)
    {
        if (!isDebug)
        {
            Debug.Log("B: " + msg);
            isDebug = true;
        }
    }
}

    原文作者:_thought
    原文地址: https://blog.csdn.net/LM_MT/article/details/78689255
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞