游戏基本登录流程

作者:追风剑情 发布于:2023-3-17 17:20 分类:Unity3d

本文介绍了最基本的登录流程,开发者要根据项目的实际情况对界面和脚本做调整。

1、登录界面

11111.png

22222.png

2、登录界面脚本

using UnityEngine;
using UnityEngine.UI;
using UnityEngine.SceneManagement;
/// <summary>
/// 登录界面
/// </summary>
public class UILogin : MonoBehaviour
{
    //是否跳过登录验证
    public bool skipVerify = false;
    //是否记住账号
    public bool rememberAccount = true;
    //是否记住密码
    public bool rememberPassword = true;

    [SerializeField]
    private InputField accountInput;
    [SerializeField]
    private InputField passwordInput;
    private LoginHttpRequest request;

    private void Awake()
    {
        request = this.GetComponent<LoginHttpRequest>();
        request.OnError.AddListener(OnLoginError);
        request.OnCompleted.AddListener(OnLoginCompleted);
    }

    private void Start()
    {
        accountInput.text = Config.Account;
        passwordInput.text = Config.Password;
    }

    public void OnClickLogin()
    {
        if (skipVerify)
        {
           SceneManager.LoadScene("xxx");
           return;
        }

        string account = accountInput.text;
        string password = passwordInput.text;
        if (string.IsNullOrEmpty(account))
        {
            UIToast.Instace.ShowError("用户名不能为空");
            return;
        }
        if (string.IsNullOrEmpty(password))
        {
            UIToast.Instace.ShowError("密码不能为空");
            return;
        }

        request.username = account;
        request.password = password;
        request.SendRequest();
    }

    public void OnLoginError(string error)
    {
        UIToast.Instace.ShowError(error);
    }

    public void OnLoginCompleted(LoginResult result)
    {
        if (result.code != 200)
        {
            Debug.Log(result.message);
            UIToast.Instace.ShowError(result.message);
            return;
        }
        if (rememberAccount)
            Config.Account = request.username;
        if (rememberPassword)
            Config.Password = request.password;

        SceneManager.LoadScene("xxxx");
    }
} 

3、登录请求脚本

using System;
using System.Collections;
using UnityEngine;
using UnityEngine.Networking;
using UnityEngine.Events;
/// <summary>
/// 登录请求
/// </summary>
public class LoginHttpRequest : MonoBehaviour
{
    public string url;
    [NonSerialized]
    public string username;
    [NonSerialized]
    public string password;

    public class OnCompletedEvent : UnityEvent<LoginResult> { }
    public class OnErrorEvent : UnityEvent<string> { }

    [NonSerialized]
    public OnCompletedEvent OnCompleted = new OnCompletedEvent();
    [NonSerialized]
    public OnErrorEvent OnError = new OnErrorEvent();

    public void SendRequest()
    {
        StartCoroutine(SendRequestCoroutine());
    }

    private IEnumerator SendRequestCoroutine()
    {
        yield return null;

        WWWForm form = new WWWForm();
        form.AddField("username", username);
        form.AddField("password", password);

        UnityWebRequest request = UnityWebRequest.Post(url, form);
        request.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        request.timeout = 5;
        yield return request.SendWebRequest();
        if (request.result == UnityWebRequest.Result.ProtocolError || 
            request.result == UnityWebRequest.Result.ConnectionError ||
            request.result == UnityWebRequest.Result.DataProcessingError ||
            request.responseCode != 200)
        {
            Debug.LogErrorFormat("Http请求出错({0})\n{1}", request.responseCode, request.error);
            OnError?.Invoke(request.error);
            yield break; //跳出协程
        }
        string text = request.downloadHandler.text;
        Debug.Log(text);
        LoginResult result = JsonUtility.FromJson<LoginResult>(text);
        OnCompleted?.Invoke(result);
    }
}

// 登录结果返回
public class LoginResult
{
    //错误码
    public int code;
    //错误消息
    public string message;
    public Data data;
    public int time;

    public class Data
    {
        public string userName;
        public string userId;
        public string token;
    }
} 

Powered by emlog  蜀ICP备18021003号-1   sitemap

川公网安备 51019002001593号