什么是软引用
创建Actor,命名为MySoftActor
软引用的声明
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SoftPath")
FSoftObjectPath softObjPath;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SoftPath")
FSoftClassPath softClsPath;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SoftPath")
TSoftObjectPtr<AActor> softObjPtr;
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "SoftPath")
TSoftClassPtr<AActor> softClsPtr;
声明完之后,进行编译,并生成蓝图,蓝图类,以及实例中就增加了对应属性
加入头文件,并在Actor的BeginPlay方法中实现同步和异步加载
#include "Engine/AssetManager.h"
void AMySoftActor::BeginPlay()
{
Super::BeginPlay();
softObjPath = TEXT("/Script/Engine.Texture2D'/Game/StarterContent/Textures/T_Brick_Clay_Beveled_M.T_Brick_Clay_Beveled_M'");
//同步加载
TSharedPtr<FStreamableHandle> SyncStreambleHandle = UAssetManager::GetStreamableManager().RequestSyncLoad(softObjPath);
if (SyncStreambleHandle) {
UTexture2D* image = Cast<UTexture2D>(SyncStreambleHandle->GetLoadedAsset());
if (image) {
GEngine->AddOnScreenDebugMessage(-1,5.0f,FColor::Red,FString::Printf(TEXT("image name is %s"), *image->GetName()));
}
}
//异步加载
FString Path = TEXT("/Script/Engine.Blueprint'/Game/StarterContent/Blueprints/Blueprint_CeilingLight.Blueprint_CeilingLight_C'");
softClsPath = Path;
TSharedPtr<FStreamableHandle> ASyncstreambleHandle = UAssetManager::GetStreamableManager().RequestAsyncLoad(softClsPath);
if (ASyncstreambleHandle) {
AActor *actor = Cast<AActor>(ASyncstreambleHandle->GetLoadedAsset());
if (actor) {
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("actor name is %s"), *actor->GetName()));
}
else {
GEngine->AddOnScreenDebugMessage(-1, 5.0f, FColor::Red, FString::Printf(TEXT("actor load error!")));
}
}
}
因篇幅问题不能全部显示,请点此查看更多更全内容