Unity实现注册登录模块

  using System;

  using UniRx;

  using UnityEngine;

  using UnityEngine.UI;

  using Zenject;

  public class LoginPanel : MonoBehaviour

  {

  public InputField userName;

  public InputField password;

  public Button LoginBtn;

  public Button RegistBtn;

  [Inject] private User _user;

  [Inject] private TipPanel _tipPanel;

  [Inject] private RegistPanel _registPanel;

  void Start()

  {

  //用户名输入完成后光标自动跳转到密码输入框

  userName.OnEndEditAsObservable()

  .Subscribe((s =>

  password.Select()));

  //输入完密码后敲击回车键或者点击登录按钮 都触发登录事件

  var enterDownStream = password.OnEndEditAsObservable()

  .Select((s => "回车键触发登录"));

  var loginBtnStream = LoginBtn.OnClickAsObservable()

  .Select((unit => "通过点击登录按钮触发的登录"));

  Observable.Merge(enterDownStream, loginBtnStream)

  .Subscribe((s =>

  {

  Debug.Log(s);

  if (LoginCheak(userName.text,password.text))

  {

  userName.text=String.Empty;

  password.text=String.Empty;

  _tipPanel.Show("登录成功");

  }

  else

  {

  userName.text=String.Empty;

  password.text=String.Empty;

  _tipPanel.Show("登录失败");

  }

  }));

  RegistBtn.OnClickAsObservable()

  .Subscribe((unit =>

  {

  this.gameObject.SetActive(false);

  _registPanel.gameObject.SetActive(true);

  }));

  }

  public bool LoginCheak(string username,string password)

  {

  bool isOK = false;

  if (_user._dictionary.ContainsKey(username))

  {

  if (_user._dictionary[username] == password)

  {

  isOK = true;

  }

  }

  return isOK;

  }

  }