当前位置:   article > 正文

Unity进阶–通过PhotonServer实现联网登录注册功能(客户端)–PhotonServer(三)_photon engine连接服务器注册

photon engine连接服务器注册

Unity进阶–通过PhotonServer实现联网登录注册功能(客户端)–PhotonServer(三)

前情提要

  • 单例泛型类

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    
    public class MyrSingletonBase<T> : MonoBehaviour where T : MonoBehaviour
    {
        private static T instance;
        public static T Instance {
            get
            {
                return instance;
            }
        }
    
        protected virtual void Awake() {
            instance = this as T;
        }
    
        protected virtual void OnDestroy() {
            instance = null;
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
  • ManagerBase

    using System.Collections;
    using System.Collections.Generic;
    using Net;
    using UnityEngine;
    
    public abstract class ManagerBase : MyrSingletonBase<ManagerBase>
    {
       public List<MoonBase> Monos = new List<MoonBase>();
    
       public void Register(MoonBase mono){
        if (!Monos.Contains(mono)){
            Monos.Add(mono);
        }
       }
       
       public virtual void ReceiveMessage(Message message){
        if (message.Type != GetMessageType()){
            return;
        } 
        foreach (var mono in Monos){
            mono.ReceiveMessage(message);
        }
       }
    
       
       public abstract byte GetMessageType();
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 消息中心

      using System.Collections;
      using System.Collections.Generic;
      using Net;
      using UnityEngine;
      
      public class MessageCenter : MyrSingletonBase<MessageCenter>
      {
          public static List<ManagerBase> Managers = new List<ManagerBase>();
      
          public void Register(ManagerBase manager){
              if (!Managers.Contains(manager)){
                  Managers.Add(manager);
              }
          }
      
          public void SendCustomMessage(Message message){
              foreach(var manager in Managers){
                  manager.ReceiveMessage(message);
              }
          }
      
          public static void SendMessage(Message message){
               foreach(var manager in Managers){
                  manager.ReceiveMessage(message);
              }
          }
      }
      
      
      • 1
      • 2
      • 3
      • 4
      • 5
      • 6
      • 7
      • 8
      • 9
      • 10
      • 11
      • 12
      • 13
      • 14
      • 15
      • 16
      • 17
      • 18
      • 19
      • 20
      • 21
      • 22
      • 23
      • 24
      • 25
      • 26
      • 27
      • 28
      • manager下的组件基础

        using System.Collections;
        using System.Collections.Generic;
        using Net;
        using UnityEngine;
        
        public class MoonBase : MonoBehaviour
        {
            public virtual void ReceiveMessage(Message message){
                
            }
        }
        
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
      • uiManager(绑在canvas上)

        using System.Collections;
        using System.Collections.Generic;
        using Net;
        using UnityEngine;
        
        public class UiManager : ManagerBase
        {
            void Start()
            {
                MessageCenter.Instance.Register(this);
            }
        
          public override byte GetMessageType()
            {
                return MessageType.Type_UI;
            }
            
        }
        
        
        
        • 1
        • 2
        • 3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11
        • 12
        • 13
        • 14
        • 15
        • 16
        • 17
        • 18
        • 19
        • 20
  • PhotonManager

    using System.Collections;
    using System.Collections.Generic;
    using UnityEngine;
    using ExitGames.Client.Photon;
    using Net;
    
    public class PhotonManager : MyrSingletonBase<PhotonManager>, IPhotonPeerListener
    {
        private  PhotonPeer peer;
        
        void Awake() {
            base.Awake();
            DontDestroyOnLoad(this);
        }
        // Start is called before the first frame update
        void Start()
        {
            peer = new PhotonPeer(this, ConnectionProtocol.Tcp);
            peer.Connect("127.0.0.1:4530", "PhotonServerFirst");
        }
    
        void Update()
        {
            peer.Service();
        }
    
        private void OnDestroy() {
            base.OnDestroy();
            //断开连接
            peer.Disconnect();    
        }
    
        public void DebugReturn(DebugLevel level, string message)
        {
    
        }
    
        /// <summary>
        /// 接收服务器事件
        /// </summary>
        /// <param name="eventData"></param>
        public void OnEvent(EventData eventData)
        {
            //拆包
            Message msg = new Message();
            msg.Type = (byte)eventData.Parameters[0];
            msg.Command = (int)eventData. Parameters[1];
            List<object> list = new List<object>();
            for (byte i = 2; i < eventData.Parameters.Count; i++){
                list.Add(eventData.Parameters[i]);
            }
            msg.Content = list.ToArray();
            MessageCenter.SendMessage(msg);
        }
    
        /// <summary>
        /// 接收服务器响应
        /// </summary>
        /// <param name="operationResponse"></param>
        public void OnOperationResponse(OperationResponse operationResponse)
        {
            if (operationResponse.OperationCode == 1){
                Debug.Log(operationResponse.Parameters[1]);
            }
    
        }
    
        /// <summary>
        /// 状态改变
        /// </summary>
        /// <param name="statusCode"></param>
        public void OnStatusChanged(StatusCode statusCode)
        {
            Debug.Log(statusCode);
        }
    
        /// <summary>
        /// 发送消息
        /// </summary>
        public void Send(byte type, int command, params object[] objs)
        {
            Dictionary<byte, object> dic = new Dictionary<byte,object>();
            dic.Add(0,type);
            dic.Add(1,command);
            byte i = 2;
            foreach (object o in objs){
                dic.Add(i++, o);
            }
            peer.OpCustom(0, dic, true);
        }
    
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93

客户端部分

  1. 搭个页面

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-2kLIN0oV-1692427488913)(../AppData/Roaming/Typora/typora-user-images/image-20230808114712080.png)]

  2. panel上挂上脚本

    using System.Collections;
    using System.Collections.Generic;
    using Net;
    using UnityEngine;
    using UnityEngine.UI;
    
    public class LoginPanel : MoonBase
    {
        //账号和密码输入框
        public InputField AccountField;
        public InputField PasswordField;
        // Start is called before the first frame update
        void Start()
        {
            UiManager.Instance.Register(this);
        }
    
        // Update is called once per frame
        void Update()
        {
            
        }
    
        public override void ReceiveMessage(Message message){
            //判断是否是自己该传递的消息
            base.ReceiveMessage(message);
            //判断消息命令
            switch (message.Command)
            {
                case MessageType.Account_Register_Res:
                    Debug.Log("注册成功");
                    break;
                case MessageType.Account_Login_res:
                    Destroy(gameObject);
                    break;
            }
        }
    
        //注册
        public void Register(){
            PhotonManager.Instance.Send(MessageType.Type_Account, MessageType.Account_Register, AccountField.text, PasswordField.text);
        }
    
        //登录
        public void Login() {
            PhotonManager.Instance.Send(MessageType.Type_Account, MessageType.Account_Login, AccountField.text, PasswordField.text);
        }
    }
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
  3. 绑定对象,绑定事件

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-jILwqbyK-1692427488914)(../AppData/Roaming/Typora/typora-user-images/image-20230808160430040.png)]

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-lmhcdlev-1692427488915)(../AppData/Roaming/Typora/typora-user-images/image-20230808161310270.png)]

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/888198
推荐阅读
相关标签
  

闽ICP备14008679号