Skip to content

Instantly share code, notes, and snippets.

@SkylakeOfficial
Created June 26, 2023 17:40
Show Gist options
  • Select an option

  • Save SkylakeOfficial/9e4bdeef25d49e23929571ab3e41a5df to your computer and use it in GitHub Desktop.

Select an option

Save SkylakeOfficial/9e4bdeef25d49e23929571ab3e41a5df to your computer and use it in GitHub Desktop.
A UnrealEngine 5 runtime level streaming manager.
#include "AFPStreamedRoomManager.h"
#include "Engine/LevelStreamingDynamic.h"
#include "Kismet/KismetArrayLibrary.h"
#include "Kismet/KismetMathLibrary.h"
#include "Kismet/GameplayStatics.h"
#include "Math/Color.h"
// Sets default values
AAFPStreamedRoomManager::AAFPStreamedRoomManager()
{
// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
PrimaryActorTick.bCanEverTick = false;
SceneRoot = CreateDefaultSubobject<USceneComponent>(TEXT("Root Comp"));
SetRootComponent(SceneRoot);
}
void AAFPStreamedRoomManager::StreamLevelByIndex(int32 LevelIndex)//��index�������͹ؿ�
{
if (LevelIndex < LevelsGenerated.Num()) {
GetWorld()->SetMapNeedsLightingFullyRebuilt(0, 0);
LevelsGenerated[LevelIndex]->SetShouldBeLoaded(true);
LevelsGenerated[LevelIndex]->SetShouldBeVisible(true);
GetWorld()->UpdateStreamingLevelShouldBeConsidered(LevelsGenerated[LevelIndex]);
}
}
void AAFPStreamedRoomManager::UnloadLevelByIndex(int32 LevelIndex)//��indexж�����͹ؿ�
{
if (LevelIndex < LevelsGenerated.Num()) {
LevelsGenerated[LevelIndex]->SetShouldBeLoaded(false);
LevelsGenerated[LevelIndex]->SetShouldBeVisible(false);
GetWorld()->UpdateStreamingLevelShouldBeConsidered(LevelsGenerated[LevelIndex]);
}
}
// Called when the game starts or when spawned
void AAFPStreamedRoomManager::BeginPlay()
{
Super::BeginPlay();
if (!LevelSlots.IsEmpty()) {
for (int i = 0; i != LevelSlots.Num(); i++)
{
//��ؿ��б������޹ؿ����������ɲ�����
if (LevelsToStream.IsEmpty()) {
UE_LOG(LogTemp, Error, TEXT("Levels to load is less than Slots to load!"));
break;
}
//���ɴ����͵Ĺؿ�
ULevelStreaming* tmpLevelToStream = NewObject<ULevelStreaming>(GetWorld(), ULevelStreamingDynamic::StaticClass(), NAME_None, RF_Transient, NULL);
//���Ѱ�����͹ؿ�
int32 Index = UKismetMathLibrary::RandomIntegerInRange(0, LevelsToStream.Num() - 1);
FName packageName = LevelsToStream[Index];
LevelsToStream.RemoveAt(Index);//�����ظ����ɣ��ʴ��б���ȥ��
//ParamSet
tmpLevelToStream->SetWorldAssetByPackageName(packageName);
tmpLevelToStream->LevelColor = FLinearColor::MakeRandomColor();
tmpLevelToStream->SetShouldBeLoaded(false);
tmpLevelToStream->SetShouldBeVisible(false);
tmpLevelToStream->LevelTransform = LevelSlots[i];
LevelsGenerated.Add(tmpLevelToStream);
}
GetWorld()->AddStreamingLevels(LevelsGenerated);
}
}
// Called every frame
void AAFPStreamedRoomManager::Tick(float DeltaTime)
{
Super::Tick(DeltaTime);
}
#pragma once
#include "CoreMinimal.h"
#include "GameFramework/Actor.h"
#include "AFPStreamedRoomManager.generated.h"
UCLASS()
class ARKNIGHTSFP_API AAFPStreamedRoomManager : public AActor
{
GENERATED_BODY()
public:
// Sets default values for this actor's properties
AAFPStreamedRoomManager();
UPROPERTY(EditAnywhere, Category = "StreamedLevels")
TArray<FName> LevelsToStream;
UPROPERTY(EditAnywhere, Category = "StreamedLevels", BlueprintReadWrite, Meta = (MakeEditWidget = true))
TArray<FTransform> LevelSlots;
UPROPERTY(BlueprintReadOnly, Category = "StreamedLevels")
TArray<ULevelStreaming *> LevelsGenerated;
UFUNCTION(BlueprintCallable, Category = "StreamedLevels")
void StreamLevelByIndex(int32 LevelIndex);
UFUNCTION(BlueprintCallable, Category = "StreamedLevels")
void UnloadLevelByIndex(int32 LevelIndex);
UPROPERTY(VisibleAnywhere)
USceneComponent* SceneRoot;
protected:
// Called when the game starts or when spawned
virtual void BeginPlay() override;
public:
// Called every frame
virtual void Tick(float DeltaTime) override;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment