Unity進(jìn)階–通過(guò)PhotonServer實(shí)現(xiàn)人物選擇和多人同步–PhotonServer(四)
服務(wù)端
-
服務(wù)端結(jié)構(gòu)如下:
-
UserModel
using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Model.User { public class UserModel { public int ID; public int Hp; public float[] Points = new float[] { -4, 1, -2 }; public UserInfo userInfo; } public class UserInfo { public static UserInfo[] userList = new UserInfo[] { new UserInfo(){ ModelID = 0, MaxHp = 100, Attack = 20, Speed = 3 }, new UserInfo(){ ModelID = 1, MaxHp = 70, Attack = 40, Speed = 4 } }; public int ModelID; public int MaxHp; public int Attack; public int Speed; } }
-
Messaage
namespace Net { public class Message { public byte Type; public int Command; public object Content; public Message() { } public Message(byte type, int command, object content) { Type = type; Command = command; Content = content; } } //消息類(lèi)型 public class MessageType { //unity //類(lèi)型 public static byte Type_Audio = 1; public static byte Type_UI = 2; public static byte Type_Player = 3; //聲音命令 public static int Audio_PlaySound = 100; public static int Audio_StopSound = 101; public static int Audio_PlayMusic = 102; //UI命令 public static int UI_ShowPanel = 200; public static int UI_AddScore = 201; public static int UI_ShowShop = 202; //網(wǎng)絡(luò) public const byte Type_Account = 1; public const byte Type_User = 2; //注冊(cè)賬號(hào) public const int Account_Register = 100; public const int Account_Register_Res = 101; //登陸 public const int Account_Login = 102; public const int Account_Login_res = 103; //選擇角色 public const int User_Select = 104; public const int User_Select_res = 105; public const int User_Create_Event = 106; //刪除角色 public const int User_Remove_Event = 107; } }
-
PSPeer
using System; using System.Collections.Generic; using Net; using Photon.SocketServer; using PhotonHostRuntimeInterfaces; using PhotonServerFirst.Bll; namespace PhotonServerFirst { public class PSPeer : ClientPeer { public PSPeer(InitRequest initRequest) : base(initRequest) { } //處理客戶端斷開(kāi)的后續(xù)工作 protected override void OnDisconnect(DisconnectReason reasonCode, string reasonDetail) { //關(guān)閉管理器 BLLManager.Instance.accountBLL.OnDisconnect(this); BLLManager.Instance.userBLL.OnDisconnect(this); } //處理客戶端的請(qǐng)求 protected override void OnOperationRequest(OperationRequest operationRequest, SendParameters sendParameters) { PSTest.log.Info("收到客戶端的消息"); var dic = operationRequest.Parameters; //打包,轉(zhuǎn)為PhotonMessage Message message = new Message(); message.Type = (byte)dic[0]; message.Command = (int)dic[1]; List<object> objs = new List<object>(); for (byte i = 2; i < dic.Count; i++) { objs.Add(dic[i]); } message.Content = objs.ToArray(); //消息分發(fā) switch (message.Type) { case MessageType.Type_Account: BLLManager.Instance.accountBLL.OnOperationRequest(this, message); break; case MessageType.Type_User: BLLManager.Instance.userBLL.OnOperationRequest(this, message); break; } } } }
-
UserBLL
using Net; using PhotonServerFirst.Model.User; using PhotonServerFirst.Dal; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Bll.User { class UserBLL : IMessageHandler { public void OnDisconnect(PSPeer peer) { //下線 UserModel user = DALManager.Instance.userDAL.GetUserModel(peer); //移除角色 DALManager.Instance.userDAL.RemoveUser(peer); //通知其他角色該角色已經(jīng)下線 foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers()) { SendMessage.Send(otherpeer, MessageType.Type_User, MessageType.User_Remove_Event, user.ID); } } public void OnOperationRequest(PSPeer peer, Message message) { switch (message.Command) { case MessageType.User_Select: //有人選了角色 //創(chuàng)建其他角色 CreateOtherUser(peer, message); //創(chuàng)建自己的角色 CreateUser(peer, message); //通知其他角色創(chuàng)建自己 CreateUserByOthers(peer, message); break; } } //創(chuàng)建目前已經(jīng)存在的角色 void CreateOtherUser(PSPeer peer, Message message) { //遍歷已經(jīng)登陸的角色 foreach (UserModel userModel in DALManager.Instance.userDAL.GetUserModels()) { //給登錄的客戶端響應(yīng),讓其創(chuàng)建這些角色 角色id 模型id 位置 SendMessage.Send(peer, MessageType.Type_User, MessageType.User_Create_Event, userModel.ID, userModel.userInfo.ModelID, userModel.Points); } } //創(chuàng)建自己角色 void CreateUser(PSPeer peer, Message message) { object[] obj = (object[])message.Content; //客戶端傳來(lái)模型id int modelId = (int)obj[0]; //創(chuàng)建角色 int userId = DALManager.Instance.userDAL.AddUser(peer, modelId); //告訴客戶端創(chuàng)建自己的角色 SendMessage.Send(peer, MessageType.Type_User, MessageType.User_Select_res, userId, modelId, DALManager.Instance.userDAL.GetUserModel(peer).Points); } //通知其他角色創(chuàng)建自己 void CreateUserByOthers(PSPeer peer, Message message) { UserModel userModel = DALManager.Instance.userDAL.GetUserModel(peer); //遍歷全部客戶端,發(fā)送消息 foreach (PSPeer otherpeer in DALManager.Instance.userDAL.GetuserPeers()) { if (otherpeer == peer) { continue; } SendMessage.Send(otherpeer, MessageType.Type_User, MessageType.User_Create_Event, userModel.ID, userModel.userInfo.ModelID, userModel.Points); } } } }
-
BLLManager
using PhotonServerFirst.Bll.User; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Bll { public class BLLManager { private static BLLManager bLLManager; public static BLLManager Instance { get { if(bLLManager == null) { bLLManager = new BLLManager(); } return bLLManager; } } //登錄注冊(cè)管理 public IMessageHandler accountBLL; //角色管理 public IMessageHandler userBLL; private BLLManager() { accountBLL = new PhsotonServerFirst.Bll.Account.AccountBLL(); userBLL = new UserBLL(); } } }
-
UserDAL
using System; using System.Collections.Generic; using System.Linq; using System.Text; using PhotonServerFirst.Model.User; namespace PhotonServerFirst.Dal.User { class UserDAL { //角色保存集合 private Dictionary<PSPeer, UserModel> peerUserDic = new Dictionary<PSPeer, UserModel>(); private int userId = 1; /// <summary> ///添加一名角色 /// </summary> /// <param name="peer">連接對(duì)象</param> /// <param name="index">幾號(hào)角色</param> /// <returns>用戶id</returns> public int AddUser(PSPeer peer, int index) { UserModel model = new UserModel(); model.ID = userId++; model.userInfo = UserInfo.userList[index]; model.Hp = model.userInfo.MaxHp; if (index == 1) { model.Points = new float[] { 0, 2, -2 }; } peerUserDic.Add(peer, model); return model.ID; } ///<summary> ///移除一名角色 /// </summary> public void RemoveUser(PSPeer peer) { peerUserDic.Remove(peer); } ///<summary> ///得到用戶模型 ///</summary> ///<param name="peer">連接對(duì)象</param> ///<returns>用戶模型</returns> public UserModel GetUserModel(PSPeer peer){ return peerUserDic[peer]; } ///<summary> ///得到全部的用戶模型 ///</summary> public UserModel[] GetUserModels() { return peerUserDic.Values.ToArray(); } ///<summary> ///得到全部有角色的連接對(duì)象 ///</summary> public PSPeer[] GetuserPeers() { return peerUserDic.Keys.ToArray(); } } }
-
DALManager
using PhotonServerFirst.Bll; using PhotonServerFirst.Dal.User; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace PhotonServerFirst.Dal { class DALManager { private static DALManager dALManager; public static DALManager Instance { get { if (dALManager == null) { dALManager = new DALManager(); } return dALManager; } } //登錄注冊(cè)管理 public AccountDAL accountDAL; //用戶管理 public UserDAL userDAL; private DALManager() { accountDAL = new AccountDAL(); userDAL = new UserDAL(); } } }
-
-
-
客戶端
- 客戶端頁(yè)面
-
綁在panel上
using System.Collections; using System.Collections.Generic; using UnityEngine; using Net; public class SelectPanel : MonoBehaviour { // Start is called before the first frame update void Start() { } // Update is called once per frame void Update() { } public void SelectHero(int modelId){ //告訴服務(wù)器創(chuàng)建角色 PhotonManager.Instance.Send(MessageType.Type_User, MessageType.User_Select, modelId); //摧毀選擇角色頁(yè)面 Destroy(gameObject); //顯示計(jì)分版 UImanager.Instance.SetActive("score", true); } }
-
后臺(tái)持續(xù)運(yùn)行
-
建一個(gè)usermanager,綁定以下腳本
using System.Collections; using System.Collections.Generic; using UnityEngine; using Net; public class UserManager : ManagerBase { void Start(){ MessageCenter.Instance.Register(this); } public override void ReceiveMessage(Message message){ base.ReceiveMessage(message); //打包 object[] obs = (object[])message.Content; //消息分發(fā) switch(message.Command){ case MessageType.User_Select_res: selectUser(obs); break; case MessageType.User_Create_Event: CreateotherUser(obs); break; case MessageType.User_Remove_Event: RemoveUser(obs); break; } } public override byte GetMessageType(){ return MessageType.Type_User; } //選擇了自己的角色 void selectUser(object[] objs){ int userId =(int)objs[0]; int modelId = (int)objs[1]; float[] point = (float[])objs[2]; if (userId > 0) { //創(chuàng)建角色 GameObject UserPre = Resources.Load<GameObject>("User/" + modelId); UserControll user = Instantiate(UserPre,new Vector3(point[0],point[1],point[2]),Quaternion.identity).GetComponent<UserControll>(); UserControll.ID =userId; //保存到集合中 UserControll.idUserDic.Add(userId, user); } else { Debug.Log("創(chuàng)建角色失敗"); } } //創(chuàng)建其他角色 void CreateotherUser(object[] objs){ //打包 int userId = (int)objs[0]; int modelId = (int)objs [1]; float[] point = (float[])objs[2]; GameObject UserPre = Resources.Load<GameObject>("User/" + modelId); UserControll user = Instantiate(UserPre, new Vector3(point[0],point[1], point[2]),Quaternion.identity).GetComponent<UserControll>(); UserControll.idUserDic.Add(userId, user); } //刪除一名角色 void RemoveUser(object[] objs){ int userId = (int)objs[0]; UserControll user = UserControll.idUserDic[userId]; UserControll.idUserDic.Remove(userId); Destroy(user.gameObject); } }
-
給物體上綁定
using System.Collections; using System.Collections.Generic; using UnityEngine; public class UserControll : MonoBehaviour { //保存了所有角色的集合 public static Dictionary<int, UserControll> idUserDic = new Dictionary<int, UserControll>(); //當(dāng)前客戶端角色的id public static int ID; private Animator ani; // Start is called before the first frame update void Start() { ani = GetComponent<Animator>(); } // Update is called once per frame void Update() { } }
-
別忘了按鈕綁定文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-660612.html
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-660612.html
-
到了這里,關(guān)于Unity進(jìn)階–通過(guò)PhotonServer實(shí)現(xiàn)人物選擇和多人同步–PhotonServer(四)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!