Unity3d object-oriented design concept (6) (Unity3d Asynchronous Network Data)

Source: Internet
Author: User

One method in the MonoBehavior class is StartCoroutine, which requires an interface to return the value of IEnumerator synergy,

In Unity3d, the function of collaboration is to return results immediately without affecting the execution of other programs.

You can use yiled in the collaboration program to wait for the execution of the current program to complete,

Then execute the code below the program. The advantage of using collaboration is not to block the execution of game threads and make the game smooth.

Using a weak connected game, I simply encapsulated a tool class to conveniently process network data,

The idea is to create an object for this class in a semi-singleton mode and not open to the outside. A static factory method + Singleton is used,

Of course, there are still many shortcomings in it. For general games with weak network connections, It is enough.

Using UnityEngine; using System. collections; public class Task {// eugenic level of the Task enum Advance {Low =-1, Normal, Advance} // type of the Task enum TaskType {Netwowk =-77, Local, message} /// <summary> /// current Task list. The Task is read in the form of a forward and backward output // </summary> BetterList <Task> ts; /// <summary> /// Task Singleton // </summary> private static Task instance; public static Task Instance {get {if (instance = null) {instance = new Task () ;}return instance ;}} public vo Id StopIEnumerator () {if (instance! = Null) {instance = null; ts = null;} private Task () {ts = new BetterList <Task> (); ts. clear () ;}/// <summary> // Initializes a new instance of the <see cref = "Task"/> class. /// </summary> /// <param name = "id"> Identifier. </param> /// <param name = "param"> Parameter. </param> private Task (int id, object param) {this. id = id; this. param = param;} // <summary> // Creates the new task. /// </summary> /// <returns> <c> true </c>, if new task was created, <c> false </c> otherwise. </returns> // <param name = "id"> Identifier. </param> /// <param name = "param"> Parameter. </param> public bool CreateNewTask (int id, object param) {Task t = new Task (id, param); ts. add (t); return true;} // <summary> // The identifier. /// </summary> public int id; // <summary> // The parameter. /// </summary> public object param; void addTask (Task t) {if (t. id> 0) {ts. add (t) ;}} public IEnumerator doTask () {Debug. log ("Size:" + ts. size); // Debug. log (RealTime. time); if (ts. size> 0) {Task t = ts. pop (); switch (t. id) {case 77: Debug. log ("ID:" + t. id + "TASK:" + t. param); WWW www = new WWW ("http://www.baidu.com"); yield return www; // Debug the data distribution after the network request is complete. log ("www" + www. text); // NGUITools. broadcast ();-> LocalDispatchbreak; case 88: Debug. log ("ID:" + t. id + "TASK:" + t. param); break; case 99: Debug. log ("ID:" + t. id + "TASK:" + t. param); break; default: break;} // continuously reads the current task and completes all the direct tasks. // if (ts. size> 0) {// Debug. log ("doTask"); // doTask (); //} // else {// wait for 3.5sDebug.Log (RealTime. time); // yield return new WaitForSeconds (3.5f); // doTask ();//}}}


In this way, create the current instance where a network request is required, and then create a new task to start the unity3d loop,

You can set a fixed period of time to read tasks.




Zookeeper

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.