我正在從web服務(WWW
)構建簡單的用戶數據檢索功能。我無法解決這個問題,所以也許你們可以幫助我?StartCoroutine函數不適用於android,但在Unity編輯器中正常工作
我正在使用StartCoroutine
方法。當我在我的android設備上構建並運行(android 6.0.1)時,我單擊按鈕,它什麼也不做!絕對沒有!但是,在Unity編輯器中,它完美運行!
我搜索了關於這個問題的更多信息,有人推薦我一個名爲「更有效的協程」的插件。 http://trinary.tech/category/mec/那麼現在你可能會認爲這個插件根本沒有幫助我!
這裏是用插件我的C#代碼(我註釋掉標準統一代碼):
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
//using "More Effective Coroutines" plugin:
using MovementEffects;
public class Login : MonoBehaviour {
public InputField LoginUsername;
private string usernameInput;
public InputField LoginPassword;
private string passwordInput;
string connectionLink = "00.00.00.211/app/login.php";
public Text conResponse;
public void OnSubmit(){
usernameInput = LoginUsername.text;
passwordInput = LoginPassword.text;
//DEFAULT: StartCoroutine (Login(usernameInput, passwordInput));
Timing.RunCoroutine(_login(usernameInput, passwordInput));
}
//DEFAULT: IEnumerator login(string username, string password){
IEnumerator<float> _login(string username, string password){
WWWForm form = new WWWForm();
form.AddField("usernamePost", username);
form.AddField("passwordPost", password);
WWW www = new WWW(connectionLink, form);
//DEFAULT: yield return www;
yield return Timing.WaitForSeconds(1f);
if (www.text == "ok") {
SceneManager.LoadScene ("inGame", LoadSceneMode.Single);
} else {
conResponse.text = www.text;
switch (www.text) {
case"noUser":
conResponse.text = "User "+username+" not found in database!";
break;
case"loginFailed":
conResponse.text = "Check your password and try again!";
break;
}
Debug.LogWarning(www.text);
}
}
}
請看看,並給我一個反饋什麼,我做錯了什麼? C#是我的新語言。
Regards, Rob。
你確定在Android上調用了'OnSubmit()'嗎?我的意思是,如果你添加一個Debug.Log(「OnSubmit clicked」);或類似的東西,你可以看到在控制檯(即logcat)? – mayo
@mayo是的,它在Android上被調用。 – user3292639
什麼是'connectionLink'?如果'00.00.00.211'是本地機器的地址,則Android上的內部版本將嘗試訪問'android'本地機器。除非您的服務器在您的手機上運行,否則該應用將無法創建連接。 .. – mayo