Unity and C #: game loop (awake, start, update)

Source: Internet
Author: User
Introduction

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

Every game must and shoshould 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() AndStart().

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 shoshould 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
  • AwakeAndStartAre 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.
  • IfGameObject 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 afterAwake, Immediately before the firstUpdate, But only if the script component is enabled.
  • This means that you can useStart 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 insideUpdate() 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:
    • Moving non-physics objects
    • Simple timers
    • Processing Input
    • Update Interval times vary

Note thatUpdate Is not called on a regular timeline. If one frame takes longer to process, then the time between update callwill be different.

FixedUpdate Is a similar function to update but it has a few important differences.FixedUpdate() Is often called more frequentlyUpdate(). 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 CILS. Immediately afterFixedUpdate Is called, any necessary physics calculations are made. As such, anything that effects a Rigidbody-meaning a physics object shoshould 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
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.