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 類中分配

 

下一個版本,加入動畫事件,以及四叉樹 范圍監測處理(假設渲染不限制,可以讓幾千人 智能對戰)。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM