为什么Unity画布不加载?

huangapple go评论103阅读模式
英文:

Why isn't a Unity Canvas loading?

问题

我尝试将它更改为Canvas,但它仍然无法跳转到下一个场景/画布。
我已经在构建设置中添加了RoomList,但仍然无法跳转到那里。
它未达到StartCoroutine(LoadNextSceneWithDelay(3f)); Debug.Log("登录成功。");部分。

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
using TMPro;
using Firebase;
using Firebase.Database;

public class LoginScript : MonoBehaviour {
  public TMP_InputField idInput;
  public TMP_InputField passwordInput;

  public Button visibilityButton;
  public Button loginButton;
  private bool isPasswordVisible = false;

  private Firebase.Database.DatabaseReference databaseReference;

  private void Start() {
    // 设置Firebase数据库连接
    databaseReference =
        Firebase.Database.FirebaseDatabase.DefaultInstance.RootReference;

    loginButton.onClick.AddListener(OnLoginButtonClick);
    visibilityButton.onClick.AddListener(OnVisibilityButtonClick);
  }

  public void OnLoginButtonClick() {
    string id = idInput.text;
    string password = passwordInput.text;

    // 检查ID和密码是否为空
    if (string.IsNullOrEmpty(id) && string.IsNullOrEmpty(password)) {
      Debug.Log("请输入您的ID和密码");
      return;
    }

    // 检查ID是否为空
    if (string.IsNullOrEmpty(id)) {
      Debug.Log("请输入您的ID");
      return;
    }

    // 检查密码是否为空
    if (string.IsNullOrEmpty(password)) {
      Debug.Log("请输入您的密码");
      return;
    }

    // 检查是否连接到Firebase数据库
    if (Application.internetReachability != NetworkReachability.NotReachable) {
      Debug.Log("已连接到Firebase数据库");
    } else {
      Debug.Log("未连接到Firebase数据库");
    }

    try {
      // 查询数据库以获取用户名和密码
      DatabaseReference usersRef =
          databaseReference.Child("Users").Child("Monitoring");

      usersRef.GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
          Debug.LogError("从Firebase数据库检索数据时出错:" +
                         task.Exception);
          return;
        }

        DataSnapshot snapshot = task.Result;
        bool idFound = false;
        bool passwordMatched = false;

        foreach (DataSnapshot userSnapshot in snapshot.Children) {
          string storedID = userSnapshot.Value.ToString();
          string storedPassword = userSnapshot.Value.ToString();

          if (storedID == id) {
            idFound = true;

            if (storedPassword == password) {
              passwordMatched = true;
              break;
            }
          }
        }

        // 检查结果并加载场景或显示调试消息
        if (idFound && passwordMatched) {
          StartCoroutine(LoadNextSceneWithDelay(3f));
          Debug.Log("登录成功。");
        } else {
          if (!idFound) {
            Debug.Log("用户名不正确");
          }

          if (!passwordMatched) {
            Debug.Log("密码不正确");
          }
        }
      });
    } catch (Exception ex) {
      Debug.LogError("OnLoginButtonClick中出现异常:" + ex.Message);
    }
  }

  private IEnumerator LoadNextSceneWithDelay(float delay) {
    yield return new WaitForSeconds(delay);
    SceneManager.LoadScene("RoomList");
  }

  public void OnVisibilityButtonClick() {
    isPasswordVisible = !isPasswordVisible;
    UpdatePasswordVisibility();
  }

  private void UpdatePasswordVisibility() {
    if (isPasswordVisible) {
      passwordInput.contentType = TMP_InputField.ContentType.Standard;
      passwordInput.ForceLabelUpdate();
    } else {
      passwordInput.contentType = TMP_InputField.ContentType.Password;
      passwordInput.ForceLabelUpdate();
    }
  }
}
英文:

I tried to change it to Canvas, but it's still not going to the next scene/canvas.
I already added the RoomList in the building settings but still not going there.
It's not reaching the StartCoroutine(LoadNextSceneWithDelay(3f)); Debug.Log("Login successful."); part.

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using UnityEngine;
using TMPro;
using Firebase;
using Firebase.Database;

public class LoginScript : MonoBehaviour {
  public TMP_InputField idInput;
  public TMP_InputField passwordInput;

  public Button visibilityButton;
  public Button loginButton;
  private bool isPasswordVisible = false;

  private Firebase.Database.DatabaseReference databaseReference;

  private void Start() {
    // Set up the Firebase database connection
    databaseReference =
        Firebase.Database.FirebaseDatabase.DefaultInstance.RootReference;

    loginButton.onClick.AddListener(OnLoginButtonClick);
    visibilityButton.onClick.AddListener(OnVisibilityButtonClick);
  }

  public void OnLoginButtonClick() {
    string id = idInput.text;
    string password = passwordInput.text;

    // Check if ID and password are empty
    if (string.IsNullOrEmpty(id) && string.IsNullOrEmpty(password)) {
      Debug.Log("Please enter your ID and password");
      return;
    }

    // Check if ID is empty
    if (string.IsNullOrEmpty(id)) {
      Debug.Log("Please enter your ID");
      return;
    }

    // Check if password is empty
    if (string.IsNullOrEmpty(password)) {
      Debug.Log("Please enter your password");
      return;
    }

    // Check if connected to the Firebase database
    if (Application.internetReachability != NetworkReachability.NotReachable) {
      Debug.Log("Connected to Firebase database");
    } else {
      Debug.Log("Not connected to Firebase database");
    }

    try {
      // Query the database for the username and password
      DatabaseReference usersRef =
          databaseReference.Child("Users").Child("Monitoring");

      usersRef.GetValueAsync().ContinueWith(task => {
        if (task.IsFaulted) {
          Debug.LogError("Error retrieving data from Firebase database: " +
                         task.Exception);
          return;
        }

        DataSnapshot snapshot = task.Result;
        bool idFound = false;
        bool passwordMatched = false;

        foreach (DataSnapshot userSnapshot in snapshot.Children) {
          string storedID = userSnapshot.Value.ToString();
          string storedPassword = userSnapshot.Value.ToString();

          if (storedID == id) {
            idFound = true;

            if (storedPassword == password) {
              passwordMatched = true;
              break;
            }
          }
        }

        // Check the results and load the scene or display debug messages
        if (idFound && passwordMatched) {
          StartCoroutine(LoadNextSceneWithDelay(3f));
          Debug.Log("Login successful.");
        } else {
          if (!idFound) {
            Debug.Log("Incorrect username");
          }

          if (!passwordMatched) {
            Debug.Log("Incorrect password");
          }
        }
      });
    } catch (Exception ex) {
      Debug.LogError("Exception in OnLoginButtonClick: " + ex.Message);
    }
  }

  private IEnumerator LoadNextSceneWithDelay(float delay) {
    yield return new WaitForSeconds(delay);
    SceneManager.LoadScene("RoomList");
  }

  public void OnVisibilityButtonClick() {
    isPasswordVisible = !isPasswordVisible;
    UpdatePasswordVisibility();
  }

  private void UpdatePasswordVisibility() {
    if (isPasswordVisible) {
      passwordInput.contentType = TMP_InputField.ContentType.Standard;
      passwordInput.ForceLabelUpdate();
    } else {
      passwordInput.contentType = TMP_InputField.ContentType.Password;
      passwordInput.ForceLabelUpdate();
    }
  }
}

答案1

得分: 1

First check that the Scene has been added to the Scenes in Build.

其次,您的foreach循环应该检查用户名和密码是否匹配单个条目,并且在错误输出中不要提供哪部分不正确,因为这有助于恶意操作者尝试暴力破解帐户。

bool loginSuccess = false;

foreach (DataSnapshot userSnapshot in snapshot.Children) {
  string storedID = userSnapshot.Value.ToString();
  string storedPassword = userSnapshot.Value.ToString();

  if (storedID == id && storedPassword == password) {
    loginSuccess = true;
    break;
  }
}

if(loginSuccess)
{
    //加载下一个场景
}
else
{
    Debug.Log("用户信息不正确");
}
英文:

First check that the Scene has been added to the Scenes in Build.

为什么Unity画布不加载?

Secondly your foreach loop should check that the username and password matches a single entry and in the error output don't put which part was incorrect as that assists bad actors trying to bruteforce accounts.

bool loginSuccess = false;
foreach (DataSnapshot userSnapshot in snapshot.Children) {
string storedID = userSnapshot.Value.ToString();
string storedPassword = userSnapshot.Value.ToString();
if (storedID == id && storedPassword == password) {
loginSuccess = true;
break;
}
}
if(loginSuccess)
{
//Load next scene
}
else
{
Debug.Log("Incorrect user details");
}

huangapple
  • 本文由 发表于 2023年6月8日 18:30:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/76430940.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定