Skip to content

Instantly share code, notes, and snippets.

@PapeCoding
Last active March 10, 2020 10:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PapeCoding/bfaee39e9baa0344b074e697a6110465 to your computer and use it in GitHub Desktop.
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
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