app直播商城源码用户头像的加载
ResourcesManager
using System.Collections; using System.Collections.Generic; using UnityEngine; public class ResourcesManager { /// <summary> /// 保存用户的头像 /// 根据名字保存 string为用户名 /// </summary> private static Dictionary<string, Sprite> nameSpriteDic = new Dictionary<string, Sprite>(); public static Sprite GetSprite(string iconName) { if (nameSpriteDic.ContainsKey(iconName))//查找用户名是否在字典中 { return nameSpriteDic[iconName]; } else { Sprite[] sprites = Resources.LoadAll<Sprite>("headIcon");//获取所有的头像 //头像名字组成为 字符串_数字 数字就为头像数组的下标 返回值为string数组 string[] nameArr = iconName.Split('_'); int index = int.Parse(nameArr[1]);//nameArr[1] 得到头像名字的数字,作为下标 Sprite temp = sprites[index];//获取用户的头像 nameSpriteDic.Add(iconName,temp);//添加到头像字典中 return temp; } } }
加载用户信息
using Protocol.Code; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class MainPanel : MonoBehaviour { #region 字段 private Image headIcon; private Text txtUserName; private Text txtCoinCount; private Button btn_Rank; private Button btn_Bank; private Button btnStand; private Button btnOnline; #endregion #region Unity回调 private void Awake() { Init(); } #endregion #region 方法 /// <summary> /// 初始化 /// </summary> private void Init() { headIcon = transform.Find("mask/headIcon").GetComponent<Image>(); txtUserName = transform.Find("txtUserName").GetComponent<Text>(); txtCoinCount = transform.Find("txtCoinCount").GetComponent<Text>(); btn_Rank = transform.Find("btn_Rank").GetComponent<Button>(); btn_Rank.onClick.AddListener(()=> { //向服务器获取排行榜 NetMsgCenter.Instance.SendMsg(OpCode.Account,AccountCode.GetRankList_CRES,null); //显示排行榜界面 EventCenter.Broadcast(EventDefine.ShowRankListPanel); }); btn_Bank = transform.Find("btn_Bank").GetComponent<Button>(); btnStand = transform.Find("btnStand").GetComponent<Button>(); btnOnline = transform.Find("btnOnline").GetComponent<Button>(); ///加载用户信息 //获取存储用户信息中的金币数 txtCoinCount.text = Models.GameModel.userDto.Coin.ToString(); //获取存储用户信息中的用户名 txtUserName.text = Models.GameModel.userDto.UserName; //加载用户头像 headIcon.sprite = ResourcesManager.GetSprite(Models.GameModel.userDto.IconName); } #endregion }
排行榜的实现
添加请求排行榜信息的子操作码
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Protocol.Code { /// <summary> /// 账号模块下的操作码 /// </summary> public class AccountCode { public const int Register_CREQ = 0;//客户端注册的子操作码 public const int Register_SRES = 1;//注册Server端回应的操作码 public const int Login_CREQ = 2;//登陆的子操作码 public const int Login_SRES = 3;//登陆server端回应的操作码 public const int GetUserInfo_CREQ = 4;//获得用户信息的子操作码 public const int GetUserInfo_SRES = 5;//获得用户信息的Server端回应的操作码 public const int GetRankList_CRES = 6;//获取排行榜的子操作码 public const int GetRankList_SRES = 7;//获取排行榜的server端的回应操作码 } }
客户端的请求
btn_Rank.onClick.AddListener(()=> { //向服务器获取排行榜 NetMsgCenter.Instance.SendMsg(OpCode.Account,AccountCode.GetRankList_CRES,null); //显示排行榜界面 EventCenter.Broadcast(EventDefine.ShowRankListPanel); });
Server端创建数据模型(排行榜里内容的数据模型)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Protocol.Dto { /// <summary> /// 排行榜所需内容的数据模型 /// </summary> [Serializable] public class RankItemDto { public string UserName; public int Coin; public RankItemDto(string UserName, int Coin) { this.UserName = UserName; this.Coin = Coin; } public void Change(string UserName, int Coin) { this.UserName = UserName; this.Coin = Coin; } } }
Server端创建数据模型(排行榜的数据模型)
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Protocol.Dto { /// <summary> ///排行榜的数据模型 /// </summary> [Serializable] public class RankListDto { public List<RankItemDto> rankList; public RankListDto() { rankList = new List<RankItemDto>(); } public void Clear() { rankList.Clear(); } public void Add(RankItemDto dto) { rankList.Add(dto); } } }
查找到数据库中的用户名和金币,构建数据模型
/// <summary> /// 获取排行榜信息 /// </summary> public static RankListDto GetRankListDto() { MySqlCommand cmd = new MySqlCommand(" select Username,Coin from userinfo order by Coin desc ", sqlConnect); MySqlDataReader reader = cmd.ExecuteReader(); rankListDto.Clear(); if (reader.HasRows) { while (reader.Read())//一直查询,直到查找完毕 { RankItemDto dto = new RankItemDto(reader.GetString("UserName"),reader.GetInt32("Coin")); rankListDto.Add(dto); } reader.Close(); return rankListDto; } reader.Close(); return null; } }
Server端处理客户端的请求,并返回给排行榜的数据模型
using GameServer.DadaBase; using MyServer; using Protocol.Code; using Protocol.Dto; namespace GameServer.Logic { /// <summary> /// 账号模块的处理 /// </summary> public class AccountHandler : IHandler { public void DisConnected(ClientPeer clientPeer) { DatabaseManager.OffLine(clientPeer); } /// <summary> /// 处理所有的子操作码 /// </summary> public void Receive(ClientPeer client, int subCode, object value) { switch (subCode) { case AccountCode.Register_CREQ: Register(client,value as AccountDto); break; case AccountCode.Login_CREQ: Login(client,value as AccountDto); break; case AccountCode.GetUserInfo_CREQ: GetUserInfo(client); break; case AccountCode.GetRankList_CRES: GetRankList(client); break; } } /// <summary> /// 客户端请求排行榜的处理 /// </summary> private void GetRankList(ClientPeer client) { SingleExecute.Instacne.Execute(()=> { RankListDto dto= DatabaseManager.GetRankListDto(); client.SendMsg(OpCode.Account, AccountCode.GetRankList_SRES, dto); }); } /// <summary> /// 客户端请求获得用户信息的处理 /// </summary> private void GetUserInfo(ClientPeer client) { SingleExecute.Instacne.Execute(()=> { UserDto dto = DatabaseManager.GetUserInfo(client); client.SendMsg(OpCode.Account, AccountCode.GetUserInfo_SRES, dto); }); } /// <summary> /// 客户端请求登陆的处理 /// </summary> private void Login(ClientPeer client, AccountDto dto) { SingleExecute.Instacne.Execute(()=> { if (DatabaseManager.IsExistUserName(dto.userName)==false) { //用户名不存在 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -1); return; } if (DatabaseManager.IsMatch(dto.userName,dto.password)==false) { //账号密码不匹配 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -2); return; } if (DatabaseManager.IsOnline(dto.userName)) { //当前用户已在线 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, -3); return; } DatabaseManager.Login(dto.userName,client); //登陆成功 client.SendMsg(OpCode.Account, AccountCode.Login_SRES, 0); }); } /// <summary> ///客户端注册请求的处理 /// </summary> private void Register(ClientPeer client, AccountDto dto) { ///单线程执行 ///作用:防止多个线程同时访问,数据错乱 SingleExecute.Instacne.Execute(()=> { //判断数据库中是否存在该用户,存在就给客户端返回-1 if (DatabaseManager.IsExistUserName(dto.userName)) { client.SendMsg(OpCode.Account, AccountCode.Register_SRES, -1); return; } //判断数据库中是否存在该用户,不存在就创建该用户并给客户端返回0 DatabaseManager.CreatUser(dto.userName, dto.password); client.SendMsg(OpCode.Account, AccountCode.Register_SRES, 0); }); } } }
app直播商城源码客户端处理Server端的回应,发送到排行榜脚本中处理
using Protocol.Code; using Protocol.Dto; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.SceneManagement; public class AccountHandler : BaseHandler { public override void OnReceive(int subCode, object value) { switch (subCode) { case AccountCode.Register_SRES: Register_SRES((int)value); break; case AccountCode.Login_SRES: Login_SRES((int)value); break; case AccountCode.GetUserInfo_SRES: GetUserInfo_SRES(value as UserDto); break; case AccountCode.GetRankList_SRES: GetRankList_SRES(value as RankListDto); break; } } /// <summary> /// 处理服务器端注册后的回应 /// </summary> private void Register_SRES(int value) { if (value==-1) { EventCenter.Broadcast(EventDefine.Hint,"该用户已经被注册"); return; } if (value==0) { EventCenter.Broadcast(EventDefine.Hint, "注册成功"); } } /// <summary> /// 处理服务器端登陆后的回应 /// </summary> private void Login_SRES(int value) { switch (value) { case -3: EventCenter.Broadcast(EventDefine.Hint, "当前用户已在线"); break; case -2: EventCenter.Broadcast(EventDefine.Hint, "账号密码不匹配"); break; case -1: EventCenter.Broadcast(EventDefine.Hint, "用户名不存在"); break; case 0: //请求获得用户信息 NetMsgCenter.Instance.SendMsg(OpCode.Account,AccountCode.GetUserInfo_CREQ,null); EventCenter.Broadcast(EventDefine.Hint, "登陆成功"); break; } } /// <summary> /// 处理获取用户信息服务器端的回应 /// </summary> private void GetUserInfo_SRES(UserDto dto) { Models.GameModel.userDto = dto;//把用户信息存储到数据中心(model层) SceneManager.LoadScene("02.Main"); } /// <summary> /// 处理获取排行榜信息服务器的回应 /// </summary> /// <param name="dto"></param> private void GetRankList_SRES(RankListDto dto) { //把排行榜信息发送到RankPanel去处理 EventCenter.Broadcast(EventDefine.SendRankListDto,dto); } }
排行榜脚本的处理
sing System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; using DG.Tweening; using Protocol.Dto; public class RankPanel : MonoBehaviour { public GameObject m_ItemPre; private Button btn_Close; private Transform m_Parent; private void Awake() { EventCenter.AddListener(EventDefine.ShowRankListPanel, Show); EventCenter.AddListener<RankListDto>(EventDefine.SendRankListDto,GetRankListDto); btn_Close = transform.Find("btn_Close").GetComponent<Button>(); btn_Close.onClick.AddListener(()=> { OnCloseButtonClick(); }); m_Parent = transform.Find("List/ScrollRect/Parent").transform; } private void OnCloseButtonClick() { transform.DOScale(Vector3.zero, 0.3f); } private void Show() { transform.DOScale(Vector3.one, 0.3f); } /// <summary> /// 得到排行榜的数据传输模型 /// </summary> private void GetRankListDto(RankListDto dto) { for (int i = 0; i < dto.rankList.Count; i++) { //如果当前名字和自身名字相同 if (dto.rankList[i].UserName==Models.GameModel.userDto.UserName) { GameObject go = Instantiate(m_ItemPre, m_Parent); go.transform.Find("Index/txtIndex").GetComponent<Text>().text = (i + 1).ToString(); go.transform.Find("Index/txtIndex").GetComponent<Text>().color = Color.red; go.transform.Find("txtUserName").GetComponent<Text>().text = "我"; go.transform.Find("txtUserName").GetComponent<Text>().color = Color.red; go.transform.Find("txtCoin").GetComponent<Text>().text = dto.rankList[i].Coin.ToString(); go.transform.Find("txtCoin").GetComponent<Text>().color = Color.red; } else { GameObject go = Instantiate(m_ItemPre, m_Parent); go.transform.Find("Index/txtIndex").GetComponent<Text>().text = (i + 1).ToString(); go.transform.Find("txtUserName").GetComponent<Text>().text = dto.rankList[i].UserName; go.transform.Find("txtCoin").GetComponent<Text>().text = dto.rankList[i].Coin.ToString(); } } } private void OnDestroy() { EventCenter.RemoveListener(EventDefine.ShowRankListPanel, Show); EventCenter.RemoveListener<RankListDto>(EventDefine.SendRankListDto, GetRankListDto); } }
以上就是app直播商城源码实现用户头像和排行榜的内容了。
声明:本文由云豹科技转发自我是赵某人博客,如有侵权请联系作者删除