Last active
March 10, 2020 10:30
-
-
Save PapeCoding/bfaee39e9baa0344b074e697a6110465 to your computer and use it in GitHub Desktop.
This shows how an Asset can be loaded from C++. Here a Plane and a corresponding Material is loaded
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
template <typename T> | |
bool LoadAsset(const FString& Path, T* & Result) | |
{ | |
ConstructorHelpers::FObjectFinder<T> Loader(*Path); | |
Result = Loader.Object; | |
if (!Loader.Succeeded()) UE_LOG(LogTemp, Error, TEXT("Could not find %s. Have you renamed it?"), *Path); | |
return Loader.Succeeded(); | |
} | |
AMyClass::AMyClass() | |
{ | |
UStaticMesh* PlaneMesh = nullptr; | |
UMaterial* StripesMaterial = nullptr; | |
// Loading of Blueprint/Meshes/Materials | |
// In this case, the plugin contains a StaticMesh named "Plane" and a material named "Stripes" | |
LoadAsset("/MyPlugin/Plane", PlaneMesh); | |
LoadAsset("/MyPlugin/Stripes", StripesMaterial); | |
// Creation of sub-components | |
USceneComponent* Root = CreateDefaultSubobject<USceneComponent>("DefaultSceneRoot"); | |
SetRootComponent(Root); | |
//Declare Result in Header file | |
Result = CreateDefaultSubobject<UStaticMeshComponent>("MyVeryOwnPlane"); | |
Result->SetStaticMesh(PlaneMesh); | |
Result->SetupAttachment(Root); | |
} | |
// Bind Materials in PostInitializeComponents, else it can have weird side effects | |
void AMyClass::PostInitializeComponents() | |
{ | |
Super::PostInitializeComponents(); | |
Result->SetMaterial(0, StripesMaterial); | |
//Can be dynamic material as well | |
//UMaterialInstanceDynamic* MyDynamicMaterial = UMaterialInstanceDynamic::Create(StripesMaterial, Result); | |
//Result->SetMaterial(0, MyDynamicMaterial); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment