Simba College-unity-Jian-Ying's C # enhancement (i) main loop

Source: Internet
Author: User

This is a beta version.

Simba College: a fair and aboveboard.

Recently left me to serve more than three years of the company, because of an endless number of times encountered the old problem, no money.

Do not know what to do before the time, the chance of coincidence and the click of the network of Wu total chat, found the VR game this window, right here when the click Network to play an advertisement bar. ^_^

Look back at desperate this period of time, abandoned a lot, and then come back to talk to you about C #.

I did an introductory series before and said something casually. Feel the introduction of such a child is similar, it should be slightly improved.

?

What's the difference between writing a program, writing an app, and writing a game? The execution time of a program is very short, and the execution time of an application is very long, that's all.

?

There is a concept of a frame in the game.

This concept is known to all, the analogy, it is the movie film of the lattice. One cell appears for a period of time, and then one is replaced.

Movie 24 in a second, the game is 30 frames a second, 60 frames.

Film film is a lattice in front of the camera show, the game logic is in the update in one frame of the execution.

So how is the driver of the update function done?

?

This is the main loop.

Let's take a look at a basic console program

Well, HelloWorld, we're already familiar with this program. A flash of that version. Because he executes and then it's over.

?

So what about this version?

You would say, I go, you wrote a dead loop.

?

Yes, the main loop is a dead loop. With this dead loop, a program can gradually become an application, a game.

Let's take the update out and look a little familiar.

?

Another onstart, isn't it more familiar?

That's the way unity is monobehaviour.

?

In any program there is a main loop, in the common interface framework, usually hides the main loop, leaving the event-type interface.

The main loop is simple, just looks like this.

The game program usually has a few types of events, and most of the logic needs to be organized from the main loop level, which means you need to understand the main loop very deeply and can create many patterns of logic from the main loop.

Let's start by introducing a few common

Main Loop and timer

Think of the following requirements, you need to print a log every three seconds, how to do this three seconds of timing?

The movie is 24 lattice, 24 frames per second, stable, shine. You know a ballpark figure by a few frames.

But we all know that the frame rate of the game is jumping, the time of each frame is not fixed.

Unity provides a parameter that tells you when the last frame started, the time elapsed from the beginning of the frame, the unit is seconds time.deltatime;

Because every frame starts at the beginning of the previous frame interval to you. Just add them up, it's a timer. You can use the stopwatch to confirm the program, his performance is as accurate as your stopwatch, exactly.

This is a timer, a timer driven by a main loop with an unstable frame rate. He just timed it and didn't do anything, so let's get him to do something.

This type of code, which is the first common in the loop pattern, never has to look back at the code once it is understood. If you have a hard time with this code, it will take a bit of effort to get you to understand the deltatime thoroughly.

?

?

?

?

Farewell, Hakuna Matata Tata.

Simba College-unity-Jian-Ying's C # enhancement (i) main loop

Related Article

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.