| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424 |
- using UnityEngine;
- using UnityEngine.UI;
- using System.Collections.Generic;
- using SmartBowSDK;
- using Newtonsoft.Json;
- using System.Linq;
- using System.Collections;
- namespace SmartBowDemoUI
- {
- public class SmartBowDemoUI : MonoBehaviour
- {
- public Transform contentRoot; // ScrollView Content
- public GameObject rowPrefab;
- [SerializeField] Image _imgAvatar;
- [SerializeField] Text _textNickname;
- [SerializeField] Text _textGender;
- string account;//账号
- string phoneNumber; //手机号
- string email; //邮箱
- string secret2 = "GF5qw/p0TF+qs9ByFBtMXnlExuU9YFe0XhHG5TDmnNM=";//游戏密钥2
- string password;//密码
- string channelId = "Android";//渠道ID
- string gameId = "SmartBowSDK";//游戏ID
- public int serverIndex = 0; //国内的用0,国外的用1
- private List<string> inputs = new List<string>();
- SmartBowHelper BowSmartBowHelper;
- public SmartBowHelper MyBowSmartBowHelper => BowSmartBowHelper;
- void Start()
- {
- SmartBowLogger.isDebug = true;
- //SmartBowNetwork.SetLocal("192.168.0.108");
- SmartBowNetwork.SetLocal("8.155.53.54");
- BowSmartBowHelper = SmartBowHelper.NewInstance();
- CreateRow("登录", "登录获取用户数据", new[] { "GameID", "ChannelID", "用户名", "密码" }, (values) =>
- {
- BowSmartBowHelper.Login(values[0], values[1], values[2], values[3], (res) =>
- {
- if (res.success)
- {
- Debug.Log("登录成功: " + res.message);
- TipText.Show(res.message);
- account = values[2];
- BowSmartBowHelper.GetUserInfo((res) =>
- {
- UserInfo userInfo = res.userInfo;
- Debug.Log("用户信息: " + JsonConvert.SerializeObject(userInfo));
- //渲染UI
- StartCoroutine(SmartBowNetwork.LoadSprite(userInfo.avatarUrl, (sprite) =>
- {
- _imgAvatar.sprite = sprite;
- }));
- _textNickname.text = userInfo.nickname;
- _textGender.text = userInfo.gender == 1 ? "男" : "女";
- });
- }
- else
- {
- Debug.LogError("登录失败: " + res.message);
- TipText.Show(res.message);
- return;
- }
- });
- });
- CreateRow("登出", "退出用户登录", null, (_) =>
- {
- BowSmartBowHelper.Logout((success) =>
- {
- TipText.Show(success ? "登出成功" : "登出失败");
- if (success)
- {
- _imgAvatar.sprite = null;
- _textNickname.text = "未登录";
- _textGender.text = "";
- }
- });
- });
- if (serverIndex == 0)
- {
- CreateRow("发送验证码", "注册修改密码等需要先发送对应的验证码", new[] { "手机号" }, (values) =>
- {
- Debug.Log(values + " = " + values[0]);
- phoneNumber = values[0];
- BowSmartBowHelper.SendPhoneValidateCode(values[0], 0, TipText.Show);
- });
- // CreateRow("单纯验证", new[] { "手机号", "Code" }, (values) =>
- // {
- // Debug.Log(values[0]);
- // if (int.TryParse(values[1], out int code))
- // {
- // BowSmartBowHelper.ValidatePhone(values[0], code, TipText.Show);
- // }
- // else
- // {
- // TipText.Show("Invalid code format");
- // }
- // });
- }
- else if (serverIndex == 1)
- {
- CreateRow("发送验证码", "注册修改密码等需要先发送对应的验证码", new[] { "邮箱" }, (values) =>
- {
- email = values[0];
- BowSmartBowHelper.SendEmailValidateCode(values[0], 0, TipText.Show);
- });
- // CreateRow("单纯验证", new[] { "邮箱", "Code" }, (values) =>
- // {
- // Debug.Log(values[0]);
- // if (int.TryParse(values[1], out int code))
- // {
- // BowSmartBowHelper.ValidatePhone(values[0], code, TipText.Show);
- // }
- // else
- // {
- // TipText.Show("Invalid code format");
- // }
- // });
- }
- else
- {
- Debug.LogError("Invalid server index: " + serverIndex);
- }
- CreateRow("注册", "注册新账号,以用户名注册,可以和手机邮箱一致", new[] { "用户名", serverIndex == 1 ? "邮箱" : "手机号", "Code1", "密码" }, (values) =>
- {
- if (!int.TryParse(values[2], out int code))
- {
- TipText.Show("Invalid code format");
- return;
- }
- //国内
- if (serverIndex == 0)
- {
- BowSmartBowHelper.Register(0, values[0], values[3], "", values[1], code, TipText.Show);
- }
- //国外
- else if (serverIndex == 1)
- {
- BowSmartBowHelper.Register(1, values[0], values[3], values[1], "", code, TipText.Show);
- }
- else
- {
- Debug.LogError("Invalid server index: " + serverIndex);
- }
- });
- CreateRow("修改密码", "修改用户名对应的密码", new[] { "用户名", serverIndex == 1 ? "邮箱" : "手机号", "Code2", "新密码" }, (values) =>
- {
- if (!int.TryParse(values[2], out int code))
- {
- TipText.Show("Invalid code format");
- return;
- }
- BowSmartBowHelper.ResetPassword(serverIndex, values[0], values[1], code, values[3], TipText.Show);
- });
- // CreateRow("修改用户名", "修改用户昵称", new[] { "昵称" }, (values) =>
- // {
- // BowSmartBowHelper.SaveUserName(values[0], TipText.Show);
- // });
- CreateRow("用户数据", "", new[] { "昵称", "头像url","生日","性别","国家", "省", "市" }, (values) =>
- {
- UserInfoExtended userInfoExtended = new UserInfoExtended();
- userInfoExtended.nickname = values[0];
- userInfoExtended.avatarUrl = values[1];//"https://thirdwx.qlogo.cn/mmopen/vi_32/Q0j4TwGTfTIWjCZKDf26TYib0Cia8vribsmQChxCkfibXYaPCVW69B7G2f5LUGYly2R6vEib9ic11L9zYBC350D93NZg/132";
- userInfoExtended.birthday = values[2];//"1995-01-01";
- userInfoExtended.gender = int.TryParse(values[3], out int gender) ? gender : 1;
- userInfoExtended.country = values[4];
- userInfoExtended.state = values[5];
- userInfoExtended.city = values[6];
- BowSmartBowHelper.SaveUserInfo(userInfoExtended, TipText.Show);
- });
- //提示使用
- CreateRow("用户注册事项", "修改用户数据,注册后要录入国省市,不然影响排行榜", null, null);
-
- //上传普通的排行榜成绩
- CreateRow("上传成绩", "密钥游戏排行记录", new[] { "密钥", "得分" }, (values) =>
- {
- int score = int.TryParse(values[1], out var s) ? s : 0;
- BowSmartBowHelper.UploadSeasonSinglePlayerGameRes(values[0], score, TipText.Show);
- });
- /**
- * 上传赛季pk分数
- * @param roomKey pk时候房间id
- * @param secret 解析 pk游戏类型密钥
- * @param p2ID 对战用户2 ID
- * @param gameRes PK游戏结果,0:玩家1胜利;1:玩家2胜利;2:双方平局
- */
- CreateRow("上传pk成绩", "", new[] { "房间id", "密钥", "对战用户2 ID", "PK游戏结果" }, (values) =>
- {
- string roomKey = values[0].Trim();
- string secret = values[1].Trim();
- string p2Str = values[2].Trim();
- string gameResStr = values[3].Trim();
- int userId2 = int.TryParse(p2Str, out var id2) ? id2 : 0;
- int gameRes = int.TryParse(gameResStr, out var res) ? res : 0;
- BowSmartBowHelper.UploadSeasonPKGameRes(roomKey, secret, userId2, gameRes, TipText.Show);
- });
- /**
- * 获取用户排行榜
- * @param secret 密钥
- * @param rankArea 排行区域 3:国,1:省,2市,其他:全球
- * @param countryCode
- * @param stateCode
- * @param cityCode
- * @return
- */
- CreateRow("获取排行榜", "", new[] { "密钥", "排行区域", "国家", "省", "市" }, (values) =>
- {
- int rankArea = int.TryParse(values[1], out var r) ? r : 0;
- BowSmartBowHelper.GetSeasonRankListByGameType(values[0], rankArea, values[2], values[3], values[4], TipText.Show);
- });
- /**
- * 获取用户排行榜以及自定义
- * @param secret 密钥
- * @param rankArea 排行区域 3:国,1:省,2市,其他:全球
- * @param countryCode
- * @param stateCode
- * @param cityCode
- * @param rankType 1是降序,其他是升序(不能小于0)
- * @param count 数量:不能小于0
- * @return
- */
- CreateRow("获取我的排名", "", new[] { "密钥", "排行区域", "国家", "省", "市", "排序", "数量" }, (values) =>
- {
- int rankArea = int.Parse(values[1]);
- int rankType = int.Parse(values[5]);
- int count = int.Parse(values[6]);
- BowSmartBowHelper.GetUserRankByGameType(values[0], rankArea, values[2], values[3], values[4], rankType, count, TipText.Show);
- });
- // 上传定制字段的排行榜成绩
- // userID为账号唯一标识码
- // taskID为任务对应id,比如判断射击是0,记忆射击是1
- // difficultType为任务难度类型,比如标准是0,进阶是1,专业是2
- // modeType为模式类型,比如无任何模式是0,战术模式是1
- // deviceType为设备类型,比如M9电子手枪为0
- // score为上述userID账户在判断射击标准难度下无任何模式使用M9电子手枪的分数3889
- CreateRow("上传成绩", "定制字段游戏数据排行记录", new[] { "密钥2", "任务ID", "难度", "模式", "设备", "得分" }, (values) =>
- {
- string secret = values[0];
- int taskId = int.TryParse(values[1], out var v1) ? v1 : 0;
- int difficultType = int.TryParse(values[2], out var v2) ? v2 : 0;
- int modeType = int.TryParse(values[3], out var v3) ? v3 : 0;
- int deviceType = int.TryParse(values[4], out var v4) ? v4 : 0;
- int score = int.TryParse(values[5], out var v5) ? v5 : 0;
- BowSmartBowHelper.UploadCustomLeaderboardScore(
- secret, taskId, difficultType, modeType, deviceType, score, TipText.Show);
- });
- //获取定制字段的排行榜
- // userID为账号唯一标识码
- // taskID为任务对应id,比如判断射击是0,记忆射击是1
- // difficultType为任务难度类型,比如标准是0,进阶是1,专业是2
- // modeType为模式类型,比如无任何模式是0,战术模式是1
- // deviceType为设备类型,比如M9电子手枪为0
- // deta排行榜日期类型,比如0是按周筛选下的排名,1为按月,2为按年,-1为不筛选日期
- CreateRow("获取排行榜", "定制字段游戏数据排行记录", new[] { "密钥2", "任务ID", "难度", "模式", "设备", "日期" }, (values) =>
- {
- string secret = values[0];
- int taskId = int.TryParse(values[1], out var v1) ? v1 : 0;
- int difficultType = int.TryParse(values[2], out var v2) ? v2 : 0;
- int modeType = int.TryParse(values[3], out var v3) ? v3 : 0;
- int deviceType = int.TryParse(values[4], out var v4) ? v4 : 0;
- int deta = int.TryParse(values[5], out var v5) ? v5 : -1;
- BowSmartBowHelper.GetCustomLeaderboard(
- secret, taskId, difficultType, modeType, deviceType, deta, TipText.Show);
- });
- }
- // 👇 新增:用于同步相同 placeholder 的输入框
- private Dictionary<string, List<InputField>> sharedInputs = new Dictionary<string, List<InputField>>();
- void CreateRow(string title, string describe, string[] placeholders, System.Action<string[]> onClick)
- {
- if (placeholders == null) placeholders = new string[0];
- GameObject row = Instantiate(rowPrefab, contentRoot);
- row.SetActive(true);
- Text titleText = row.transform.Find("TitleText").GetComponent<Text>();
- Text describeText = row.transform.Find("Describe").GetComponent<Text>();
- describeText.text = describe;
- Transform contentGroup = row.transform;
- Button execButton = contentGroup.Find("ExecButton").GetComponent<Button>();
- if(placeholders.Length == 0 && onClick == null)
- {
- execButton.gameObject.SetActive(false);
- }
- else
- {
- execButton.gameObject.SetActive(true);
- }
- Transform inputTemplate = contentGroup.Find("Input");
- inputTemplate.gameObject.SetActive(false);
- List<InputField> inputFields = new List<InputField>();
- int execIndex = execButton.transform.GetSiblingIndex();
- for (int i = placeholders.Length - 1; i >= 0; i--)
- {
- string placeholder = placeholders[i];
- Transform inputTf = Instantiate(inputTemplate, contentGroup);
- inputTf.name = "Input" + i;
- inputTf.SetSiblingIndex(execIndex);
- inputTf.gameObject.SetActive(true);
- LayoutElement _layoutElement = inputTf.gameObject.AddComponent<LayoutElement>();
- _layoutElement.preferredWidth = 230;
- _layoutElement.preferredHeight = 60;
- InputField input = inputTf.GetComponent<InputField>();
- input.placeholder.GetComponent<Text>().text = placeholder;
- // 从本地读取覆盖默认值
- string savedValue = PlayerPrefs.GetString($"SmartBowInput_{placeholder}", "");
- if (!string.IsNullOrEmpty(savedValue))
- {
- // input.text = savedValue;
- StartCoroutine(SetInputTextNextFrame(input, savedValue));
- }
- else
- {
- //默认值
- if (placeholder == "GameID")
- input.text = gameId;
- else if (placeholder == "ChannelID")
- input.text = channelId;
- else if (placeholder == "密钥2")
- StartCoroutine(SetInputTextNextFrame(input, secret2));
- }
- inputFields.Insert(0, input); // 保持顺序
- // 👇 注册同步逻辑
- if (!sharedInputs.ContainsKey(placeholder))
- sharedInputs[placeholder] = new List<InputField>();
- sharedInputs[placeholder].Add(input);
- bool isSyncing = false;
- input.onValueChanged.AddListener((newValue) =>
- {
- if (isSyncing) return;
- isSyncing = true;
- foreach (var otherInput in sharedInputs[placeholder])
- {
- if (otherInput != input && otherInput.text != newValue)
- otherInput.text = newValue;
- }
- isSyncing = false;
- });
- input.onEndEdit.AddListener((newValue) =>
- {
- // 存储到本地
- PlayerPrefs.SetString($"SmartBowInput_{placeholder}", newValue);
- PlayerPrefs.Save();
- });
- }
- titleText.text = title;
- execButton.onClick.RemoveAllListeners();
- execButton.onClick.AddListener(() =>
- {
- var values = inputFields.Select(field => field.text).ToArray();
- onClick.Invoke(values);
- });
- StartCoroutine(RefreshLayoutNextFrame(row.transform));
- }
- IEnumerator RefreshLayoutNextFrame(Transform root)
- {
- yield return null; // 等待一帧
- LayoutRebuilder.ForceRebuildLayoutImmediate((RectTransform)root);
- }
- IEnumerator SetInputTextNextFrame(InputField input, string text)
- {
- yield return new WaitForSeconds(1.0f);//new WaitForEndOfFrame(); // 等待布局初始化
- input.text = text;
- }
- }
- public static class TipText
- {
- public static void Show(string message)
- {
- Debug.Log("[提示] " + message);
- // 或者调用 UI 展示
- }
- public static void Show(RequestResult result)
- {
- //Show(result.code + " = " + result.msg);
- string dataStr = result.data != null ? JsonConvert.SerializeObject(result.data, Formatting.Indented) : "null";
- Show($"{result.code} = {result.msg} = Data:{dataStr}");
- }
- }
- }
|