Unity3d combat "Little Ten Legends" series bis: Control Layer (upper)

Source: Internet
Author: User
Tags event listener fsm

Hello everyone, we are here today to study the control layer of the legend of the small ten.

Operation Steps

To make the structure beautiful, create a new empty node and move the Model,view,ctrl to the Ten directory:

Under the assets directory, under the new Home Code folder, create a new Ctrl,view C # script under code.

Modify the Ctrl.cs script

An important noun: the finite state machine is included in the toolkit provided by the game development Geek Gdgeek package.

Finite state machines can control the game's start, game, and end state transitions.

Add three states using a finite state machine. and set the state trigger function, the code is as follows:

usingUnityengine;usingSystem.Collections;usingGdgeek; Public classCtrl:monobehaviour {PrivateFSM FSM =NewFSM ();  PublicView view=NULL; //Use this for initialization    voidStart () {fsm.addstate ("begin", BeginState ()); Fsm.addstate ("Play", Playstate ()); Fsm.addstate ("End", Endsdtate ()); Fsm.init ("begin"); }    //Update is called once per frame    voidUpdate () {} state beginstate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart+=Delegate{view.begin.gameObject.SetActive (true);        }; State.onover+=Delegate{view.begin.gameObject.SetActive (false);        }; returnState ; } State Playstate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.play.gameObject.SetActive (true);        }; State.onover+=Delegate{view.play.gameObject.SetActive (false);        }; returnState ; } State Endsdtate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.end.gameObject.SetActive (true);        }; State.onover+=Delegate{view.end.gameObject.SetActive (false);        }; returnState ; }}

With the right-click on undefined functions and variables, choosing Quick Create can save a lot of time:

Create three public variables in view at the same time

usingUnityengine;usingSystem.Collections; Public classView:monobehaviour { PublicGameobject begin=NULL;  PublicGameobject play=NULL;  PublicGameobject end=NULL; //Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}}

Bind the variables on the Inspector panel of the view with start, play,end.

Then like BeginState, add playstate and EndState:

State Playstate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.play.gameObject.SetActive (true);        }; State.onover+=Delegate{view.play.gameObject.SetActive (false);        }; returnState ; } State Endsdtate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.end.gameObject.SetActive (true);        }; State.onover+=Delegate{view.end.gameObject.SetActive (false);        }; returnState ; }

Then we are ready to run the game, run it before we check begin,paly,end and then click Uncheck Show Check.

Thus, the initial state of the three interface is not displayed, and then the interface in Ctrl.cs script control, run, the correct display of the initial interface:

To add a response to the button for the start interface:

Create a new public function in CTRL:

 Public void fsmpost (string  msg) {    fsm.post (msg);}

Then in the Start screen, click the button and add the response in the Inspector panel:

Click the plus sign to add a response function, drag the Ctrl object in hierarchy to the first box, and select the Fsmpost function on the right.

In the second box, fill in the ClickStart (never write the begin as in the video tutorial), and the content in this box is the parameter value that is automatically passed in when the fsmpost is clicked.

Then add the event listener in the BeginState function value:

State.addevent ("clickstart","play");  // Get the Start button click event, go to play status

This function, the first parameter is the button click on the parameter value, the second parameter is the name of the state to jump, because before the paragraph was written

Fsm.addstate ("begin", BeginState ()); Fsm.addstate ("play " , Playstate ()); Fsm.addstate ("end", Endsdtate ());

So, once jump to play state, BeginState will perform onover response, Playstate will perform onstart response, show is to jump from the start interface to the game interface.

All right, I'll write about it here today, mainly studying two points:

    • The response function of the button
    • The jump of the interface

I am Orzangleli, thank you have been reading to now, if you see this article, please trouble reply me, give me some power update down, thank you!

Complete code: Ctrl.cs

usingUnityengine;usingSystem.Collections;usingGdgeek; Public classCtrl:monobehaviour {PrivateFSM FSM =NewFSM ();  PublicView view=NULL;  Public voidFsmpost (stringmsg)    {fsm.post (msg); }    //Use this for initialization    voidStart () {fsm.addstate ("begin", BeginState ()); Fsm.addstate ("Play", Playstate ()); Fsm.addstate ("End", Endsdtate ()); Fsm.init ("begin"); }    //Update is called once per frame    voidUpdate () {} state beginstate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart+=Delegate{view.begin.gameObject.SetActive (true);        }; State.onover+=Delegate{view.begin.gameObject.SetActive (false);        }; State.addevent ("ClickStart","Play");//Get the Start button shock event then go in play State        returnState ; } State Playstate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.play.gameObject.SetActive (true);        }; State.onover+=Delegate{view.play.gameObject.SetActive (false);        }; returnState ; } State Endsdtate () {Statewitheventmap State=NewStatewitheventmap ();//status class with a message map tableState.onstart + =Delegate{view.end.gameObject.SetActive (true);        }; State.onover+=Delegate{view.end.gameObject.SetActive (false);        }; returnState ; }}
View Code

View.cs

usingUnityengine;usingSystem.Collections; Public classView:monobehaviour { PublicGameobject begin=NULL;  PublicGameobject play=NULL;  PublicGameobject end=NULL; //Use this for initialization    voidStart () {}//Update is called once per frame    voidUpdate () {}}
View Code

Unity3d combat "Little Ten Legends" series bis: Control Layer (upper)

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.