MonoBehaviour Lifecycle(生命周期/脚本执行顺序)
脚本执行顺序
前言
搭建一个示例来验证Unity脚本的执行顺序,大概测试以下部分:
- 物理方面(Physics)
- 渲染(Scene rendering)
- 输入事件(InputEvent)
流程图
Unity文档:https://docs.unity3d.com/Manual/ExecutionOrder.html
原图地址:https://docs.unity3d.com/uploads/Main/monobehaviour_flowchart.svg
测试场景
搭建场景
根据上图中的脚本生命周期,我编写三个脚本来测试部分阶段的生命周期:
- Logs.cs 输出日志(可选)
- TestInputEvent.cs(Input输入事件)
- TestPhysicOrder.cs(物理事件执行顺序)
- TestSceneRender.cs(Render顺序)
创建一个空的场景,创建三个Gameobject,每个gameobject上分别绑上要测试的脚本。每次测试不同的功能,分别激活不同的gameobject
打印调用堆栈脚本
可以打印调用方法的堆栈,包括方法名,文件名
using System;
using System.Diagnostics;
public class Logs {
/// <summary>
/// 打印调用者的方法名
/// </summary>
public static void DoLog()
{
StackTrace st = new StackTrace(true);
//获取当前调用的方法名
StackFrame stackFrame = st.GetFrame(1);
//var callInfo = string.Format("{0}:{1}.{2}",stackFrame.GetFileName(),stackFrame.GetFileLineNumber(),stackFrame.GetMethod().Name);
var callInfo = stackFrame.GetMethod().Name.ToString();
DoLog(callInfo);
}
public static void DoLog(string szMsg, params object[] args)
{
string log = string.Format("[{0}]{1}", DateTime.Now.ToString("HH:mm:ss.ffff"), string.Format(szMsg, args));
UnityEngine.Debug.Log(log);
}
}
物理测试
测试脚本
测试脚本中写了Unity的各个脚本函数,大致内容如下:
using System;
using UnityEngine;
using System.Collections;
public class TestPhysicOrder : MonoBehaviour
{
// Reset to default values
public void Reset()
{
Logs.DoLog();
}
// Awake is called when the script instance is being loaded
public void Awake()
{
StartCoroutine(YieldOneFrame());
StartCoroutine(YieldEndOfFrame());
StartCoroutine(YieldWaitForFixedUpdate());
Logs.DoLog();
}
// This function is called when the object becomes enabled and active
public void OnEnable()
{
Logs.DoLog();
}
// Use this for initialization
void Start()
{
Logs.DoLog();
}
// This function is called every fixed framerate frame, if the MonoBehaviour is enabled
public void FixedUpdate()
{
Logs.DoLog();
}
// Update is called once per frame
void Update()
{
Logs.DoLog();
}
IEnumerator YieldWaitForFixedUpdate()
{
yield return new WaitForFixedUpdate();
Logs.DoLog("WaitForFixedUpdate");
}
IEnumerator YieldOneFrame()
{
yield return 1;
Logs.DoLog("YieldOneFrame");
}
IEnumerator YieldEndOfFrame()
{
yield return new WaitForEndOfFrame();
Logs.DoLog("YieldEndOfFrame");
}
// LateUpdate is called every frame, if the Behaviour is enabled
public void LateUpdate()
{
Logs.DoLog();
}
// This function is called when the behaviour becomes disabled or inactive
public void OnDisable()
{
Logs.DoLog();
}
// This function is called when the MonoBehaviour will be destroyed
public void OnDestroy()
{
Logs.DoLog();
}
// Sent to all game objects when the player gets or looses focus
public void OnApplicationFocus(bool focus)
{
Logs.DoLog();
}
// Sent to all game objects when the player pauses
public void OnApplicationPause(bool pause)
{
Logs.DoLog();
}
// Sent to all game objects before the application is quit
public void OnApplicationQuit()
{
Logs.DoLog();
}
}
测试结果
开始部分截图
结束部分截图
Render测试
测试脚本
using System;
using UnityEngine;
public class TestSceneRender : MonoBehaviour
{
// OnPreCull is called before a camera culls the scene
public void OnPreCull()
{
Logs.DoLog();
}
// OnPreRender is called before a camera starts rendering the scene
public void OnPreRender()
{
Logs.DoLog();
}
// Callback that is sent if an associated RectTransform has it's dimensions changed
public void OnRectTransformDimensionsChange()
{
Logs.DoLog();
}
// Callback that is sent if an associated RectTransform is removed
public void OnRectTransformRemoved()
{
Logs.DoLog();
}
// OnRenderImage is called after all rendering is complete to render image
public void OnRenderImage(RenderTexture source, RenderTexture destination)
{
Logs.DoLog();
}
// OnRenderObject is called after camera has rendered the scene
public void OnRenderObject()
{
Logs.DoLog();
}
// OnWillRenderObject is called once for each camera if the object is visible
public void OnWillRenderObject()
{
Logs.DoLog();
}
// Implement this OnDrawGizmosSelected if you want to draw gizmos only if the object is selected
public void OnDrawGizmos()
{
Logs.DoLog();
}
// OnGUI is called for rendering and handling GUI events
public void OnGUI()
{
Logs.DoLog();
}
}
测试结果
当把测试脚挂在Cube或者Camera上,会执行的函数是不相同的。
绑在Camera上
绑在Cube上
MonoBehaviour Lifecycle(生命周期/脚本执行顺序)的更多相关文章
- Unity脚本生命周期与执行顺序
文章目录 脚本生命周期 MonoBehavior生命周期图 脚本执行顺序 自定义执行顺序 在Unity中,脚本可以理解为附加在游戏对象上的用于定义游戏对象行为的指令代码.必须绑定在游戏对象上才能开始它 ...
- 附实例!图解React的生命周期及执行顺序
本文由云+社区发表 作者:前端林子 1.七个可选的生命周期 可以结合下图来看: (1) componentWillMount() 仅在render()方法前被调用一次,如果在该方法中调用了setSta ...
- Spring的Bean的生命周期方法执行顺序测试
通过一个简单的Maven工程来演示Spring的Bean生命周期函数的执行顺序. 下面是工程的目录结构: 直接贴代码: pom.xml文件内容: <?xml version="1.0& ...
- Unity3D脚本的生命周期(执行顺序)
Unity脚本中有许多固定的函数 例如Start();Update(); 而这些函数都有固定的执行顺序 搞清楚这些函数的执行顺序 对于我们理清代码的逻辑就显得尤为重要 举个简单的例子 //脚本A pu ...
- vue中的父组件及子组件生命周期的执行顺序
一.没有任何任何显示与隐藏限制条件的情况下: 1.运行的顺序依次是: 父组件created→父组件beforeMounted→子组件created→子组件beforeMounted→子组件mounte ...
- 简单记录一下vue生命周期及 父组件和子组件生命周期钩子执行顺序
首先,vue生命周期可以用下图来简单理解 当然这也是官方文档的图片,详细的vue周期详解请参考这里 然而当同时存在父子组件的时候生命周期钩子是如何执行的呢? 请看下文: 加载渲染过程父beforeCr ...
- react教程 — 组件的生命周期 和 执行顺序
一.组件执行的生命周期: 参考 https://www.cnblogs.com/soyxiaobi/p/9559117.html 或 https://www.c ...
- Vue 的父组件和子组件生命周期钩子执行顺序是什么
加载渲染过程父beforeCreate->父created->父beforeMount->子beforeCreate->子created->子beforeMount-&g ...
- Unity3D_02_基类MonoBehaviour/自带函数以及脚本执行的生命周期
导引: 其中Time,Input,Physics都是Unity中的全局变量.GameObject是游戏中的基本物件.GameObject是由Component组合而成的,GameObject本身必须有 ...
随机推荐
- git命令分类图
- 2、C#核心编程结构
本学习主要参考Andrew Troelsen的C#与.NET4高级程序设计,这小节主要述说以下几个东西: Hello World的Main方法: 利用VS2010新建一个控制台应用程序Hello W ...
- js判断本机是否已安装app
需求:在浏览器或者app webview中打开的页面,js判断本机是否已安装搜狐新闻客户端. 一.微信 1.分享——好友/朋友圈,feed会有搜狐新闻标记,打开url后缀参数isappinstalle ...
- 注意 AppResLib.dll.*.mui 的生成操作应该为 Content
为 Windows Phone 8 App 添加本地化的时候,发现修改 AppResLib.dll.*.mui 后不仅没有其变化,还发现修改它导致它失效.通过对比代码发现,问题原因是 AppResLi ...
- iOS冰与火之歌(番外篇) - 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权
iOS冰与火之歌(番外篇) 基于PEGASUS(Trident三叉戟)的OS X 10.11.6本地提权 蒸米@阿里移动安全 0x00 序 这段时间最火的漏洞当属阿联酋的人权活动人士被apt攻击所使用 ...
- C# if中连续几个条件判断
C# if中连续几个条件判断 1.if (条件表达式1 && 条件表达式2) 当条件表达式1为true时 using System; using System.Collections. ...
- 严重: Exception sending context initialized event to listener instance of class
问题描述:Exception sending context initialized event to listener instance of class org.springframework.w ...
- jdk链表笔记
LinkedList LinkedList是双链表,并且有头尾指针 数据结构 public class LinkedList extends AbstractSequentialList implem ...
- 摆脱DOM操作,从TodoMVC看angularJS
取代jQuery? 我很久之前便听说了angularJS的大名,之前的leader也经常感叹angularJS的设计如何如何精妙,可叹一直没有机会深入了解,国庆长假因为没钱出游,倒是可以对他做一个了解 ...
- BFC深入理解
BFC 在上一篇文章中,清除浮动方法解析,我们谈及了一些使用css属性解决浮动带来的影响.但是在解决浮动带来的影响的方法中,如果细心思考,会产生如下疑问: 为什么overflow可以清除浮动带来的影响 ...