unity战斗卡牌视频教程 第七课 - 角色创建及服务器启动
前言
预览图
本课我们讲了 保存账号密码。内容灰化和shader的代码使用等内容。
时长:41分钟
视频地址
系列教程目录汇总:http://www.bobsong.net/886.html
客户端登陆代码
对于上一课。加入了保存账号密码,真实的注册服务器接口 和登陆代码
using UnityEngine; using System.Collections; public class SceneLogin : SceneBase { #region 界面加载相关 protected override void OnInitFront() { base.OnInitFront(); _type = SceneType.SceneLogin; } protected override void OnInitSkinFront() { base.OnInitSkinFront(); SetMainSkinPath("Game/Login/SceneLogin"); } protected override void OnInitDone() { base.OnInitDone(); InitData(); } protected override void OnDestroyDone() { base.OnDestroyDone(); } protected override void OnDestroyFront() { base.OnDestroyFront(); } protected override void OnClick(GameObject target) { base.OnClick(target); ButtonClick(target); } public override void OnInit(params object[] sceneArgs) { base.OnInit(sceneArgs); } public override void OnShowed() { base.OnShowed(); } #endregion #region 数据定义 private UIInput mInputAccount; private UIInput mInputPassword; #endregion #region ui逻辑 void InitData() { mInputAccount = skinTransform.Find("InputAccount").GetComponent<UIInput>(); mInputPassword = skinTransform.Find("InputPassword").GetComponent<UIInput>(); if (PlayerPrefs.GetString("Account") != null && PlayerPrefs.GetString("Account") != "") { mInputAccount.value = PlayerPrefs.GetString("Account"); } if (PlayerPrefs.GetString("Password") != null && PlayerPrefs.GetString("Password") != "") { mInputPassword.value = PlayerPrefs.GetString("Password"); } } void ButtonClick(GameObject click) { if(click.name.Equals("BtnLogin")) { if (mInputAccount.value == "") { LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("没有输入账号!"); } else if (mInputPassword.value == "") { LogicMgr.GetInstance().GetLogic<LogicTips>().AddTips("没有输入密码!"); } else { ActionParam ap = new ActionParam(); GameSetting.Instance.Pid = mInputAccount.value; GameSetting.Instance.Password = mInputPassword.value; Net.Instance.Send((int)ActionType.Login, LoginReturn, null); } } else if(click.name.Equals("BtnRegis")) { Net.Instance.Send((int)ActionType.Regis, RegisReturn, null); } } #endregion #region 服务器返回 void LoginReturn(ActionResult action) { if(action != null) { PlayerPrefs.SetString("Account", mInputAccount.value); PlayerPrefs.SetString("Password", mInputPassword.value); } if (action != null && action.Get<int>("GuideID") == (int)ActionType.CreateRote) { //登陆返回未创建角色。需要跳转创建角色 SceneMgr.GetInstance().SwitchingScene(SceneType.SceneCreateRote,111,222,333,444); } else { SceneMgr.GetInstance().SwitchingScene(SceneType.SceneTest); } } void RegisReturn(ActionResult action) { if (action == null) return; Debug.LogError("一键注册成功:账号:" + action.Get<string>("passportID") + " 密码:" + action.Get<string>("password")); mInputAccount.value = action.Get<string>("passportID"); mInputPassword.value = action.Get<string>("password"); } #endregion }
客户端创建角色代码
using UnityEngine; using System.Collections; public class SceneCreateRote : SceneBase { #region 界面加载相关 protected override void OnInitFront() { base.OnInitFront(); _type = SceneType.SceneCreateRote; } protected override void OnInitSkinFront() { base.OnInitSkinFront(); SetMainSkinPath("Game/Login/SceneCreateRote"); } protected override void OnInitDone() { base.OnInitDone(); } protected override void OnDestroyDone() { base.OnDestroyDone(); } protected override void OnDestroyFront() { base.OnDestroyFront(); } protected override void OnClick(GameObject target) { base.OnClick(target); ButtonClick(target); } public override void OnInit(params object[] sceneArgs) { base.OnInit(sceneArgs); int i1 = (int)sceneArgs[0]; int i2 = (int)sceneArgs[1]; Debug.LogError(i1 + "--" + i2); } public override void OnShowed() { base.OnShowed(); InitData(); } #endregion #region 数据定义 private UIInput mInputAccount; private UITexture mBtnMan; private UITexture mBtnWoman; private int mSex = 0; #endregion #region ui逻辑 void InitData() { mInputAccount = skinTransform.Find("InputAccount").GetComponent<UIInput>(); mBtnMan = skinTransform.Find("BtnMan").GetComponent<UITexture>(); mBtnWoman = skinTransform.Find("BtnWoman").GetComponent<UITexture>(); } void ButtonClick(GameObject click) { if (click.name.Equals("BtnCreate")) { ActionParam ap = new ActionParam(); ap["roleName"] = mInputAccount.value; ap["Sex"] = mSex; Net.Instance.Send((int)ActionType.CreateRote, CreateRoteReturn, ap); } else if (click.name.Equals("BtnMan")) { mSex = 0; mBtnWoman.shader = Shader.Find("Unlit/Transparent Colored Gray"); mBtnMan.shader = Shader.Find("Unlit/Transparent Colored"); } else if (click.name.Equals("BtnWoman")) { mSex = 1; mBtnMan.shader = Shader.Find("Unlit/Transparent Colored Gray"); mBtnWoman.shader = Shader.Find("Unlit/Transparent Colored"); } } #endregion #region 服务器返回 void CreateRoteReturn(ActionResult action) { SceneMgr.GetInstance().SwitchingScene(SceneType.SceneLoading); } #endregion }
服务器代码
登陆代码
using GameServer.Model; using System; using System.Collections.Generic; using ZyGames.Framework.Cache.Generic; using ZyGames.Framework.Game.Cache; using ZyGames.Framework.Game.Context; using ZyGames.Framework.Game.Contract; using ZyGames.Framework.Game.Contract.Action; using ZyGames.Framework.Game.Service; namespace GameServer.CsScript.Action { /// <summary> /// 1004_用户登录 /// </summary> public class Action1004 : LoginExtendAction { public Action1004(ActionGetter actionGetter) : base((short)1004, actionGetter) { } protected override bool DoSuccess(int userId, out IUser user) { user = null; Console.WriteLine("登录成功!"); user = null; //原因:重登录时,数据会回档问题 var cacheSet = new GameDataCacheSet<GameUser>(); GameUser userInfo = cacheSet.FindKey(userId.ToString()); if (userInfo == null) { //通知客户跳转到创建角色接口 GuideId = 1005; return true; } else { user = new SessionUser(userInfo); userInfo.LoginTime = DateTime.Now; userInfo.SessionID = Sid; userInfo.IsOnline = true; userInfo.ServerId = ServerID; userInfo.GameId = GameType; } return true; //如写登录日志 } public override void TakeActionAffter(bool state) { base.TakeActionAffter(state); } } }
一键注册代码
using System; using System.Collections.Generic; using ZyGames.Framework.Game.Contract.Action; using ZyGames.Framework.Game.Service; using ZyGames.Framework.Game.Sns; using ZyGames.Framework.Game.Lang; using ZyGames.Framework.Game.Runtime; namespace GameServer.CsScript.Action { /// <summary> /// 获取通行证接口 /// </summary> /// <remarks>继续BaseStruct类:允许无身份认证的请求;AuthorizeAction:需要身份认证的请求</remarks> public class Action1002 : BaseStruct { /// <summary> /// 用户手机类型 /// </summary> private int _mobileType; /// <summary> /// 游戏类型 /// </summary> private int _gameType; /// <summary> /// 渠道ID游戏推广渠道编号 /// </summary> private int _retailID; /// <summary> /// 客户端版本号末填写则默认为1.0版本 /// </summary> private string _clientAppVersion; /// <summary> /// 屏幕宽度(像素) /// </summary> private int _screenX; /// <summary> /// 屏幕高度(像素) /// </summary> private int _screenY; /// <summary> /// 设备ID /// </summary> private string _deviceID; /// <summary> /// 分服ID /// </summary> private int _serverID; /// <summary> /// 通行证ID /// </summary> private string _passportID; /// <summary> /// 密码 /// </summary> private string _password; public Action1002(ActionGetter actionGetter) : base((short)1002, actionGetter) { } /// <summary> /// 客户端请求的参数较验 /// </summary> /// <returns>false:中断后面的方式执行并返回Error</returns> public override bool GetUrlElement() { if (httpGet.GetInt("MobileType", ref _mobileType) && httpGet.GetInt("GameType", ref _gameType) && httpGet.GetInt("RetailID", ref _retailID) && httpGet.GetString("ClientAppVersion", ref _clientAppVersion) && httpGet.GetString("DeviceID", ref _deviceID) && httpGet.GetInt("ServerID", ref _serverID)) { httpGet.GetInt("ScreenX", ref _screenX); httpGet.GetInt("ScreenY", ref _screenY); return true; } return false; } /// <summary> /// 业务逻辑处理 /// </summary> /// <returns>false:中断后面的方式执行并返回Error</returns> public override bool TakeAction() { try { Console.WriteLine("_deviceID:" + _deviceID); string[] userList = SnsManager.GetRegPassport(_deviceID); _passportID = userList[0]; _password = userList[1]; return true; } catch (Exception ex) { this.SaveLog(ex); return false; } } /// <summary> /// 下发给客户的包结构数据 /// </summary> public override void BuildPacket() { this.PushIntoStack(_passportID); this.PushIntoStack(_password); } } }
创建角色代码
using System; using System.Collections.Generic; using ZyGames.Framework.Cache.Generic; using ZyGames.Framework.Common; using ZyGames.Framework.Game.Context; using ZyGames.Framework.Game.Contract; using ZyGames.Framework.Game.Contract.Action; using ZyGames.Framework.Game.Lang; using ZyGames.Framework.Game.Service; using ZyGames.Framework.Model; using GameServer.Model; namespace GameServer.CsScript.Action { /// <summary> /// 1005_创建角色接口 /// </summary> public class Action1005 : RegisterAction { public Action1005(ActionGetter actionGetter) : base((short)1005, actionGetter) { } protected override bool GetActionParam() { return true; } protected override bool CreateUserRole(out IUser user) { user = null; if (UserName.Length < 2 || UserName.Length > 12)//如果名字过长或过短 { ErrorCode = Language.Instance.ErrorCode; ErrorInfo = Language.Instance.St1005_UserNameNotEnough; return false; } if (GameUser.IsNickName(UserName)) { ErrorCode = Language.Instance.ErrorCode; ErrorInfo = Language.Instance.St1005_UserNameNotEnough; return false; } var userCache = new PersonalCacheStruct<GameUser>(); GameUser gameUser; if (userCache.TryFindKey(UserId.ToString(), out gameUser) == LoadingStatus.Success) { if (gameUser == null) { gameUser = new GameUser { UserId = UserId, PassportId = Pid, NickName = UserName, Sex = Sex, }; userCache.Add(gameUser); } gameUser.Exp = 0; gameUser.Lv = 1; gameUser.LoginTime = DateTime.Now; gameUser.Action = 100; return true; } return false; } public override void BuildPacket() { } public override void TakeActionAffter(bool state) { base.TakeActionAffter(state); } } }
为什么我找不到视频下载地址???