#include "CoreMinimal.h" #include "Engine/StaticMeshActor.h" #include "TargetStaticMeshActor.generated.h" /** * h中的文件 */ UCLASS() class LEAMCPP_API ATargetStaticMeshActor : public AStaticMeshActor { GENERATED_BODY() public: UPROPERTY(EditDefaultsOnly, Category = "TargetSetting")//定义两个材质,用于来回切换 UMaterialInterface* TargetRed; UPROPERTY(EditDefaultsOnly, Category = "TargetSetting") UMaterialInterface* TBlue; ATargetStaticMeshActor(); virtual void NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) override; };
// Fill out your copyright notice in the Description page of Project Settings. //cpp里的文件 #include "TargetStaticMeshActor.h" #include "Engine/Engine.h" #include "LeamCppProjectile.h" ATargetStaticMeshActor::ATargetStaticMeshActor() { } static bool Ifc=true;//定义一个全局变量用于检测颜色以及换色 void ATargetStaticMeshActor::NotifyHit(class UPrimitiveComponent* MyComp, AActor* Other, class UPrimitiveComponent* OtherComp, bool bSelfMoved, FVector HitLocation, FVector HitNormal, FVector NormalImpulse, const FHitResult& Hit) { Super::NotifyHit(MyComp, Other, OtherComp, bSelfMoved, HitLocation, HitNormal, NormalImpulse, Hit); if (Other == nullptr) return; ALeamCppProjectile* projectile = Cast<ALeamCppProjectile>(Other); if (projectile != nullptr) { class UStaticMeshComponent* staticMesh = GetStaticMeshComponent(); if (Ifc) {//如果颜色为a,那么换成b staticMesh->SetMaterial(0, TargetRed); Ifc = false; } else {//如果颜色为b,换成a staticMesh->SetMaterial(0, TBlue); Ifc = true; } if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("ALeamCppProjectile")); } } else { if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotALeamCppProjectile")); } } if (GEngine) { GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, TEXT("NotifyHit")); } }