2021-07-24

Recently, in the process of working on a project, a simple message communication between the outside and the server is required, and the server of the other party uses the http protocol. After research, it is found that unity has a special class for processing. The problem is that when sending messages It is easy to get stuck, so the best way is to call Ctrip, and the return message from the server needs to be received during the processing, and using global variables is not the best way, so the form of callback is considered.

Ctrip's declaration method

You only need to add an Action callback item to the original declaration and add the call of this function at the end of the processing. As follows:
public static IEnumerator Post(int cmdtype, string name, Action callback)
the last parameter is the declaration of the callback
IEnumerator Post(int cmdtype, string name, Action callback)
{ if (callback != null) callback(); }


Declaration of the callback function

The declaration method of this function is the same as that of other functions

void overpost()///获取反馈消息
{
    MSG.text = HttpTool.showtxt;
}
如上所示。

call method

void OnsendCMD()
{ StartCoroutine(Post(2, “hhhh”, overpost)); //send message }

If this Ctrip is declared in other classes, you need to add the public static keyword in front of the Ctrip declaration function, and add the class name to StartCoroutine
StartCoroutine(htptool.Post(2, “hhhh”, overpost));

The above is how Ctrip adds callbacks.

Guess you like

Origin blog.csdn.net/llhllq2015/article/details/119052374