Game
2022年9月22日大约 2 分钟
Game
提示
- 2022/9/22 对于游戏引擎文档的编写
介绍
- 最早接触的游戏启蒙左右年份阶段
- 2009
游戏引擎
- 2021/6/
Godot
Unity
Cocos
游戏素材
搜集一些游戏素材网站
国外
通用素材
NES
国内
准备使用的素材
游戏服务器框架
IoGameUnity
- WebSocket
using System;
using System.Collections;
using System.Collections.Generic;
using Google.Protobuf;
using UnityEngine;
using UnityEngine.SceneManagement;
using UnityWebSocket;
/// <summary>
/// socket可以和 ExternalMessage 类进行捆绑使用
/// </summary>
public class SocketClient : MonoBehaviour{
[Header("退出游戏面板")]
public GameObject quitGamePanel;
public static SocketClient v;
public static readonly string address = "ws://192.168.1.3:10100/websocket";
private static WebSocket socket;
private void Awake(){
if (v != null){
Destroy(gameObject);
return;
}
v = this;
Connect();
DontDestroyOnLoad(this);
//注册函数
Init();
StartCoroutine(TryConnect());
}
private void Init(){ //注册回调
UserAction.Init();
ChatAction.Init();
GameAction.Init();
HeroAction.Init();
}
IEnumerator TryConnect(){ //尝试重新链接
while (true){
yield return new WaitForSeconds (5);
if (socket.ReadyState != WebSocketState.Open){
Connect();
}
}
}
public void QuitGame(){
socket.CloseAsync();
Application.Quit();
}
private void Update(){
if (Input.GetKeyDown(KeyCode.Escape) ){
quitGamePanel.SetActive(true);
AnimationUtil.DoScaleAnim(quitGamePanel.transform.GetChild(0).gameObject);
}
}
private static void OnError(object sender, ErrorEventArgs e){//异常回调
}
private static void OnMessage(object sender, MessageEventArgs e){ //消息回调
if (e.IsBinary){
HandleManager.ParseMessage(e.RawData);
}
}
private static void OnClose(object sender, CloseEventArgs e){//关闭回调
}
private static void OnOpen(object sender, OpenEventArgs e){//打开回调
Debug.Log("链接服务器成功!");
}
/** m里面做了null判断 */
public static void Send(int cmd, int subCmd, IMessage m = null){//发送数据
socket.SendAsync(HandleManager.BuildMessage(cmd, subCmd, m));
}
public static void Connect(){
socket = new WebSocket(address);
// 注册回调
socket.OnOpen += OnOpen;
socket.OnClose += OnClose;
socket.OnMessage += OnMessage;
socket.OnError += OnError;
socket.ConnectAsync();
}
}
- HandleManager
using System;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Google.Protobuf;
using UnityEngine;
/// <summary>
/// 一个通用项目对象,
/// </summary>
public static class HandleManager{ //回调处理
//---------------------------发送处理
private static readonly Dictionary< int , EventHandler<Execute> > handlers = new();
public class Execute : EventArgs{ // 事件参数
/** 状态码 */
public readonly int responseStatus;
/** 返回数据 */
public readonly ByteString data;
internal Execute(ByteString data,int responseStatus){
this.data = data;
this.responseStatus = responseStatus;
}
}
public static void AddHandler(int cmd,int subCmd, EventHandler<Execute> IHandler){ // 注册回调
handlers.Add(GetMergeCmd(cmd,subCmd),IHandler);
}
private static void PackageHandler( int mergeCmd, ExternalMessage data ) { //分发消息
try{
EventHandler<Execute> handler = handlers[mergeCmd]; //对于路由不存在的错误处理
if (handler != null) handler.Invoke(handler, new Execute(data.DataContent,data.ResponseStatus));
}
catch (Exception e){
Debug.Log("不存在的路由: "+GetCmd(data.CmdMerge)+"-"+GetSubCmd(data.CmdMerge));
Console.WriteLine(e);
throw;
}
}
public static void ParseMessage(byte[] bytes){ //解析消息
ExternalMessage message = new ExternalMessage();
message.MergeFrom(bytes);
//Debug.Log(message);
PackageHandler(message.CmdMerge,message);
}
//---------------------------路由命令处理
public static int GetCmd(int merge) {//获取cmd
return merge >> 16;
}
public static int GetSubCmd(int merge) {//获取subCmd
return merge & 0xFFFF;
}
public static int GetMergeCmd(int cmd, int subCmd) { //获取mergeCmd
return (cmd << 16) + subCmd;
}
//---------------------------封装发送结果处理
public static byte[] BuildMessage(int cmd,int subCmd,IMessage v = null){ // 封装消息发送
ByteString data = ByteString.Empty;
if (v != null){
data = v.ToByteString();
}
var message = new ExternalMessage{
CmdMerge = GetMergeCmd(cmd, subCmd),
DataContent = data,
ProtocolSwitch = 0,
CmdCode = 1
};
return message.ToByteArray();
}
}