2023-04-03 11:04:31 +08:00

349 lines
10 KiB
C#

using System.IO;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEditor;
using BF;
namespace BFEditor
{
[System.Serializable]
public class Role
{
public string id;
public string pwd;
public string serverName;
public string roleName;
}
[System.Serializable]
public class RoleList
{
public List<Role> list = new List<Role>();
public void SortSelf()
{
list.Sort((Role x, Role y) =>
{
return int.Parse(x.id) - int.Parse(y.id);
});
}
}
public class SelectAccontWindow : EditorWindow
{
const string KEY_ROLE_LIST = "ROLE_LIST";
const string KEY_ACCOUNT = "ACCOUNT";
string curId = "";
RoleList roleList = new RoleList();
Vector2 scrollPos;
List<Role> currentRoleList = new List<Role>();
GUIStyle yellowStyle;
GUIStyle greenStyle;
void OnEnable()
{
this.titleContent = new GUIContent("选择账号");
var curAccountStr = PlayerPrefs.GetString(KEY_ACCOUNT);
if (curAccountStr.Contains("|"))
{
curId = curAccountStr.Split('|')[0];
}
var roleListStr = PlayerPrefs.GetString(KEY_ROLE_LIST, "");
if (roleListStr != "")
{
roleList = JsonUtility.FromJson(roleListStr, typeof(RoleList)) as RoleList;
roleList.SortSelf();
}
RefreshCurrentRoleList();
}
void RefreshCurrentRoleList()
{
currentRoleList.Clear();
foreach (var role in roleList.list)
{
if (role.id == curId)
{
currentRoleList.Add(role);
}
}
}
void OnGUI()
{
yellowStyle = new GUIStyle(GUI.skin.label);
yellowStyle.normal.textColor = Color.yellow;
greenStyle = new GUIStyle(GUI.skin.label);
greenStyle.normal.textColor = Color.green;
DrawTop();
DrawCurrentAccount();
DrawScrollView();
}
void DrawTop()
{
GUILayout.Space(10);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if (GUILayout.Button("清除所有账号", GUILayout.Width(90), GUILayout.Height(25)))
{
OnClickClearAll();
}
if (GUILayout.Button("导入账号", GUILayout.Width(90), GUILayout.Height(25)))
{
OnClickImport();
}
GUILayout.EndHorizontal();
GUILayout.Space(2);
GUILayout.Box("", GUI.skin.box, GUILayout.Width(520), GUILayout.Height(2));
GUILayout.Space(5);
}
void OnClickClearAll()
{
if (EditorUtility.DisplayDialog("提示", "确认清楚所有账号吗", "确认", "取消"))
{
PlayerPrefs.DeleteKey(KEY_ROLE_LIST);
PlayerPrefs.DeleteKey(KEY_ACCOUNT);
roleList.list.Clear();
curId = "";
RefreshCurrentRoleList();
BFLog.Log("清除所有账号成功");
}
}
void OnClickImport()
{
AddAccountWindow.OpenWindow((string json) =>
{
if (string.IsNullOrEmpty(json))
{
EditorUtility.DisplayDialog("提示", "格式不正确", "ok");
return;
}
var addRoleList = JsonUtility.FromJson(json, typeof(RoleList)) as RoleList;
if (addRoleList == null || addRoleList.list.Count == 0)
{
EditorUtility.DisplayDialog("提示", "格式不正确", "ok");
return;
}
EditorUtility.DisplayDialog("提示", "导入成功", "ok");
AddAccountWindow.CloseWindow();
});
}
void DrawCurrentAccount()
{
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.Label("当前账号: " + curId, GUILayout.Width(130));
if (GUILayout.Button("导出", GUILayout.Width(50)))
{
OnClickExport(curId);
}
GUILayout.EndHorizontal();
GUILayout.Space(5);
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.Label("账号id", yellowStyle, GUILayout.Width(100));
GUILayout.Label("服务器", yellowStyle, GUILayout.Width(90));
GUILayout.Label("角色名", yellowStyle, GUILayout.Width(90));
GUILayout.EndHorizontal();
var count = 0;
foreach (var role in currentRoleList)
{
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if (count == 0)
{
GUILayout.Label(role.id, GUILayout.Width(100));
}
else
{
GUILayout.Label("", GUILayout.Width(100));
}
GUILayout.Label(role.serverName, GUILayout.Width(90));
GUILayout.Label(role.roleName, GUILayout.Width(90));
GUILayout.EndHorizontal();
count++;
}
GUILayout.Space(5);
GUILayout.Box("", GUI.skin.box, GUILayout.Width(520), GUILayout.Height(2));
GUILayout.Space(5);
}
void OnClickExport(string id)
{
if (string.IsNullOrEmpty(id))
{
return;
}
var json = "";
var tempRoleList = new RoleList();
foreach (var role in roleList.list)
{
if (id == role.id)
{
tempRoleList.list.Add(role);
}
}
json = JsonUtility.ToJson(tempRoleList);
var te = new TextEditor();
te.text = json;
te.SelectAll();
te.Copy();
}
void DrawScrollView()
{
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.Label("所有账号:");
GUILayout.EndHorizontal();
GUILayout.BeginHorizontal();
GUILayout.Space(10);
GUILayout.Label("账号id", yellowStyle, GUILayout.Width(100));
GUILayout.Label("服务器", yellowStyle, GUILayout.Width(90));
GUILayout.Label("角色名", yellowStyle, GUILayout.Width(90));
GUILayout.EndHorizontal();
scrollPos = GUILayout.BeginScrollView(scrollPos, GUILayout.Width(510));
var removedId = "";
var idRecord = "";
foreach (var role in roleList.list)
{
if (role.id != idRecord && idRecord != "")
{
GUILayout.Space(15);
}
GUILayout.BeginHorizontal();
GUILayout.Space(10);
if (idRecord != role.id)
{
if (curId == role.id)
{
GUILayout.Label(role.id + "(✔)", greenStyle, GUILayout.Width(100));
}
else
{
GUILayout.Label(role.id, GUILayout.Width(100));
}
}
else
{
GUILayout.Label("", GUILayout.Width(100));
}
GUILayout.Label(role.serverName, GUILayout.Width(90));
GUILayout.Label(role.roleName, GUILayout.Width(90));
if (idRecord != role.id)
{
if (curId == role.id)
{
GUILayout.Label("(当前号✔)", greenStyle, GUILayout.Width(55));
}
else
{
if (GUILayout.Button("切换", GUILayout.Width(55)))
{
OnClickSelect(role);
}
}
if (GUILayout.Button("移除", GUILayout.Width(55)))
{
if (OnClickClear(role.id))
{
removedId = role.id;
}
}
if (GUILayout.Button("导出", GUILayout.Width(55)))
{
OnClickExport(role.id);
}
}
idRecord = role.id;
GUILayout.EndHorizontal();
}
GUILayout.EndScrollView();
if (removedId != "")
{
RemoveAccount(removedId);
}
}
void OnClickSelect(Role role)
{
var accountStr = role.id + "|" + role.pwd;
PlayerPrefs.SetString(KEY_ACCOUNT, accountStr);
curId = role.id;
RefreshCurrentRoleList();
BFLog.Log("选择账号:" + role.id);
//对于新版本 还要更新专用数据
PlayerPrefs.SetString("ULogin_Auto_Login_Key", string.Empty);
PlayerPrefs.SetString("DroidHang_Once_ShuMeiID", role.pwd);
PlayerPrefs.Save();
}
bool OnClickClear(string id)
{
if (EditorUtility.DisplayDialog("提示", "确认移除吗", "确认", "取消"))
{
if (id == curId)
{
curId = "";
PlayerPrefs.DeleteKey(KEY_ACCOUNT);
RefreshCurrentRoleList();
}
BFLog.Log("移除账号:" + id);
return true;
}
return false;
}
void RemoveAccount(string id)
{
for (int i = roleList.list.Count - 1; i >= 0; i--)
{
var role = roleList.list[i];
if (role.id == id)
{
roleList.list.RemoveAt(i);
}
}
PlayerPrefs.SetString(KEY_ROLE_LIST, JsonUtility.ToJson(roleList));
}
}
}