目录
4.1.创建蓝图 Gameinstance 并继承 MyGameInstance
2.4.在右侧空白的地方: 右键 —> 点击添加,然后填入如下信息
必要说明:
本文案例仅适用于C++(加蓝图)开发的UE项目
部分代码直接拷贝官方文档的,但是会有一些避坑的修改
一、准备
1.创建一个空的C++项目(随便创建一个空的)
2.准备一个服务器,或者直接在本机上建立服务器也可以(本文使用的是Windows server 2019)
二、项目
1.打开插件
2.添加模块到 xxx(项目名字).Build.cs 文件
PrivateDependencyModuleNames.AddRange(
new string[]
{
"ChunkDownloader"
});
3.创建GameInstance(C++类)
//本文直接使用 MyGameInstance 命名
MyGameInstance实现
.h头文件
// Fill out your copyright notice in the Description page of Project Settings.
#pragma once
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MyGameInstance.generated.h"
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FPatchCompleteDelegate, bool, Succeeded);
DECLARE_DYNAMIC_MULTICAST_DELEGATE_OneParam(FManifestUpdateDelegate, bool, Succeeded);
UCLASS()
class FILEPAKTES_API UMyGameInstance : public UGameInstance
{
GENERATED_BODY()
public:
/** Overrides */
virtual void Init() override;
virtual void Shutdown() override;
void OnManifestUpdateComplete(bool bSuccess);
//获取下载和安装进度
UFUNCTION(BlueprintPure, Category = "Patching|Stats")
void GetLoadingProgress(int32& BytesDownloaded, int32& TotalBytesToDownload, float& DownloadPercent, int32& ChunksMounted, int32& TotalChunksToMount, float& MountPercent) const;
/**启动游戏补丁过程。如果补丁清单不是最新的,则返回false。*/
UFUNCTION(BlueprintCallable, Category = "Patching")
bool PatchGame();
protected:
/**文件块下载过程完成时调用*/
void OnDownloadComplete(bool bSuccess);
/**ChunkDownloader加载模式完成时调用*/
void OnLoadingModeComplete(bool bSuccess);
/**ChunkDownloader完成挂载文件块时调用*/
void OnMountComplete(bool bSuccess);
public:
/**补丁过程成功或失败时触发*/
UPROPERTY(BlueprintAssignable, Category = "Patching");
FPatchCompleteDelegate OnPatchComplete;
//开始下载时触发
UPROPERTY(BlueprintAssignable, Category = "Patching");
FManifestUpdateDelegate OnManifestUpdateComp;
/**要尝试和下载的文件块ID列表*/
UPROPERTY(EditDefaultsOnly, Category = "Patching")
TArray<int32> ChunkDownloadList;
protected:
//Tracks Whether or not our local manifest file is up to date with the one hosted on our website
bool bIsDownloadManifestUpToDate;
};
.cpp文件
#include "MyGameInstance.h"
#include "ChunkDownloader.h"
#include "Misc/CoreDelegates.h"
#include "AssetRegistryModule.h"
void UMyGameInstance::Init()
{
Super::Init();
const FString DeploymentName = "PatchingDemoLive";
//这里是可以配置的,作为版本号使用
const FString ContentBuildId = "PatchingKey";
// initialize the chunk downloader
TSharedRef<FChunkDownloader> Downloader = FChunkDownloader::GetOrCreate();
Downloader->Initialize("Windows", 8);
// load the cached build ID
Downloader->LoadCachedBuild(DeploymentName);
//注意,这里官方文档是有个坑的
TFunction<void(bool bSuccess)> UpdateCompleteCallback = [&](bool bSuccess) { OnManifestUpdateComplete(bSuccess); };
Downloader->UpdateBuild(DeploymentName, ContentBuildId, UpdateCompleteCallback);
}
void UMyGameInstance::Shutdown()
{
Super::Shutdown();
// Shut down ChunkDownloader
FChunkDownloader::Shutdown();
}
void UMyGameInstance::OnManifestUpdateComplete(bool bSuccess)
{
bIsDownloadManifestUpToDate = bSuccess;
if (bIsDownloadManifestUpToDate)
{
UE_LOG(LogTemp, Display, TEXT("Patch Game to download file pak."));
PatchGame();
}
OnManifestUpdateComp.Broadcast(bSuccess);
}
//注意,这里官方文档,头文件与cpp文件的参数名字不对
void UMyGameInstance::GetLoadingProgress(int32& BytesDownloaded, int32& TotalBytesToDownload, float& DownloadPercent, int32& ChunksMounted, int32& TotalChunksToMount, float& MountPercent) const
{
//获取ChunkDownloader的引用
TSharedRef<FChunkDownloader> Downloader = FChunkDownloader::GetChecked();
//获取加载统计结构体
FChunkDownloader::FStats LoadingStats = Downloader->GetL