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

news2024/9/26 1:26:17

文章目录

  • Unity进阶–通过PhotonServer实现联网登录注册功能(客户端)–PhotonServer(三)
    • 前情提要
    • 客户端部分

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;
        }
    }
    
    
  • 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();
    }
    
    
    • 消息中心

      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);
              }
          }
      }
      
      
      • manager下的组件基础

        using System.Collections;
        using System.Collections.Generic;
        using Net;
        using UnityEngine;
        
        public class MoonBase : MonoBehaviour
        {
            public virtual void ReceiveMessage(Message message){
                
            }
        }
        
        
      • 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;
            }
            
        }
        
        
        
  • 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. 搭个页面

    [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(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);
        }
    }
    
    
  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)]

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/899601.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

Android6:片段和导航

创建项目Secret Message strings.xml <resources><string name"app_name">Secret Message</string><string name"welcome_text">Welcome to the Secret Message app!Use this app to encrypt a secret message.Click on the Star…

周末时间在家重新做了一个电脑系统,手艺没有丢!!!

有个朋友的电脑抱怨自己太卡&#xff0c;有缘见过几次他的电脑&#xff0c;确实哦&#xff0c;10年的老笔记本了&#xff0c;关键还是日本买的东芝t552,配置4G500G&#xff0c;昨天晚上朋友提过来的时候&#xff0c;大吃已经&#xff0c;还以为是电磁炉呢。看下面的图片就知道了…

平方数之和(力扣)双指针 JAVA

给定一个非负整数 c &#xff0c;你要判断是否存在两个整数 a 和 b&#xff0c;使得 a&#xff3e;2 b&#xff3e;2 c 。 示例 1&#xff1a; 输入&#xff1a;c 5 输出&#xff1a;true 解释&#xff1a;1 * 1 2 * 2 5 示例 2&#xff1a; 输入&#xff1a;c 3 输出&am…

Linux系统调试——核心转储(core dump)

本篇讲解Linux应用程序发生Segmentation fault段错误时&#xff0c;如何利用core dump文件定位错误。 核心转储 在 Linux 系统中&#xff0c;常将“主内存”称为核心(core)&#xff0c;而核心映像(core image) 就是 “进程”(process)执行当时的内存内容。 当进程发生错误或…

计算机竞赛 图像识别-人脸识别与疲劳检测 - python opencv

文章目录 0 前言1 课题背景2 Dlib人脸识别2.1 简介2.2 Dlib优点2.3 相关代码2.4 人脸数据库2.5 人脸录入加识别效果 3 疲劳检测算法3.1 眼睛检测算法3.3 点头检测算法 4 PyQt54.1 简介4.2相关界面代码 5 最后 0 前言 &#x1f525; 优质竞赛项目系列&#xff0c;今天要分享的是…

LVS-DR模型实例

一、LVS-DR集群介绍 LVS-DR&#xff08;Linux Virtual Server Director Server&#xff09;工作模式&#xff0c;是生产环境中最常用的一 种工作模式。 1、LVS-DR 工作原理 LVS-DR 模式&#xff0c;Director Server 作为群集的访问入口&#xff0c;不作为网关使用&#xff0…

借助Midjourney创作龙九子图

&#xff08;本文阅读时间&#xff1a;5 分钟&#xff09; 《西游记》中有这么一段描写&#xff1a; 龙王道&#xff1a;“舍妹有九个儿子。那八个都是好的。第一个小黄龙&#xff0c;见居淮渎&#xff1b;第二个小骊龙&#xff0c;见住济渎&#xff1b;第三个青背龙&#xff0…

Linux 消息队列的创建与使用

消息队列的创建与使用 进程a发送一条消息&#xff0c;进程b读取消息。 a.c代码&#xff1a; b.c代码&#xff1a; 1.a进程创建向消息队列&#xff0c;并向消息队列中发送消息 运行a程序之前&#xff0c;当前系统中消息队列的数量为0&#xff1a; 运行一次a程序&#xff0c;消…

JVM——StringTable面试案例+垃圾回收+性能调优+直接内存

JVM——引言JVM内存结构_北岭山脚鼠鼠的博客-CSDN博客 书接上回内存结构——方法区。 这里常量池是运行时常量池。 方法区 面试题 intern()方法 intern() 方法用于在运行时将字符串添加到内部的字符串池stringtable中&#xff0c;并返回字符串池stringtable中的引用。 返…

【rust/egui】(三)看看template的app.rs:序列化、持久化存储

说在前面 rust新手&#xff0c;egui没啥找到啥教程&#xff0c;这里自己记录下学习过程环境&#xff1a;windows11 22H2rust版本&#xff1a;rustc 1.71.1egui版本&#xff1a;0.22.0eframe版本&#xff1a;0.22.0上一篇&#xff1a;这里 serde app.rs中首先定义了我们的Templ…

2023.8.19-2023.8.XX 周报【人脸3D+虚拟服装方向基础调研-Cycle Diffusion\Diffusion-GAN\】更新中

学习目标 1. 这篇是做diffusion和gan结合的&#xff0c;可以参照一下看看能不能做cyclegan的形式&#xff0c;同时也可以调研一下有没有人follow这篇论文做了类似cyclegan的事情 Diffusion-GAN论文精读https://arxiv.org/abs/2206.02262 2. https://arxiv.org/abs/2212.06…

python——json、字典的区别及相互转换方法

前言 json&#xff0c;是一种轻量级的数据交换格式&#xff0c;由JavaScript语言创建&#xff0c;广泛应用于网页数据交互&#xff0c;常见于爬虫和数据分析领域。 json格式简洁、结构清晰&#xff0c;存储格式为&#xff1a;键值对&#xff08;key:value&#xff09; 在pytho…

创作的1024天 分享月入5K的副业心得

机缘 今天早上醒来打开电脑&#xff0c;和往常一样点开csdn&#xff0c;看见有一封私信&#xff0c;原来是系统通知&#xff0c;今天是我成为创作者的1024天&#xff0c;那就趁着这个机会&#xff0c;分享一下目前我月入5K的副业心得。我是一个普通人&#xff0c;最初想成为创…

【100天精通python】Day41:python网络爬虫开发_爬虫基础入门

目录 专栏导读 1网络爬虫概述 1.1 工作原理 1.2 应用场景 1.3 爬虫策略 1.4 爬虫的挑战 2 网络爬虫开发 2.1 通用的网络爬虫基本流程 2.2 网络爬虫的常用技术 2.3 网络爬虫常用的第三方库 3 简单爬虫示例 专栏导读 专栏订阅地址&#xff1a;https://blog.csdn.net/…

提高 Snowflake 工作效率的 6 大工具

推荐&#xff1a;使用 NSDT场景编辑器 助你快速搭建可二次编辑的3D应用场景 Snowflake 彻底改变了企业存储、处理和分析数据的方式&#xff0c;提供了无与伦比的灵活性、可扩展性和性能。但是&#xff0c;与任何强大的技术一样&#xff0c;要真正利用其潜力&#xff0c;必须拥有…

vsCode使用cuda

一、vsCode使用cuda 前情提要&#xff1a;配置好mingw&#xff1a; 1.安装cuda 参考&#xff1a; **CUDA Toolkit安装教程&#xff08;Windows&#xff09;&#xff1a;**https://blog.csdn.net/qq_42951560/article/details/116131410 2.在vscode中添加includePath c_cp…

VS2015打开Qt的pro项目文件 报错

QT报错&#xff1a;Project ERROR: msvc-version.conf loaded but QMAKE_MSC_VER isn‘t set 解决方法&#xff1a; 找到本机安装的QT路径&#xff0c;找到“msvc-version.conf”文件&#xff0c;用记事本打开&#xff0c; 在其中添加版本“QMAKE_MSC_VER 1900”保存即可。 …

2023-8-18 二进制中1的个数

题目链接&#xff1a;二进制中1的个数 #include <iostream>using namespace std;int lowbit(int x) {return x&-x; }int main() {int n;cin >> n;for(int i 0; i < n; i){int x;cin >> x;int res 0;while(x) x - lowbit(x), res;cout << re…

sql:知识点记录一

1.Mysql逻辑架构&#xff1a;连接层、服务层、引擎层、存储层 2.show engines&#xff1a;查看存储引擎 3.Mysql两种存储引擎的区别&#xff1a; 建立索引&#xff1a;比如说用户很喜欢用name去查询表&#xff0c;就可以给数据库的name字段建立索引&#xff0c;提高查询效率&a…

stm32开关控制led灯泡(附Proteus电路图)

说明&#xff1a;我的灯泡工作电压2V&#xff0c;电流设置为10um,注意了不是10毫安时微安啊&#xff0c;要不然电流太小亮不起来的。 2&#xff1a;我用的开关不是按钮button而是switch, 3&#xff1a;PB0,PB1默认都是低电平&#xff0c;采用了PULLDOWN模式&#xff0c;如果设…