【问题标题】:StartCoroutine function not working on android, but works fine in Unity editorStartCoroutine 函数不能在 android 上运行,但在 Unity 编辑器中运行良好
【发布时间】:2017-11-22 07:02:25
【问题描述】:

我正在从 Web 服务 (WWW) 构建简单的用户数据检索功能。我无法解决这个问题,所以也许你们可以帮助我?

我正在使用StartCoroutine 方法。当我在我的 android 设备(android 6.0.1)上构建和运行时,我单击按钮,它什么也不做!绝对没有!但是,在 Unity 编辑器中,它运行良好!

我搜索了有关此问题的更多信息,有人向我推荐了一个名为 “更有效的协程” 的插件。 http://trinary.tech/category/mec/ 好吧,现在你可能会觉得这个插件对我一点帮助都没有!

这是我的带有插件的 C# 代码(我注释掉了标准 Unity 代码):

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# 是我的新语言。

问候, 抢。

【问题讨论】:

  • 您确定在 Android 上调用了 OnSubmit() 吗?我的意思是,如果你添加一个 Debug.Log("OnSubmit clicked ");或类似的东西,您可以在控制台(即 logcat)中看到吗?
  • @mayo 是的,它在 Android 上被调用。
  • 什么是connectionLink?如果00.00.00.211 是您本地机器的地址,Android 上的构建将尝试访问“android”本地机器。除非您的服务器在您的手机上运行,​​否则应用程序将无法创建连接...
  • @mayo 它实际上是域,但我已将其更改为随机数字。让我们说它:domain.com
  • 就是这样!!!我添加了https:// 并且它有效!该死!

标签: c# android unity5 coroutine ienumerator


【解决方案1】:

在@mayo 的帮助下,我发现了为什么它不能在移动设备上运行!

我只是忘记在地址开头添加https://

string connectionLink = "https://00.00.00.211/app/login.php";

希望其他人会对此有用!

【讨论】:

  • 确实如此,因为在 Windows 上您不需要它 - 它会自动添加,但 android 设备不需要。
猜你喜欢
  • 2020-09-17
  • 2019-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多