使用Unreal引擎制作游戏或VR产品的时候,需要遵循Unreal引擎已经约定好的框架;
Unreal引擎非常复杂,本人是基于Application的应用层面来使用Unreal封装好的功能;
在制作一款联网端游的时候遇到了网络的问题,特此记录一下,个人的经验分享!
https://docs.unrealengine.com/en-US/InteractiveExperiences/Networking/index.html
#1 Unreal 的框架简介
#2 Unreal 中的网络框架
#3 Unreal 属性同步,蓝图和C++
#4 Unreal 属性同步,Notify方式
#5 Unreal 中基于Actor同步
#6 Unreal 中使用RPC同步
#1 Unreal 的框架简介
Unreal中默认使用的框架在GameMode的设置中列举出来了
-
GameModeOverride
游戏模式
The GameModeBase defines the game being played. It governs the game rules, scoring, what actors
are allowed to exist in this game type, and who may enter the game.
It is only instanced on the server and will never exist on the client.
A GameModeBase actor is instantiated when the level is initialized for gameplay in C++ UGameEngine::LoadMap().
The class of this GameMode actor is determined by (in order) either the URL ?game=xxx,
the GameMode Override value set in the World Settings, or the DefaultGameMode entry set in the game’s Project Settings.
简介:每个关卡都可以设置一个游戏模式
作用:设置游戏的规则
网游:只存在于服务器中
单机:每个关卡都有 -
Default Pawn Class
默认的控制
Pawn is the base class of all actors that can be possessed by players or AI.
They are the physical representations of players and creatures in a level.
简介:游戏默认的操作控制
作用:封装好了游戏的各种控制
网游:由服务器复制到各个客户端
单机:自由控制切换 -
HUD Class
游戏UI
Base class of the heads-up display. This has a canvas and a debug canvas on which primitives can be drawn. It also contains a list of simple hit boxes that can be used for simple item click detection.
A method of rendering debug text is also included.Provides some simple methods for rendering text, textures, rectangles and materials which can also be accessed from blueprints.
简介:游戏战斗时的UI
作用:可以绘制各种UI,血条、伤害数字等
网游:和服务没有关系
单机:自由控制显示 -
Player Controller Class
玩家控制器
PlayerControllers are used by human players to control Pawns.
ControlRotation (accessed via GetControlRotation()), determines the aiming
orientation of the controlled Pawn.
In networked games, PlayerControllers exist on the server for every player-controlled pawn,
and also on the controlling client’s machine. They do NOT exist on a client’s
machine for pawns controlled by remote players elsewhere on the network.
简介:游戏玩家的控制器
作用:设置游戏的输入
网游:服务器及客户端同步
单机:每个关卡都有 -
Game State Class
游戏状态
GameState is a subclass of GameStateBase that behaves like a multiplayer match-based game.
It is tied to functionality in GameMode.
简介:游戏进行的状态
作用:管理当前游戏的状态
网游:服务器及客户端同步
单机:每个关卡都有 -
Player State Class
玩家状态
A PlayerState is created for every player on a server (or in a standalone game).
PlayerStates are replicated to all clients, and contain network game relevant information about the player, such as playername, score, etc.
简介:游戏玩家的状态
作用:管理当前玩家的状态
网游:服务器及客户端同步
单机:每个关卡都有 -
Spectator Class
观战控制
SpectatorPawns are simple pawns that can fly around the world, used by
PlayerControllers when in the spectator state.
简介:游戏玩家观战
作用:控制当前玩家的观战
网游:和服务没有关系
单机:一般来说不需要
#2 Unreal 中的网络框架
Unreal中的服务器分为两种:监听服务器(局域网对战)和专用服务器(独立云服务器),两种服务器的游戏在制作的过程中差不多,打包的方式不一样;
制作测试的时候可以使用Unreal提供的便捷功能:
可以设置测试开启的玩家数量和网络模式
- Play Offline 单机运行
- Play As Listen Server 监听服务器
- Play As Client 专用服务器(默认会开启一个服务器)
其中学习的时候使用监听服务器,可以区别服务器和客户端,使用HasAuthority()
方法;
项目使用专用服务器,开启的全是客户端。
#3 Unreal 属性同步,蓝图和C++
这种同步的属性无论是否有变化,数据一直在同步,比较耗网络资源
- 蓝图同步设置,基于Actor中的属性(必须要到Actor层级的继承才有网络同步)
- 创建的属性需要设置为复制,表示这个变量是Server所有权
- 蓝图测试,Actor和关卡蓝图,Replicated的变量有特殊标志
- 关卡蓝图测试
以下是C++中使用属性复制功能: - 创建C++ Actor类
// HowTo_ReplicateProps.h
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "HowTo_ReplicateProps.generated.h"
UCLASS()
class TUTORIALSDEMO_API AHowTo_ReplicateProps : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AHowTo_ReplicateProps();
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
//Step1. 标记为Replicated