forked from Harrison1/unrealcpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChangeMaterialMesh.cpp
More file actions
53 lines (40 loc) · 1.81 KB
/
ChangeMaterialMesh.cpp
File metadata and controls
53 lines (40 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// Harrison McGuire
// UE4 Version 4.20.2
// https://github.com/Harrison1/unrealcpp
// https://severallevels.io
// https://harrisonmcguire.com
//helpful links
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UMeshComponent/SetMaterial/index.html
// https://docs.unrealengine.com/latest/INT/API/Runtime/Engine/Components/UPrimitiveComponent/OnComponentBeginOverlap/
#include "ChangeMaterialMesh.h"
// include draw debug helpers header file and the box component
#include "DrawDebugHelpers.h"
#include "Components/BoxComponent.h"
// Sets default values
AChangeMaterialMesh::AChangeMaterialMesh()
{
MyMesh = CreateDefaultSubobject<UStaticMeshComponent>(TEXT("My Mesh"));
MyMesh->SetupAttachment(RootComponent);
RootComponent = MyMesh;
MyBoxComponent = CreateDefaultSubobject<UBoxComponent>(TEXT("My BoxComponent"));
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);
}
// Called when the game starts or when spawned
void AChangeMaterialMesh::BeginPlay()
{
Super::BeginPlay();
DrawDebugBox(GetWorld(), GetActorLocation(), FVector(100,100,100), FColor::White, true, -1, 0, 10);
MyMesh->SetMaterial(0, OffMaterial);
}
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);
}
}