项目说明 (一共就用到一个脚本,带有项目默认账号(可以自定义),也有玩家登录过后存到本地的账号(注册过后才有))

添加场景的方法

脚本
cs
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
public class LogManage : MonoBehaviour
{
[Header("两个面板")]
public GameObject loginPanel;//登录面板
public GameObject registerPanel;//注册面板
[Header("四个按钮")]
public Button loginBtn;//登录按钮
public Button goregisterBtn;//跳转到注册界面按钮
public Button backBtn;//返回按钮
public Button registerBtn;//注册按钮
[Header("登录面板的两个输入框")]
public InputField useridInput;//用id输入框
public InputField passwordInput;//密码输入框
[Header("注册面板的两个输入框")]
public InputField setuseridInput;//设置id输入框
public InputField setpasswordInput;//设置密码输入框
//在场景中输入要跳转的场景的名字//不输入就不会跳转
[SerializeField] private string Scenenameext;
//登录画布//可以选择登录成功后影藏登录画布//默认情况登录成功影藏画布
[SerializeField] private GameObject loginCanvas;
//提示面板
public GameObject tipPanel;
void Start()
{
loginBtn.onClick.AddListener(Login);
goregisterBtn.onClick.AddListener(goRegister);
backBtn.onClick.AddListener(Back);
registerBtn.onClick.AddListener(Register);
}
void Login()
{
string userid = useridInput.text;
string password = passwordInput.text;
// 使用 Resources.Load 来读取文件内容
string filePathtext = "PlayerLogin/Login";
TextAsset loginfile = Resources.Load<TextAsset>(filePathtext);
//先查找默认账号有木有玩家的数据
if (loginfile != null)
{
string str = loginfile.text;
// 在这里可以添加逻辑验证 userid 和 password 是否与 str 相匹配
string[] str1 = str.Split('\n');//第一次分割
for (int i = 0; i < str1.Length; i++)
{
string[] str2 = str1[i].Split(':');
if (str2[0] == userid && str2[1] == password)
{
Debug.Log("登录成功!");
//****************登录成功的逻辑*****************
if (Scenenameext != "")
{
SceneManager.LoadScene(Scenenameext); //跳转到指定场景
Debug.Log("你还没有指定跳转场景!");
}
else //如果没设置跳转场景的名字默认影藏画布
{
loginCanvas.SetActive(false); //登录成功后影藏登录画布
}
break;
}
else if (str2[0] == userid && str2[1] != password)
{
Debug.Log("密码错误");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "密码错误";
Invoke("closetip", 2f);
break;
}
else
{
if (i == str1.Length - 1)
{
//这里遍历本地账号看有木有玩家
string content = "PlayerLogin";
string[] str3 = File.ReadAllLines(Application.persistentDataPath + "/" + content + "/" + "Login.txt");
for (int j = 0; j < str3.Length; j++)
{
string[] str4 = str3[j].Split(':');
if (str4[0] == userid && str4[1] == password)
{
Debug.Log("登录成功!");
//****************登录成功的逻辑*
if (Scenenameext != "")
{
SceneManager.LoadScene(Scenenameext); //跳转到指定场景
Debug.Log("你还没有指定跳转场景!");
}
else //如果没设置跳转场景的名字默认影藏画布
{
loginCanvas.SetActive(false); //登录成功后影藏登录画布
}
break;
}
else if (str4[0] == userid && str4[1] != password)
{
Debug.Log("密码错误");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "密码错误";
Invoke("closetip", 2f);
break;
}
else if(j== str3.Length-1)
{
Debug.Log("该账号不存在");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "该账号不存在";
Invoke("closetip", 2f);
}
}
}
}
}
}
else
{
Debug.LogError("登录文件未找到!");
}
}
void goRegister()
{
//跳转到注册界面
loginPanel.SetActive(false);
registerPanel.SetActive(true);
//清空输入框
setuseridInput.text = "";
setpasswordInput.text = "";
}
void Back()
{
//返回登录界面
loginPanel.SetActive(true);
registerPanel.SetActive(false);
}
void Register()
{
//注册逻辑
string userid = setuseridInput.text;
string password = setpasswordInput.text;
//判断是不是默认账号
string filePathtext = "PlayerLogin/Login";
TextAsset loginfile = Resources.Load<TextAsset>(filePathtext);
//检测本地账号文本是否存在
string content = "PlayerLogin";
if (!File.Exists(Application.persistentDataPath + "/" + content + "/" + "Login.txt"))//路径存在后创建数据文本(如果没找到的话)
{
Directory.CreateDirectory(Application.persistentDataPath + "/" + content);//这是一个双重保险
string filePath0 = Application.persistentDataPath + "/" + content + "/" + "Login.txt";//这里换成对应文件位置
string content2 = "玩家账号:玩家密码"+"\n";
File.WriteAllText(filePath0, content2);
//Debug.Log("测试文本写入成功");
}
string str = loginfile.text;
Debug.Log(str); // 输出文件内容
//判断玩家注册的账号是不是默认账号
string[] str1 = str.Split('\n');//第一次分割
for (int i = 0; i < str1.Length; i++)
{
string[] str2 = str1[i].Split(':');
if (str2[0] == userid)
{
Debug.Log("该账号已被注册!");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "该账号已被注册!";
Invoke("closetip", 2f);
break;
}
else if (i == str1.Length - 1)
{
//这里遍历本地账号看有木有玩家
string[] str3 = File.ReadAllLines(Application.persistentDataPath + "/" + content + "/" + "Login.txt");
for (int j = 0; j < str3.Length; j++)
{
string[] str4 = str3[j].Split(':');
if (str4[0] == userid)
{
Debug.Log("该账号已被注册!");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "该账号已被注册!";
Invoke("closetip", 2f);
break;
}
else if (j == str3.Length - 1)
{
//注册成功
Debug.Log("注册成功!");
//提示面板的显示
tipPanel.SetActive(true);
tipPanel.gameObject.transform.Find("Tipsimage/Tipstxt").GetComponent<Text>().text = "注册成功!";
Invoke("closetip", 2f);
//写入本地文件
string filePath = Application.persistentDataPath + "/" + content + "/" + "Login.txt";
string content1 = userid + ":" + password + "\n";
File.AppendAllText(filePath, content1);
Debug.Log("玩家数据写入成功");
//跳转到登录界面
loginPanel.SetActive(true);
//自动将账号和密码填进去
useridInput.text = setuseridInput.text;
passwordInput.text = setpasswordInput.text;
registerPanel.SetActive(false);
break;
}
}
}
}
}
void closetip()
{
tipPanel.SetActive(false);
}
}
