Unity uses MVC mode

Source: Internet
Author: User

These two days to see the next boss of the project, he is basically using the MVC pattern, write very well, here I understand the record down

Model: Entity objects (classes that correspond to database records)

View: Views

Presenter (Controller): Business processing

Present object in view, model and view object in present

The interaction of the UI in the view invokes the method that the present handles (when the method finishes processing, it invokes the view after processing (the changed model object is passed), and the view display changes the displayed content)

The structure is the following:

IPresenter:

namespace nginar{    publicinterfacewhereclass    {           void  Init ();}    }

IView:

namespace nginar{    publicinterfacewhereclass    {        /* */ void Setpresenter (T presenter);}}              

Iglassview:

namespacenginar{ Public InterfaceIglassview:iview<iglasspresenter>    {        //sets the contents of the current step for displaying        voidSetstepcontent (stringcontent); //Set Current Step        voidSetcurrentstep (intcurrentstep); }     Public InterfaceIglasspresenter:ipresenter<iglassview>    {        //Current Step        BOOLstepcurrent (); //Next Step        BOOLStepnext (); //Previous step        BOOLSteplast (); voidOnstepchange (intCurrenttaskid,intcurrentstepid); }}

Step:

namespace nginar{    publicclass  Step    {        publicintget  set;}          Public string Get Set ; }          Public string Get Set ; }    }}

Fileutil:

namespacenginar{//Read File tool     Public classFileutil {//load script file to save step contents         Public Static voidLoadmodel (Textasset asset,list<step>steps) {            string[] lines = Asset.text.Split ("\ n"[0]);  for(inti =0; I < lines. Length; i++)            {                string[] cols = Lines[i]. Split (" "[0]); Step Step=NewStep (); Step.id=int. Parse (cols[0]); Step.content= cols[1]; Step.type= cols[2]; Steps.            ADD (step); } logutil.i ("size"+steps.        Count); }    }}

Logutil:

 Public classlogutil{Private Static BOOLLogi =true; Private Static BOOLLOGW =true; Private Static BOOLLOGE =true;  Public Static voidIstringmess) {        if(Logi) {Debug.Log (mess); }    }     Public Static voidWstringmess) {        if(LOGW) {debug.logwarning (mess); }    }     Public Static voidEstringmess) {        if(LOGE) {debug.logerror (mess); }    }}

MainView (Inherit Iglassview):

UI events call the corresponding processing method in present:

   //new Logical Management Object Mvp-pPrenseter =NewMainpresenter (Asset, This); //set the listener callback for the Refresh current step buttonBtn_text.onClick.AddListener (Delegate() {prenseter.            Stepcurrent ();            }); //set a listener callback for the previous buttonBtn_last.onClick.AddListener (Delegate() {prenseter.            Steplast ();            }); //set the listener callback for the Next buttonBtn_next.onClick.AddListener (Delegate() {prenseter.            Stepnext (); });

Iglasspresenter (Inherit Iglasspresenter):

The UI correspondence processing method in present changes the model, and the view display after processing the method display in the call view is finished:

  Public BOOLSteplast () {Currentstep--; if(Currentstep <0) {Currentstep=0; return false; } onstepchange (0, Currentstep); return true; }        //Next Logical         Public BOOLStepnext () {if(Steps. Count <=0)            {                return false; } currentstep++; if(Currentstep >=steps. Count) {Currentstep= steps. count-1; return false; } onstepchange (0, Currentstep); return true; }        //Step Change Call         Public voidOnstepchange (intCurrenttaskid,intcurrentstepid) {             This. Currentstep =Currentstepid; Logutil.i ("Currentstepid"+currentstepid); Currentstepcontent=steps[currentstep].content;            View.setstepcontent (currentstepcontent);        View.setcurrentstep (Currentstep); }

There are also ways to load files in present:

The file format is this: 1 1. Possible consequences of the risk: collapse, high fall, road disruption, cross-shelf closure, etc. image1

The above fileutil can be parsed and assigned to the Step (model) object

View Display Method (parameter is the information in model):

Other UI or models are loaded in addition to displaying the model information

 Public void setstepcontent (string  content)        {            Debug.Log ("content: "+content);             // button text set to step content            Text.text = content;        }

   Public voidSetcurrentstep (intcurrentstep) {Currentstep= Currentstep +1; if(Application.loadedLevelName.Equals ("Kyj"))            {                if(Currentmodel! =NULL) Destroy (Currentmodel); Play ("kyj_"+currentstep); Loadprefab ("prefabs/kyj_step/kyj_"+Currentstep, Objecttarget.transform); if(Currentstep = = One) endstep.setactive (true); Elseendstep.setactive (false); }}

Unity uses MVC mode

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.