Unity-based responsive programming (reactive programming) Getting Started

Source: Internet
Author: User

Series Catalogue

"Unity3d base" lets the object move ①--based on the Ugui mouse click Movement

"Unity3d base" let the object move ②--ugui mouse click Frame

Time boiled rain Unity3d let the object move ③-ugui dotween&unity native2d Realization

Time to boil the rain Unity3d realize 2D character animation ①ugui&native2d sequence frame animation

Time to boil the rain Unity3d realize 2D character animation ②unity2d animation System & Resource Efficiency

Background

Before the Murong Small bastard of a "deconstruction of C # game framework Uframe and talk about game architecture design", citing the content of the text

Uframe is a framework plugin for 3D developers, which itself mimics the schema pattern of MVVM (which in fact does not include the model part and the controller part is more). Because it is used for Unity3d, it provides developers with a set of editor-based visual editing tools that can be used to manage code structures and so on. What needs to be pointed out is that it is an important idea, but also an important idea in software engineering is to focus on separation (separation of CONCERN,SOC). Uframe implements this separation with control inversion (IoC)/Dependency Injection (DI), which further implements the MVVM pattern. After the 1.5 release, the Unirx library was introduced, and the idea of responsive programming was introduced.

Read tall, this article mainly wants to start from the actual, start the last sentence "and after the 1.5 version, introduced the Unirx library, introduced the idea of responsive programming." "How to use the Unirx Library in unity, using responsive programming."

Of course, to list so many new conceptual things, as a novice must be difficult to understand, of course, I hope you are very different people. Here are a few points, if you do not understand, please go to study or review, come back to see also not late.

1. Linq basics, the Nature of LINQ and the distinction and advantages of traditional imperative programming

2. Concept and difference of declarative programming and imperative programming

3. What is responsive programming

4. What is Observer mode

5. The concept of stream in software programming

Okay, pack B. Time passed, let us simply say what is responsive programming. Here also do not talk nonsense, quote a paragraph, see understand nature understand, don't understand still don't understand

What is reactive programming: Reactive programming (reactive programming), called Rx, is an asynchronous programming model that uses the LINQ style to write observer-based patterns. Simply say Rx = observables + LINQ + schedulers.

Why this is the introduction of Responsive programming Rx in game development, the answer is that the game is particularly suitable for RX programming, because the concept of time (frame) and event (UI) is widely used in games, time itself is a stream, and events are a signal based on time (not particularly accurate, tacit), which is what Rx excels at.

Realize

This article is based on the Sprite mouse movement and sequence frame animation in a series of articles, with no basic reference to the traditional implementation of the first two articles

Time to boil the rain Unity3d realize 2D character animation ①ugui&native2d sequence frame animation

Time to boil the rain Unity3d realize 2D character animation ②unity2d animation System & Resource Efficiency

The Unirx library is introduced here to implement responsive programming and declarative programming code refactoring with the following code:

Using Unityengine;
Using Unirx;

public class Playercontroller:monobehaviour
{
public float speed;
Private Vector3 movedirection;

private int currenttexture = 0;
Public sprite[] Texturearray;
Use this for initialization
void Start ()
{
Mouse control movement, update per frame
Observable.everyupdate ()
. Subscribe (_ =
{
1. Get the current position
Vector3 curenposition = this.transform.position;
2. Get Directions
if (Input.getbutton ("Fire1"))
{
Vector3 Movetoward = Camera.main.ScreenToWorldPoint (input.mouseposition);

Movedirection = movetoward-curenposition;
movedirection.z = 0;
Movedirection.normalize ();
}
3. Interpolation movement
Vector3 target = movedirection * speed + curenposition;
Transform.position = Vector3.lerp (curenposition, Target, time.deltatime);
});

Frame animation
Spriterenderer spriterenderer = getcomponent<spriterenderer> ();
Timer every 5 frames
Observable.intervalframe (5). Subscribe (_ =
{
currenttexture++;
if (currenttexture >= texturearray.length)
{
currenttexture = 0;
}
Spriterenderer.sprite = Texturearray[currenttexture];
});
}

}
Yes no, you didn't find the familiar update function, if the above function let you see is put all the code in the start, we re-construct the code, use the extraction method, look at the effect, this is the charm of declarative programming, program readability enhanced, more suitable for human thinking mode

Using Unityengine;
Using Unirx;

public class Playercontroller:monobehaviour
{
public float speed;
Private Vector3 movedirection;

private int currenttexture = 0;
Public sprite[] Texturearray;
Use this for initialization
void Start ()
{
Mouse control movement, update per frame
Playermove ();

Character Frame animations
Playeranimation ();
}

<summary>
Character Frame animation control
</summary>
private void Playeranimation ()
{
Spriterenderer spriterenderer = getcomponent<spriterenderer> ();
Timer every 5 frames
Observable.intervalframe (5). Subscribe (_ =
{
currenttexture++;
if (currenttexture >= texturearray.length)
{
currenttexture = 0;
}
Spriterenderer.sprite = Texturearray[currenttexture];
});
}

<summary>
Mouse control movement, update per frame
</summary>
private void Playermove ()
{
Observable.everyupdate ()
. Subscribe (_ =
{
1. Get the current position
Vector3 curenposition = this.transform.position;
2. Get Directions
if (Input.getbutton ("Fire1"))
{
Vector3 Movetoward = Camera.main.ScreenToWorldPoint (input.mouseposition);

Movedirection = movetoward-curenposition;
movedirection.z = 0;
Movedirection.normalize ();
}
3. Interpolation movement
Vector3 target = movedirection*speed + curenposition;
Transform.position = Vector3.lerp (curenposition, Target, time.deltatime);
});
}
}

Summarize

Remember here the Unirx two methods Observable.everyupdate,observable.intervalframe (remember the timer in the previous article, how simple is this timer), and Observablewww.getwww (a function of an asynchronous load of resources in the previous article), using declarative programming, look at the function name to know what it is, and read the document or explain what?

The article content is simple, the realization function is also simple, the function is also simple, hoped that you like.

Unity-based responsive programming (reactive programming) Getting Started

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.