静态方法:
对资源的软引用:
FSoftObjectPath MeshSoftRef("StaticMesh'/Game/StarterContent/Props/SM_Door.SM_Door'");
?
对类的软引用:
FSoftClassPath bpwidgetcalss(TEXT("Blueprint'/Game/bp_widget.bp_widget_C'"));
实例: FSoftClassPath bpwidgetcalss(TEXT("Blueprint'/Game/bp_widget.bp_widget_C'")); UClass* widgetclass = bpwidgetcalss.TryLoadClass<UUserWidget>(); bpwidget = widgetclass;
使用ConstructorHelpers::FClassFinder的方法可以找到特定的蓝图类
ConstructorHelpers::FClassFinder<AActor> MyCat(TEXT("Blueprint'/Game/bp/bp_Cat.bp_Cat_C'"));
TSubclassOf<AActor> ChildType;
ChildType = MyCat.Class;
?
使用LoadClass函数可以加载一个蓝图类
UClass* StaticMeshClass = LoadClass<AActor>(nullptr, TEXT("Blueprint'/Game/Test/BP_StaticMesh.BP_StaticMesh_C'"));
?
动态引用的方法:
LoadObject<T>的函数可以加载一个资源
UStaticMesh* StaticMesh = LoadObject<UStaticMesh>(nullptr, TEXT("StaticMesh'/Game/StarterContent/Props/SM_Door.SM_Door'"));
?
LoadClass<T>的函数可以加载一个蓝图类
UClass* TestClass = LoadClass<AActor>(nullptr, TEXT("Blueprint'/Game/Test/BP_Test.BP_Test_C'"));
动态的软引用方法:
FSoftObjectPath可以动态软引用一个资源
FSoftObjectPath MeshSoftRef("StaticMesh'/Game/StarterContent/Props/SM_Door.SM_Door'");
FSoftClassPath可以动态软引用一个蓝图类
FSoftClassPath ClassSoftRef("Blueprint'/Game/Blueprint/BP_Test.BP_Test_C'");
?
TSoftObjectPtr<T>可以对任何UObject的子类进行软引用
TSoftObjectPtr<UTexture2D> TextureSoftRef = TSoftObjectPtr<UTexture2D>(FSoftObjectPath("Texture2D'/Game/Textures/MyTexture.MyTexture'"));
?
动态创建组件的方法:
UStaticMeshComponent* SMC = NewObject<UStaticMeshComponent>(this);
SMC->SetStaticMesh(SM);
SMC->AttachToComponent(GetRootComponent(), FAttachmentTransformRules::KeepRelativeTransform);
SMC->RegisterComponent();
?