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

Source: Internet
Author: User
Tags switch case

1.

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 timer is the most basic of a logic, fixed frame rate in the game is only an idealized state, in most cases can not achieve fixed frame rate.

Almost all of the game logic involves timing issues, all of which involve logical control over the floating frame rate.

Time is cumulative, keep this in mind.

Main loop and easing

Use the timer to drive the value of the change, in some cases, known as easing, Dotween,itween, the name of a tween of this kind of library or plug-ins, are doing easing.

They have packaged a wide range of easing mode codes to help you save a little time.

Let's move a box 3 seconds from point A to point B, and all the easing systems are considered in such a way that 0 is the beginning and the end. So let's get our timer to complete a zero-to-one process in a few seconds, which is an easing cycle.

Using our previous code to consider, our timer added to 3 seconds, then become smaller, then to three, and then become smaller, OK. Directly to the timer+= time by a factor can change the speed of the timer change, can be adjusted to three seconds to complete from zero to one, but we have the introductory article, good code from the name, since he is called a timer, he should not from zero to one, from zero to one is the slow progress of the indicator.

Name is Lerp

As the above code completes an easing logic, the script to a cube, and then begin to fill in the different values, run to see.

No matter what kind of slow-moving library you use, it's always good to know, and that's the thing behind the slow-motion framework.

Main loop and state machine

Introductory chapter We talk about a space-time view of the problem, the concept of this program is the state, Turing machine, von Neumann machines, is the basis of the computer, the basis is said state machine.

Even from a larger scale, the high-level language looks. In the sequential execution of the program system, state machine is still the basis of programming.

We've told you before. Single-step debugging, each breakpoint, is a state.

A program is composed of one state.

From the perspective of larger scales, functional modules and program structure, state machine is still the basis of function, I am now in the main menu or Battle menu? Am I charging or spending now?

The same level of function at one time must be in a certain function, is the basis of our design.

Where the game frame starts, it must start with the state's segmentation. Some frameworks provide a higher-level pattern than a state machine, and the navigator, in fact, belongs to the state machine, but the navigator records the previous state and can be navback.

What is the Navigator mode? Just open a mobile app, click a feature, click a feature, and then press Back,back, which is the navigator.

Now because of the popularity of the Hand tour, the game interface is modeled after a lot of app design, so now the navigator design is a very mainstream design.

Since we're just a way of raising the process, we're not going to write a navigator frame out here, we're just going to explain the behavior behind the state machine.

This status code, a bit long, we have two paragraphs, a section is the update function

section is the Ongui function

Look carefully at this code, you may say, you cheat, this is a hammer of the state machine Oh, this is an if else

This is an if else, the nature of the state machine is the switch case, that is if else, unless, give him a structured design.

When you have only two or three logical states, if else, no. Then let's get him structured.

Look at the post-structured code.

We abstracted a state of expression interface, so the main code becomes very concise, this is a state machine.

But the implementation code is less concise.

With this design as the basis, add a lot of state is not afraid of, but this is just a code for interpretation.

The actual operation is certainly more complicated than the problem faced.

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.