Mission System Writing in Unity

Source: Internet
Author: User

Mission system has always been a very important game system, it has been accompanied by the game around, the player through the task to earn equipment, to upgrade, so that the task system is very important, it is worth our good to structure. The task system in the phone is not as complex as the PC side, and it is relatively simple to implement in unity due to the syntactic functions of delegates and events. Then we begin to implement the task system.

The first step: we need to define the task class, our design idea is that a task object is a coroutine, the task can start, stop, pause, if you try to start a task that is stopped or is terminated it is wrong.

Here's the code build:

public class task{public bool running {get { Return task. Running;}} Public bool paused {get {return task. Paused;}} Public delegate void finishedhandler (bool manual);p Ublic event finishedhandler  finished;public task (ienumerator c, bool autostart = true) {task =  taskmanager.createtask (c); task. Finished += taskfinished;if (AutoStart) Start ();} Public void start () {task. Start ();} Public void stop () {task. Stop ();} Public void pause () {task. Pause ();} Public void unpause () {task. Unpause ();} Void taskfinished (bool manual) {finishedhandler handler = finished;if (handler !=  null) handler (manual);} Taskmanager.taskstate task;} Step two: We need a management class to create the task. 
class taskmanager : monobehaviour{public class taskstate{public bool  running {get {return running;}} public bool paused  {get {return paused;}} Public delegate void finishedhandler (bool manual);p Ublic event finishedhandler  Finished;IEnumerator coroutine;bool running;bool paused;bool stopped;public  Taskstate (ienumerator c) {coroutine = c;} Public void pause () {paused = true;} Public void unpause () {paused = false;} Public void start () {Running = true;singleton. Startcoroutine (Callwrapper ());} Public void stop () {stopped = true;running = false;} Ienumerator callwrapper () {Yield return null;ienumerator e = coroutine;while ( Running)  {if (paused) yield return null;else {if (e != null && e. MoVenext ())  {yield return e.current;} Else {running = false;}}} Finishedhandler handler = finished;if (Handler != null) handler (stopped);}} Static taskmanager singleton;public static taskstate createtask (IEnumerator  Coroutine) {if (singleton == null)  {gameobject go = new gameobject (" TaskManager "); Singleton = go. Addcomponent<taskmanager> ();} Return new taskstate (Coroutine);}}

We constantly create new tasks by calling function Ceatetask.

The task system of the hand tour end can realize the task system simply by entrusting, of course, our data load other need createtask in the process to realize.

This article is from the "Kaiyukan mobile" blog, so be sure to keep this source http://jxwgame.blog.51cto.com/943299/1602630

Mission System Writing in Unity

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.