\

The original tutorial is based on UE 4.18, I am based on UE 4.25.

English original Address

In this tutorial, we will change the material of the static grid when an overlapping event occurs. First, create a new Actor subclass, which I’ll call Changematerian Mesh in this tutorial.

First, in the.h file, we will create a UStaticMeshComponent, two UMaterial classes, and a UBoxComponent. Add the element to the public part of the header file.

.UPROPERTY(VisibleAnywhere)
class UStaticMeshComponent* MyMesh;

UPROPERTY(EditAnywhere)
class UMaterial* OnMaterial;

UPROPERTY(EditAnywhere)
class UMaterial* OffMaterial;

UPROPERTY(a)class UBoxComponent* MyBoxComponent;

UFUNCTION(a)void OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult);
Copy the code

Now let’s go to the.cpp file. First of all, at the top contains DrawDebugHelpers. H and the Components/BoxComponent. H file, so that we can use our visualization and collision.

// include draw debug helpers header file
#include "DrawDebugHelpers.h"
#include "Components/BoxComponent.h"
Copy the code

Next, we will set the constructor and set the default values. Create a static grid with CreateDefaultSubobject and set it to RootComponent. Then, create the box component with CreateDefaultSubobject, and we will set its size range to FVector(100,100,100) by using InitBoxExtent. The box component will be initialized as a collision profile named Trigger that will be attached to the RootComponent. Next, create a grid switch between the two materials, establish a default value for bool, and finally join the overlap function.

Here is the constructor code.

AChangeMaterialMesh::AChangeMaterialMesh()
{
 	// Set this actor to call Tick() every frame. You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;

	MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("MyMesh"));
	RootComponent = MyMesh;

	MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("MyBoxComponent"));
	MyBoxComponent->InitBoxExtent(FVector(100.100.100));
	MyBoxComponent->SetCollisionProfileName("Trigger");
	MyBoxComponent->SetupAttachment(RootComponent);

	OnMaterial = CreateDefaultSubobject<UMaterial>(TEXT("OnMaterial"));
	OffMaterial = CreateDefaultSubobject<UMaterial>(TEXT("OffMaterial"));

	MyBoxComponent->OnComponentBeginOverlap.AddDynamic(this, &AChangeMaterialMesh::OnOverlapBegin);

}
Copy the code

Next, in the BeginPlay() method, we will draw the debugbox with DrawDebugBox and set the first material of the grid with SetMaterial. Here is the BeginPlay() code.

void AChangeMaterialMesh::BeginPlay(a)
{
	Super::BeginPlay(a);DrawDebugBox(GetWorld(), GetActorLocation(), FVector(100.100.100), FColor::White, true.- 1.0.10);

	MyMesh->SetMaterial(0, OffMaterial);
	
}
Copy the code

Now we will create an overlap function to change the material of the mesh. We’ll check if OtherActor is not empty, OtherActor is not the same actor, and OtherComp is not empty. If all goes through, we will call SetMaterial and pass in the new material and set it to the first material of the grid.

The following is the overlapping function.

void AChangeMaterialMesh::OnOverlapBegin(class UPrimitiveComponent* OverlappedComp, class AActor* OtherActor, class UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult) 
{
	if( (OtherActor ! =nullptr) && (OtherActor ! =this) && ( OtherComp ! =nullptr ) ) 
	{
		MyMesh->SetMaterial(0, OnMaterial); }}Copy the code

Compile the code. Drag and drop the actor into the game world. Add a grid to the details panel and add two materials to the Actor that are set in the details panel of the parent component (instance). Now when you press Play, the mesh will change material when it overlaps.

The renderings are as follows