Simple Unity Time Architecture design (Cronus Key)

Source: Internet
Author: User

Simple Unity Time Architecture design (Cronus Key)

Well, this time the topic a bit of the title party suspicion, proposed this design, is because recently played a ghost cry, which has a level called "for their own master", the task, the need to use the Cronus key to slow down the time, easy through the laser town.

After using the Cronus key, the protagonist's action is normal, transported away, attacked and so on. Other objects, such as monsters and death effects, have been slow to update. At that time I thought, how to let different objects can be updated at different frequencies?

In unity, the script updates the time.fixedupdate on time, changing its rate to just modify the Time.timescale. However, this is very "reckless" because the value is global and all game objects updated with Time.fixedupdate will be affected. For example, to implement the game's pause function, many beginners will use the object update Time.fixedupdate, and then dynamically modify the timescale. This will stop all game objects that are updated with Fixedupdate.

There seems to be no problem, but it affects all scripts that are updated with fixedupdate, such as Dfgui,ngui. I used the above method, the result is a problem, that is, the timescale set to 0, the UI of the monitoring event unexpectedly did not respond, then debugging for a long time to reflect.

Later, in order to solve this problem, as well as the local object pause, defined a lot of variables to control, feeling too troublesome. Later when the ghost cry, suddenly have the inspiration, why not write a time controller?

The first thing to know is what the requirements are: Design a time controller based on Unity's own update script to control how often all objects are updated.

My design is this, write a parent class, implement update,lateupdate,fixedupdate normal update and timing, pause control. Then all object scripts that need to control the update inherit this control class, and the corresponding method is replicated.

When used, direct control of a number of global static variables can control all the game objects that inherit this class, either to implement a pause, or to delay the local object. This can be a good extension, you can replicate different methods to achieve different object update frequency. You can play it yourself. Like the Ghost Cry, the protagonist's action does not take the effect of the time key, while the other objects will be delayed.

It's pretty big, but the code is simple (I like to use the most streamlined code to do it), using static variables for global control, simple timers, and inheritance and replication. Not much to say the code:

usingUnityengine; Public Abstract classgamecontrollor:monobehaviour{//Write a main frame to declare the updated function     Public Abstract voidFixedupdategame ();//a function that is updated according to Fixedupdate, of course you can define or add it yourself, make a copy in the subclass, and pay attention to which update your needs are based on.     Public Abstract voidUpdategame ();//a function updated by update, ditto     Public Abstract voidLateupdategame ();//a function updated according to Lateupdate, ibid.}
usingUnityengine; Public Abstract classmygamecontrollor:gamecontrollor{//Why write abstract classes, because the controller itself has no specific meaning, just control the time, and directly control the property on the line.     Private Static BOOLIsstopgame =false;//controls whether to pause     Public Static BOOLIsstopgame {Get{returnMygamecontrollor.isstopgame;} Set{Mygamecontrollor.isstopgame =value;} }    Private Static floatGameTime =0;//the time of the script update, 0 for the normal update, and 1 for the 1-second update     Public Static floatGameTime {Get{returnMygamecontrollor.gametime;} Set{Mygamecontrollor.gametime =value;} }    Private Static floatruntime =0;//Timer    Private BOOLIsontime =false; voidUpdate ()//Update Updates    {        if(isontime) {updategame (); }    }    voidFixedupdate ()//fixedupdate Update    {        if(Isontime =(Isrun ()))        {Fixedupdategame (); }    }    voidlateupdate () {if(isontime) {lateupdategame (); }    }    Private BOOLLatetime ()//Whether this judgment is up to the time of the update    {        if(GameTime <=0)return true; Runtime+=Time.fixeddeltatime; if(Runtime >=GameTime) {Runtime=0; return true; }        return false; }    Private BOOLIsrun ()//decide whether to pause    {        if(!isstopgame) {            if(Latetime ())//It is not time to decide whether to update when paused            {                return true; }        }        return false; }     Public Override voidFixedupdategame () {} Public Override voidUpdategame () {} Public Override voidLateupdategame () {}}

The code is simple, the key is the plan and the idea. How to use it? All logically updated scripts inherit this mygamecontrollor and then copy your updated method, OK, test it (this is to take care of the little white):

usingUnityengine;usingSystem.Collections; Public classPlayermove:mygamecontrollor {//inherit Mygamecontrollor, and replicate Updategame methods     Public voidMove () {//a simple moving method for observing the testTransform. Translate (NewVector3 (Random.range (-1,1), Random.range (-1,1), Random.range (-1,1))*time.deltatime); }     Public Override voidUpdategame () {//Carbon UpdategameMove (); }}

Finally is a time controller code, that is, press ESC pause, the code is very simple, and then give the small white look:

using Unityengine; using System.Collections;  Public class systemtimecontrol:monobehaviour{        void  Update ()    {        if (Input.getkeydown (keycode.escape))        {            = ! mygamecontrollor.isstopgame;     }}}

Well, about the time of the architecture design is introduced to this, the scheme is simple and practical, perhaps there is a better way to welcome the exchange. Make an advertisement: Junior software Engineering Major (Java direction), good at object-oriented design, commonly used algorithms. Self-taught js,c#, as well as Unity3d engine, specializes in front-end development and system architecture optimization.

Original address: http://www.cnblogs.com/jqg-aliang/p/4719429.html. Reprint please declare the source, thank you

Simple Unity Time Architecture design (Cronus Key)

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.