UE4 network优化

工具

https://medium.com/neobards/ue4-network-profiler-1-fa8a22372d46

这里面是总结使用UE4 Replicate系统进行优化

首先介绍一些调试命令

net.Reliable.Debug 2 查看Reliable 的RPC函数(具体看函数发起在哪 如果是客户端发起调用,那么只能在客户端查看到该命令的rpc,以下命令也是这种情况)

net.ListActorChannels 查看同步的actor

net.RPC.Debug 1 查看RPC函数调用

netprofile 切换停止/开始录制net profiler数据

1.设置Actor的更新频率,对于一些不需要频繁更新状态的actor可以将频率设置的很低,当需要执行具体联网操作时调用ForceNetUpdate进行更新,例如一扇门,只有打开关闭的时候 调用ForceNetUpdate(注意不要使用Reliable的rpc函数,如果使用的话是会无视更新频率的)

2.设置NetCull距离 注意 这里面的数值是距离的平方 也就是说默认的距离实际是15000(150m)

3.使用NetworkProfiler查看网络信息

工具的位置在引擎Engine\Binaries\DotNET  查看的数据通过netprofile命令获得位置在工程/Saved\Profiling

如果没有特殊设置的 一般默认的17777是客户端 61358是作为服务器端

4.设置Actor的Net Dormancy属性

如果设置为Initial的时候 actor不进行复制 可以通过蓝图SetNetDormancy修改该值,经过测试 设置为其他任何值都会进行复制, 同样有FlushNetDormancy进行强制刷新(应对NetUpdateFrequency比较低的情况)

5.就是参照NetSerialization.h中的说明 自己创建需要复制的struct 并进行序列化

可以手动确定哪些变量进行复制

USTRUCT(BlueprintType)
struct FTestA
{
	GENERATED_USTRUCT_BODY()
public:
	UPROPERTY(BlueprintReadWrite)
		int a;
	UPROPERTY(BlueprintReadWrite)
		int b;
	/**
	 * @param Ar			FArchive to read or write from.
	 * @param Map			PackageMap used to resolve references to UObject*
	 * @param bOutSuccess	return value to signify if the serialization was succesfull (if false, an error will be logged by the calling function)
	 *
	 * @return return true if the serialization was fully mapped. If false, the property will be considered 'dirty' and will replicate again on the next update.
	 *	This is needed for UActor* properties. If an actor's Actorchannel is not fully mapped, properties referencing it must stay dirty.
	 *	Note that UPackageMap::SerializeObject returns false if an object is unmapped. Generally, you will want to return false from your ::NetSerialize
	 *  if you make any calls to ::SerializeObject that return false.
	 *
	*/
	bool NetSerialize(FArchive& Ar, class UPackageMap* Map, bool& bOutSuccess)
	{
		// Your code here!
		Ar << a;
		return false;
	}

	/**
	 * @param DeltaParms	Generic struct of input parameters for delta serialization
	 *
	 * @return return true if the serialization was fully mapped. If false, the property will be considered 'dirty' and will replicate again on the next update.
	 *	This is needed for UActor* properties. If an actor's Actorchannel is not fully mapped, properties referencing it must stay dirty.
	 *	Note that UPackageMap::SerializeObject returns false if an object is unmapped. Generally, you will want to return false from your ::NetSerialize
	 *  if you make any calls to ::SerializeObject that return false.
	 *
	*/
	bool NetDeltaSerialize(FNetDeltaSerializeInfo& DeltaParms)
	{
		// Your code here!
		return false;
	}
};

template<>
struct TStructOpsTypeTraits< FTestA > : public TStructOpsTypeTraitsBase2< FTestA >
{
	enum
	{
		WithNetSerializer = true,
		WithNetDeltaSerializer = false,
	};
};
发布了144 篇原创文章 · 获赞 15 · 访问量 9万+

猜你喜欢

转载自blog.csdn.net/maxiaosheng521/article/details/94446332