Unity3D-我无法创建帐户创建系统

时间:2018-07-26 14:46:11

标签: c# unity3d delegates

我试图在Unity中创建一个帐户创建系统,但是遇到以下错误:

  

错误CS0246:找不到类型或名称空间名称Response。您是否缺少装配参考?

脚本网络连接

public void CreateUser(string userName , string email , string pass , Action<Response> response){
     StartCoroutine (Make_CreateUser (userName, email, pass, response));
 }
 private IEnumerator Make_CreateUser(string userName , string email , string pass , Action<Response> response){
     WWWForm form = new WWWForm ();
     form.AddField ("userName", userName);
     form.AddField ("email", email);
     form.AddField ("pass", pass);

     WWW w = new WWW ("http://localhost/game/createUser.php", form);

     yield return w;
     response (JsonUtility.FromJson<Response> (w.text));
 }
 [Serializable]
 public class Response {
     public bool done       = false;
     public string message = "";
 }

MenuController脚本:

[SerializeField] private InputField userNameInput     = null;
 [SerializeField] private InputField emailInput         = null;
 [SerializeField] private InputField passwordInput     = null;
 [SerializeField] private InputField RepasswordInput     = null;
 [SerializeField] private Text textInput             = null;
 private NetworkConnection s_NetworkConnection =  null;
 public GameObject Register;
 private void Awake(){
     s_NetworkConnection = GameObject.FindObjectOfType<NetworkConnection> ();
 }
 public void SubmitRegister(){
     if (userNameInput.text == "" || emailInput.text == "" || passwordInput.text == "" || RepasswordInput.text == "") {
         textInput.text = "Please, complete all fields";
         return;
     }
     if (passwordInput.text == RepasswordInput.text) {
         textInput.text = "Loading...";
         s_NetworkConnection.CreateUser (userNameInput.text, emailInput.text, passwordInput.text, delegate (Response response) {
             textInput.text = response.message;
         });
     } else {
         textInput.text = "Passwords are not equals";
     }
 }
 public void OnMouseDown ()
 {
     Register.SetActive (!Register.activeSelf);
 }

问题出在此代码段的 MenuController 脚本中:

  

s_NetworkConnection.CreateUser(userNameInput.text,emailInput.text,passwordInput.text,委托(响应响应){

1 个答案:

答案 0 :(得分:1)

问题在于Response脚本中的NetworkConnection类是声明的嵌套的,使得其他脚本无法直接访问它:

public class NetworkConnection : MonoBehaviour
{
     .....

    [Serializable]
    public class Response
    {
        .....
    }
}

尝试使用它时有两个选择:

1 。在尝试引用Response时,提供Response类所在的类。在这种情况下,这就是NetworkConnection类。

所以改变

s_NetworkConnection.CreateUser(userNameInput.text, emailInput.text,
    passwordInput.text, delegate (Response response)
{
    textInput.text = response.message;
});

textInput.text = "Loading...";
s_NetworkConnection.CreateUser(userNameInput.text, emailInput.text,
    passwordInput.text, delegate (NetworkConnection.Response response)
{
    textInput.text = response.message;
});

请注意,Response现在前面有NetworkConnection.


2 。另一种选择是将Response类移到NetworkConnection类之外。那应该可以直接从其他脚本访问它。

更改

public class NetworkConnection : MonoBehaviour
{
    .....

    [Serializable]
    public class Response
    {
        .....
    }
}

public class NetworkConnection : MonoBehaviour
{
    .....
}

[Serializable]
public class Response
{
    .....
}
相关问题