我试图在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,委托(响应响应){
答案 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
{
.....
}