如何从 C# 调用服务器端的方法

How to call a method on server side from c#

我在堆栈溢出时得到了这个 QA --How to call code behind server method from a client side javascript function?

然而, 它是从 javascript 调用函数,我怎么能从 从 c# 调用函数呢?我用unity3d开发了一个ios的app,之前没有做过。有人知道怎么做吗?

提前致谢。

P.S。关于后端

后端给出如下所示。 url: https://{服务器地址}/register 参数:

和return: - user_id, 整数

我运行服务器方法有正确的参数,如果参数正确,它返回一个int/user_id return.

WWW unity3d Class,这可能就是您正在寻找的。万维网是

a small utility module for retrieving the contents of URLs.

有关使用 C# 与服务器 (PHP) 交互的详细信息,我会向您推荐这个 unitywiki link。您将需要按照本教程的建议执行类似的操作。

 IEnumerator GetData()
    {
        gameObject.guiText.text = "Loading Scores";
        WWW hs_get = new WWW(highscoreURL);//highscoreURL this is ur url as u said
        yield return hs_get;

        if (hs_get.error != null)//checking empty or error response etc
        {
            print("There was an error getting the high score: " + hs_get.error);
        }
        else
        {
            gameObject.guiText.text = hs_get.text; // this is a GUIText that will display the scores in game.
        }
    }

您可以根据您的规格自定义的重置内容主要使用的是 Co-routine 的 WWW。

我从 this blog -- devindia

中找到了关于此的 post

在face里,给WWW加上一个WWWForm作为副参数,然后it is a post to server.

using UnityEngine;
using System.Collections;

public class PostJsonDataScript : MonoBehaviour
{
    // Use this for initialization
    string Url;
    void Start()
    {
        Url = "Url to the service";
        PostData(100,"Unity");
    }
    // Update is called once per frame
    void Update()
    {

    }
    void PostData(int Id,string Name)
    {
        WWWForm dataParameters = new WWWForm();
        dataParameters.AddField("Id", Id);
        dataParameters.AddField("Name", Name);
        WWW www = new WWW(Url,dataParameters);
        StartCoroutine("PostdataEnumerator", Url);
    }
    IEnumerator PostdataEnumerator(WWW www)
    {
        yield return www;
        if (www.error != null)
        {
            Debug.Log("Data Submitted");
        }
        else
        {
            Debug.Log(www.error);
        }
    }
}