• UE4C++3 物体挨打后来回换色


    #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"));
        }
    
    
    }
  • 相关阅读:
    在基础数据类型和在对象中 使用 == 与equals的差别
    对Java中静态代理和动态代理的简单理解
    MyBatis中sqlSession的创建及执行流程
    Spring相关概念
    AOP相关
    IOC容器和Bean的配置
    如何将本地项目上传到GitHub进行托管
    稀疏数组
    Java集合回忆
    JSTL标签库
  • 原文地址:https://www.cnblogs.com/tilyougogannbare666/p/14014723.html
Copyright © 2020-2023  润新知