Unity3d hot update book-script (2) Two-Level Differentiation

Source: Internet
Author: User

The previous article clarified what scripts we are discussing: code written in a text file, which can be loaded as resources, obtained strings, and then executed.

But why are there so many scripts in the world? But does the usage look completely different? This is because each script has its own location, and different complexity scripts will show completely different appearances. Let's take a look.

Complexity 1: Computing

The core function that executes the string is like this.

Int I = eval ("1 + 2 ");

We have a set of examples. At the end of this article, we will explain how to obtain examples.

In the program, you can execute the logic by executing the string. Then, you can change the logic of the string.

Imagine that if you want to do formula calculation, it looks like this.

Int I = eval ("Hp1 + Hp2 * 0.5 );

How can Hp1 and Hp2 come here, we need to pass the value to the script for script Calculation

Script. setvalue ("Hp1", getfromini ("Hp1 "));

Script. setvalue ("Hp2", getfromini ("Hp2 "));

Int I = script. eval ("Hp1 + Hp2 * 0.5 ");

Complexity 2: including logical branches and calls

When the complexity of the script logic reaches a certain level, embedding strings directly becomes undesirable.

We hope that, while calculating HP, the script hitting the log requires the script to call the function. At the same time, the script needs to know the day of the week, and also needs to give him a function call.

At this time, it is best to write the script into a separate text file.

Script1:

Debug. Log ("today =" + today ());

If (today () = Monday)

Return Hp1 + Hp2;

If (today () = Sunday)

Return Hp1 + Hp2 * 2;

Return Hp1 + Hp2 * 0.5;

Call code:

Int I = script. eval (getscriptfromfile ("script1 "));

 

Complexity 3: Functions and types

When the script logic becomes more complex, it is difficult to organize the logic by expression alone. At this time, the script will introduce functions and even types.

Script1:

Int calc1 ()

{

...

}

Int calc2 ()

{

...

}

Int calc3 ()

{

...

}

Int gethp ()

{

If (today () = Monday)

{

Return calc1 ();

}

If (...)

{

....

}

....

}

Call code:

Script. Build (getscriptfromfile ("script1 "));

Int I = script. eval ("gethp ();");

Complexity 4: Evolution

Further complicated, the script will be converted into something completely different.

A single script cannot complete the logic, and the script and script can be associated.

Concept of a project

Script1:

Class scriptclass1

{

Static void run ()

{

Scriptclass2 = new scriptclass2 ();

S2.xxx

...

...

...

}

}

Script2:

Class scriptclass2

{

...

....

...

}

Call code:

Script. buildproject ("scirpt1", "script2 ")

Script. Run ("scriptclass1.run ();");

Complexity 5: customer-oriented

When the script has already produced the concept of a project, it can be detached from the dedicated program host.

Like python, it starts a program with a common python.exe and becomes a language with the ability to run independently.

This series will show the integration of various complexity display scripts and unity3d

For specific code, refer to the Code on GitHub and use

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.