赞
踩
充值功能的UI搭建
RechargePanel :Unity充值UI脚本
using DG.Tweening; using Protocol.Code; using System.Collections; using System.Collections.Generic; using UnityEngine; using UnityEngine.UI; public class RechargePanel : MonoBehaviour { private GameObject goods; private Button[] btnGoodsArr;//所有的充值按钮 private Button btn_Close; private int RechargeCount;//每次充值的金币数 private void Awake() { Init(); } private void Init() { EventCenter.AddListener(EventDefine.ShowRechargePanel,Show); //监听收到Server端完成充值后的回应的方法 EventCenter.AddListener<int>(EventDefine.SendRechargePanel,UpdateCoinCount); goods = transform.Find("goods").gameObject; btnGoodsArr = new Button[goods.transform.childCount]; //得到所有充值按钮 for (int i = 0; i < goods.transform.childCount; i++) { btnGoodsArr[i] = goods.transform.GetChild(i).GetComponentInChildren<Button>(); } btn_Close = transform.Find("btn_Close").GetComponent<Button>(); btn_Close.onClick.AddListener(() => { OnCloseButtonClick(); }); //添加每个充值按钮的点击事件 btnGoodsArr[0].onClick.AddListener(delegate{ OnRechargeButtonClick(10); }); btnGoodsArr[1].onClick.AddListener(delegate{ OnRechargeButtonClick(20); }); btnGoodsArr[2].onClick.AddListener(delegate{ OnRechargeButtonClick(50); }); btnGoodsArr[3].onClick.AddListener(delegate{ OnRechargeButtonClick(100); }); btnGoodsArr[4].onClick.AddListener(delegate{ OnRechargeButtonClick(200); }); btnGoodsArr[5].onClick.AddListener(delegate{ OnRechargeButtonClick(500); }); } /// <summary> /// 关闭按钮的点击 /// </summary> private void OnCloseButtonClick() { transform.DOScale(Vector3.zero, 0.3f); } //显示的方法 private void Show() { transform.DOScale(Vector3.one, 0.3f); } /// <summary> /// 充值按钮的点击 /// </summary> /// <param name="coin"></param> private void OnRechargeButtonClick(int coin) { RechargeCount = coin; NetMsgCenter.Instance.SendMsg(OpCode.Account,AccountCode.GetRecharge_CREQ,coin); } /// <summary> /// 金币充值成功的提示 /// </summary> private void UpdateCoinCount(int value) { Models.GameModel.userDto.Coin = value;//保存每次充值后的金币数 EventCenter.Broadcast(EventDefine.Hint,"充值"+RechargeCount+"金币成功"); } private void OnDestroy() { //移除监听 EventCenter.RemoveListener<int>(EventDefine.SendRechargePanel, UpdateCoinCount); EventCenter.RemoveListener(EventDefine.ShowRechargePanel, Show); } }
数据库的处理:更新金币数,加上每次需要充值的金币,并返回充值成功后的金币数量
/// <summary> /// 更新金币数量 /// </summary> public static int UpdateCoinCount(int userId,int value) { MySqlCommand cmd = new MySqlCommand("select Coin From userinfo where ID=@id",sqlConnect); cmd.Parameters.AddWithValue("id",userId); MySqlDataReader reader= cmd.ExecuteReader(); if (reader.HasRows) { reader.Read(); int remainCoinCount = reader.GetInt32("Coin"); reader.Close(); MySqlCommand updateCmd = new MySqlCommand("update userinfo set Coin=@coin where ID=@id",sqlConnect); updateCmd.Parameters.AddWithValue("coin",remainCoinCount+value); updateCmd.Parameters.AddWithValue("id", userId); updateCmd.ExecuteNonQuery(); return remainCoinCount + value; } reader.Close(); return 0; } }
Server端的处理:发送给客户端充值成功后的金币
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; 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; case AccountCode.GetRecharge_CREQ: GetRecharge_CREQ(client,(int)value); break; } } /// <summary> /// 客户端请求充值的处理 /// </summary> private void GetRecharge_CREQ(ClientPeer client,int coinCount) { SingleExecute.Instacne.Execute(()=> { int count = DatabaseManager.UpdateCoinCount(client.id, coinCount); //完成充值请求后的处理,回应给客户端 client.SendMsg(OpCode.Account, AccountCode.GetRecharge_SRES, count); }); } /// <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); }); } } }
客户端处理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; case AccountCode.GetRecharge_SRES: //发送到充值脚本自己处理 EventCenter.Broadcast(EventDefine.SendRechargePanel, (int)value); 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); } }
客户端处理充值成功后的操作
有需要学习视频的欢迎关注微信公众号:
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。