Unity and C#: Game Loop (Awake, Start, Update)

Introduction

The central component of any game, from a programming standpoint, is the game loop. It allows the game to run smoothly regardless of a user‘s input or lack thereof.

Every game must and should have a game loop because a game must continue regardless of a user‘s input. In this tip, I will be talking about two important event functions in unity3D, i.e., Awake() and Start().

Basics of Scripting

Firstly, you need to understand what is unity3D and the importance of scripting (C#/UnityScript) in game development. To do this, you have to visit my technical blog where I blogged the important aspects and concepts of unity2D/3D game development.

Learn Gaming

Time to Awake() and Start()

By now, you should have understood what are the main components and aspects of unity2D/3D game development. Just to quickly recall, in Unity2D/3D:

  • 1 Project = 1 Game
  • 1 Level = 1 Scene
  • Awake and Start are two functions that are called automatically when a script is loaded

When a scene starts, Awake() is the function which is always called (once for each object in the scene) before anyStart functions. But there are some points to remember:

  • Awake() is called only after a prefab is instantiated.
  • If a GameObject is in-active during start up, Awake is not called until it is made active, or a function in any script attached to it is called.
  • Awake is called first even if the script component is not enabled and is best used for setting up any resources between scripts and initialization.

Start() is called before the first frame update only if the script instance is enabled.

  • Start is called after Awake, immediately before the first Update, but only if the script component is enabled.
  • This means that you can use Start for anything you need to occur when the script component is enabled. This allows you to delay any part of your initialization code until it‘s really needed.

Using the Code (C#)

 Collapse | Copy Code

using UnityEngine;

using System.Collections; 

public class AwakeAndStart : MonoBehaviour
{
//Awake is called first even if the script component is not enabled and is best used for setting up any   resources between scripts and initialization. 

    void Awake ()
    {
        Debug.Log("Awake called.");
    }

//Start is called after Awake, immediately before the first Update, but only if the script component is enabled.  

    void Start ()
    {
        Debug.Log("Start called.");
    }
} 

Time for Update() and FixedUpdate()

When you‘re keeping track of game logic and interactions, animations, camera positions, etc., there are a few different events you can use. The common pattern is to perform most tasks inside the Update() function, but there are also other functions you can use.
Update is the most commonly used function in unity. It‘s called once per frame on every script that uses it. Almost anything that needs to be changed or adjusted happens here.

  • Called every frame
  • Used for regular updates such as:
    • Moving Non-Physics objects
    • Simple Timers
    • Receiving Input
    • Update interval times vary

Note that Update is not called on a regular timeline. If one frame takes longer to process, then the time between update calls will be different.

FixedUpdate is a similar function to update but it has a few important differences. FixedUpdate() is often called more frequently than Update(). It can be called multiple times per frame, if the frame rate is low and it may not be called between frames at all if the frame rate is high. FixedUpdate is called on a regular timeline and will have the same time between calls. Immediately after FixedUpdate is called, any necessary physics calculations are made. As such, anything that effects a rigidbody-meaning a physics object should be executed in fixed update rather than update.

In short:

  • Called every physics step
  • FixedUpdate intervals are consistent
  • Used for regular updates such as adjusting physics (Rigibody) objects

If you are planning to change the state of a physics GameObject-FixedUpdate()
Non-Physics GameObject-Update()

Using the Code(C#)

 Collapse | Copy Code

[code language="csharp"]//Run this code in UnityEditor and you will understand the differences.
using UnityEngine;
using System.Collections;

public class UpdateAndFixedUpdate : MonoBehaviour
{
//Logs a regular time interval say :0.02s
    void FixedUpdate ()
    {
        Debug.Log("FixedUpdate time :" + Time.deltaTime);
    }

//Logs inconsistent time intervals
    void Update ()
    {
        Debug.Log("Update time :" + Time.deltaTime);
    }
}[/code]

References

  • Script Reference: Awake
  • Script Reference: Start
  • Unity3d.com

Unity and C#: Game Loop (Awake, Start, Update),布布扣,bubuko.com

时间: 2024-08-26 19:10:08

Unity and C#: Game Loop (Awake, Start, Update)的相关文章

Unity学习疑问记录之Awake和Update

Awake() 当一个脚本实例被载入时Awake被调用. Awake用于在游戏开始之前初始化变量或游戏状态.在脚本整个生命周期内它仅被调用一次.Awake在所有对象被初始化之后调用,所以你可以安全的与其他对象对话或用诸如 GameObject.FindWithTag 这样的函数搜索它们.每个游戏物体上的Awke以随机的顺序被调用.因此,你应该用Awake来设置脚本间的引用,并用Start来传递信息.Awake总是在Start之前被调用.它不能用来执行协同程序. Start() Start仅在Up

Tip8:Unity中诸如 Awake() Start() Update()等函数的 执行顺序

Unity脚本中有很多的事件函数,下面是各种函数的执行顺序: 1.reset(); 2.Awake(); 3.OnEnable; 4.OnLevelWasLoaded(); 5.Start(); 6.OnApplicationPause(); 7.FixedUpdate(); 8.Update(); 9.LateUpdate(); 10.Rendering(渲染)类 11.Coroutines(协调程序)类 12.OnDestroy(); 13.OnApplicationQuit(); 14.O

unity, editorWindow update计时

对于editorWindow,Time.deltaTime不起作用,所以需用下面方法对update进行计时: public class myEditorWindow : EditorWindow{ public float m_lastUpdateTime=0; public myEditorWindow(){        wantsMouseMove = true;    } public void Update(float dt){ //to do } private void Updat

Unity 最佳实践

转帖:http://www.glenstevens.ca/unity3d-best-practices/ 另外可以参考:http://devmag.org.za/2012/07/12/50-tips-for-working-with-unity-best-practices/ 组件的拆分可以参考:http://gameprogrammingpatterns.com/component.html,此网站上列出了很多的游戏开发中的设计模式,非常值得认真研读. 论坛里的讨论也很有价值:https://

unity简单设计模式---CoroutineScheduler

内容 1 描述 2 使用 3 代码 3.1 CoroutineScheduler.js 3.2 CoroutineNode.js 3.3 CoroutineSchedulerTest.cs 3.4 CoroutineScheduler.cs 3.5 CoroutineNode.cs 4 Additional Implementation C# 4.1 Sample Usage 4.2 Addition to CoroutineScheduler.cs 4.3 Addition to Corout

(转)Unity笔记之编辑器(UnityEditor)

在使用unity3d的过程中,时常会需要从场景中寻找或者调用一个对象,而Unity就提供了一个贴心的功能——拖拽.用鼠标拖一下中比写堆代码直观的多吧!但是Unity提供的远远不止这一丢丢,下面我们来简单了解下UnityEditor部分的内容. 编辑器最最基本的用法呢就是编辑Inspector. 而Inspector中最最基本的就是把字段显示出来.给几个例子: [code]csharpcode: using UnityEngine; using System.Collections; // 这里没

使用Unity3D50个技巧-50 Tips for Working with Unity (Best Practices)

原文: 50 Tips for Working with Unity (Best Practices) About these tips These tips are not all applicable to every project. They are based on my experience with projects with small teams from 3 to 20 people. There's is a price for structure, re-usabilit

Unity面试题整理

Unity技术面试题 一:什么是协同程序?答:在主线程运行时同时开启另一段逻辑处理,来协助当前程序的执行.换句话说,开启协程就是开启一个可以与程序并行的逻辑.可以用来控制运动.序列以及对象的行为. 二:Unity3d中的碰撞器和触发器的区别?答:碰撞器是触发器的载体,而触发器只是碰撞器身上的一个属性.当Is Trigger=false时,碰撞器根据物理引擎引发碰撞,产生碰撞的效果,可以调用OnCollisionEnter/Stay/Exit函数:当Is Trigger=true时,碰撞器被物理引

Unity学习笔记—— 常用脚本函数

1. Awake() Start() Update() FixedUpdate()  unity 是单线程的 , 对于unity后台执行脚本,每个脚本的Awake.Update.LateUpdate.FixedUpdate,方法在后台都有一个总汇.把每个 后台的Awake() { 脚本0中的Awake(); 脚本1中的Awake(): 脚本2中的Awake(); } 后台的方法 Awake.Update.LateUpdate.FixedUpdate等等都是按照顺序,等所有子脚本中的Awake执行