资源分类
蓝图类资源:也就是BlueprintClass,继承于UObject并且蓝图化的资源。
非蓝图类资源:UTexture,UStaticMesh,UParticleSystem,UMaterialInterface这些资源:如纹理,粒子,静态网格,材质等等
同步加载
LoadClass(同步加载蓝图类资源为UClass*)
//注意蓝图类资源路径后缀要加 _C,否则加载出来的无法使用
FString filePath = "/Game/StarterContent/Blueprints/Blueprint_Effect_Fire.Blueprint_Effect_Fire_C";
UClass* pClass = LoadClass<AActor>(this, *filePath);
if (pClass)
{
UE_LOG(LogTemp, Warning, TEXT("UClass name is %s"), *pClass->GetName());
}
LoadObject(同步加载非蓝图类资源)
FString filePath1 = "/Game/StarterContent/Materials/M_AssetPlatform.M_AssetPlatform";
UMaterial* material = LoadObject<UMaterial>(this, *filePath1);
if (material)
{
UE_LOG(LogTemp, Warning, TEXT("UClass name is %s"), *material->GetName());
}
FStreamableManager streamable;
FString filePath2 = "/Game/StarterContent/Blueprints/Blueprint_Effect_Fire.Blueprint_Effect_Fire_C";
UClass* pClass1 = streamable.LoadSynchronous<UClass>(FSoftObjectPath(filePath2));
if (pClass1)
{
UE_LOG(LogTemp, Warning, TEXT("UClass name is %s"), *pClass1->GetName());
}
LoadSynchronous(同步加载非蓝图类资源)
FStreamableManager streamable1;
FString filePath3 = "/Game/StarterContent/Materials/M_AssetPlatform.M_AssetPlatform";
UMaterial* material1 = streamable1.LoadSynchronous<UMaterial>(FSoftObjectPath(filePath3));
if (material1)
{
UE_LOG(LogTemp, Warning, TEXT("UClass name is %s"), *material1->GetName());
}
void AMyProject7Character::BeginPlay()
{
Super::BeginPlay();
FStreamableManager streamableManager;
FString strBPClassPath = "/Game/testActor.testActor_C";
FStreamableDelegate streamableDelegate;
FSoftClassPath SoftBPClassPathName = FSoftClassPath(strBPClassPath);
streamableDelegate.BindUObject(this, &ThisClass::LoadFinish, SoftBPClassPathName);
streamableManager.RequestAsyncLoad(SoftBPClassPathName, streamableDelegate);
}
void AMyProject7Character::LoadFinish(FSoftClassPath SoftBPClassPathName)
{
TSoftClassPtr<AActor> ActorClassPtr = TSoftClassPtr<AActor>(SoftBPClassPathName);
UClass* pClass = ActorClassPtr.Get();
if (pClass)
{
UE_LOG(LogTemp, Error, TEXT("UStaicMesh name is %s"), *pClass->GetName());
}
}
RequestAsyncLoad(异步加载非蓝图类资源)
void ATestLoadObjectCharacter::BeginPlay()
{
Super::BeginPlay();
FStreamableManager streamableManager;
FString strMeshFileName = "/Game/Geometry/Meshes/1M_Cube.1M_Cube";
FStreamableDelegate streamableDelegate;
FSoftObjectPath strMeshObjectFileName = FSoftObjectPath(strMeshFileName);
streamableDelegate.BindUObject(this, &ThisClass::LoadFinish, strMeshObjectFileName);
streamableManager.RequestAsyncLoad(strMeshObjectFileName, streamableDelegate);
}
void ATestLoadObjectCharacter::LoadFinish(FSoftObjectPath meshFilePath)
{
FSoftObjectPtr meshObjectPtr = FSoftObjectPtr(meshFilePath);
UObject* pObject = meshObjectPtr.Get();
if (nullptr == pObject)
return;
UStaticMesh* pStaticMesh = Cast<UStaticMesh>(pObject);
if (pStaticMesh)
{
UE_LOG(LogTemp, Error, TEXT("UStaicMesh name is %s"), *pStaticMesh->GetName());
}
}