zoukankan      html  css  js  c++  java
  • gameUnity 网络游戏框架

    常常在想,有没有好的方式,让开发变得简单,让团队合作更加容易。

    于是,某一天 动手写一个 架构, 目前版本 暂定 0.1 版本。(unity5.0.0f4 版本以上)

    我打算 开源出来

    0.1有什么功能?

    首先类结构图

    前台包括:事件类,视图逻辑类,单例数据类

    后台包括:私聊,公聊,人机通信

    如何二次开发?

    参考view 文件夹。

    举例

    FamilyView.cs

    FamilyCommand.cs

    using UnityEngine;
    using System.Collections;
    using System;
    using System.ComponentModel;
    using WebSocket4Net;
    using LitJson;
    using UnityEngine.UI;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    /// <summary>
    /// 主界面
    /// </summary>
    public class FamilyView : FamilyCommand
    {
        public Queue<JsonData> JsonList;
        public Transform Button; //绑定一个按钮,推荐(单层界面中使用)
        public Transform Button1; //绑定一个按钮,推荐(单层界面中使用)* 注意如果是列表,那么必须每个item一个view类
        public Transform Button2; //绑定一个按钮,推荐(单层界面中使用)* 注意如果是列表,那么必须每个item一个view类
         
        void Start()
        { 
            JsonList = GameModel.getInstance().JsonList;
            addlistener();//添加视图层监听
            addListenerSocket();//添加服务器监听
             
            FamilyView.Get(Button.gameObject).onClick = ButtonClick;
            FamilyView.Get(Button1.gameObject).onClick = Button1Click;
            FamilyView.Get(Button2.gameObject).onClick = Button2Click;
    
        }
    
        //弹出 换装系统
        private void Button2Click(GameObject obj)
        {
            SendWeapon(this);
        }
    
    
        //ugui 事件测试  按钮点击
        private void ButtonClick(GameObject obj)
        {
            //Despawner(gameObject, 0.1f);//删除战斗界面 可以自动删除监听
            SendEvent(this);
        }
        //ugui 事件测试  按钮点击 
        private void Button1Click(GameObject obj)
        {
            SendSocketEvent(this);
        }
    
        // Update is called once per frame
        void Update()
        {
            //通过队列,获得服务器数据
            if (JsonList.Count > 0)
            {
                JsonData mes = JsonList.Dequeue();
                SplitDisposal(mes);//拿到数据,处理 ,并删除队列
            }
        }
    
    
        //登录返回的数据
        public override void user_login_return(JsonData jsontext)
        {
            //如果需要 特殊处理  ,比如脏话 变星号 
            base.user_login_return(jsontext);
            
            print(jsontext.ToJson()); 
            JsonList.Enqueue(jsontext);// 加入队列
        }
    
    
        //回调服务器事件  聊天信息(测试)
        public override void user_chat_return(JsonData jsontext)
        {
            //如果需要 特殊处理  ,比如脏话 变星号 
            base.user_chat_return(jsontext);
            print(jsontext["username"] + "== 异步得到服务器数据,但unity只能同步赋值===" + jsontext["for"]);
            JsonList.Enqueue(jsontext);// 加入队列
        }
    
        //回调视图 事件
        public override void ActionHandle(JsonData jsontext)
        {
            print("委托事件得来的数据:" + jsontext["Agreement"] + "" + jsontext["username"] + "" + jsontext["message"]);
        }
    
    
        void OnDestroy()
        {
            removelistener();//删除视图监听
            removeListenerSocket();//删除服务器监听
        }
    }
    using UnityEngine;
    using System.Collections;
    using System;
    using System.ComponentModel;
    using WebSocket4Net;
    using LitJson;
    using UnityEngine.UI;
    using System.Collections.Generic;
    using System.Text.RegularExpressions;
    
    public class FamilyCommand : SocketEvent
    {
    
        //中英文 字符识别 
        public void Character_processing(string str)
        {
            Regex regChina = new Regex("^[^x00-xFF]");
            if (regChina.IsMatch(str))
            {
                print("中文");
            }
            else
            {
                print("其他");
            }
        }
    
        //服务端返回后,数据赋值这里处理
        public void SplitDisposal(JsonData jsontext)
        {
            String Agreementstr = jsontext["Agreement"].ToString();
            switch (Agreementstr)
            {
                case Agreement.user_chat_return:
    
                    print(jsontext["username"] + "--服务器数据 同步---------" + jsontext["for"]);
                    break;
                case Agreement.user_login_return://登录
                    //角色基础值
                    OnNameperson(jsontext);
                    //头盔
                    OnHelmet(jsontext);
                    //护肩
                    OnPauldron(jsontext); 
                    break;
                default: break;
            }
        }
        //角色基础值
        public void OnNameperson(JsonData jsontext)
        {
            Dictionary<string, GamePerson> GamePersonDictionary = GameModel.getInstance().GamePersonDictionary; 
            for (int i = 0; i < 5; i++)
            {
                string nameperson = "person" + i;
                string PersonId = jsontext[nameperson]["PersonId"].ToString();
    
                GamePerson gamePerson = new GamePerson();
                gamePerson.Attack = jsontext[nameperson]["Attack"].ToString();
                gamePerson.Defense = jsontext[nameperson]["Defense"].ToString();
                gamePerson.Distance = jsontext[nameperson]["Distance"].ToString();
                gamePerson.Life = jsontext[nameperson]["Life"].ToString();
                gamePerson.Anger = jsontext[nameperson]["Anger"].ToString();
                gamePerson.HelmetId = jsontext[nameperson]["HelmetId"].ToString();
                gamePerson.PauldronId = jsontext[nameperson]["PauldronId"].ToString();  
    
                GamePersonDictionary.Add(PersonId, gamePerson);
            }
            GameModel.getInstance().userplay.Gold = jsontext["Gold"].ToString();
            GameModel.getInstance().userplay.Diamonds = jsontext["Diamonds"].ToString(); 
    
        }
    
        //护肩
        public void OnPauldron(JsonData jsontext)
        {
            for (int i = 0; i < 8; i++)
            {
                string Pauldron = "Pauldron" + i.ToString();
    
               // print(jsontext[Pauldron]["PauldronId"]); //护肩id 
               // print(jsontext[Pauldron]["Attack"] + "  " + jsontext[Pauldron]["Defense"] + "" + jsontext[Pauldron]["Distance"]);
                string MagicGemType = "MagicGemType" + i.ToString();
                for (int j = 0; j < 11; j++)
                {
                 //   print(jsontext[Pauldron][MagicGemType]);//魔法宝石  
                }
            } 
        }
        //头盔
        public void OnHelmet(JsonData jsontext)
        {
            Dictionary<string, WeaponHelmet> WeaponHelmetDictionary = GameModel.getInstance().WeaponHelmetDictionary;
            //头盔
            for (int i = 0; i < 8; i++)
            {
                string Helmet = "Helmet" + i.ToString();
                string HelmetId = jsontext[Helmet]["HelmetId"].ToString();//头盔id  
                string MagicGemType = "MagicGemType" + i.ToString();
    
                WeaponHelmet weaponHelmet = new WeaponHelmet();
                weaponHelmet.Attack = jsontext[Helmet]["Attack"].ToString();
                weaponHelmet.Defense = jsontext[Helmet]["Defense"].ToString();
                weaponHelmet.Distance = jsontext[Helmet]["Distance"].ToString();
    
                for (int j = 0; j < 11; j++)
                {
                    weaponHelmet.MagicGemType1 = jsontext[Helmet][MagicGemType].ToString();
                }
    
                WeaponHelmetDictionary.Add(HelmetId, weaponHelmet);
            }
        }
    
        //弹出换装系统
        public void SendWeapon(FamilyView familyView)
        {
            Spawner("CanvasWeaponScene");
        
        }
    
    
        //(测试)某个unity 事件类
        public void SendEvent(FamilyView familyView)
        {
            //用过 familyView 获得组件上的 数据, 赋给  JsonData
            if (OnDelegateEvent().callbackEvent != null)
            {
                //实例化自定义类和 协议   
                JsonData message = new JsonData();
                message["Agreement"] = "weewewwe";  //这里的值 可以通过actionView 获得,也可以通过GameModel得到
                message["username"] = "36546545454";
                message["message"] = "哈哈哈";
                OnDelegateEvent().callbackEvent(Protocol.Action, message);
            }
        }
        private WebSocket ws;
        public void SendSocketEvent(FamilyView familyView)
        {
            ws = GameModel.getInstance().socketModel.websocket;
            JsonData message = new JsonData();
            message["Agreement"] = Agreement.user_public_chat_return;
            message["username"] = GameModel.getInstance().userplay.play_name;
            ws.Send(message.ToJson());//这个是发到消息端  
        }
    }

    前后台 事件和unity内部事件,都是通过协议来 分配的。

    内部事件:DelegateSubEvent类中 分配

    socket事件 :SocketEvent 类中分配

    下一个版本,加入动画事件,以及四叉树 范围监测处理(假设渲染不限制,可以让几千人 智能对战)。

  • 相关阅读:
    Power of Cryptography
    Radar Installation
    Emag eht htiw Em Pleh
    Help Me with the Game
    89. Gray Code
    87. Scramble String
    86. Partition List
    85. Maximal Rectangle
    84. Largest Rectangle in Histogram
    82. Remove Duplicates from Sorted List II
  • 原文地址:https://www.cnblogs.com/big-zhou/p/4559079.html
Copyright © 2011-2022 走看看