日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > c/c++ >内容正文

c/c++

UE4 实现C++蓝图接口

發(fā)布時間:2023/12/20 c/c++ 61 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UE4 实现C++蓝图接口 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

藍圖接口和C++、Java中的接口類似,都是用來被繼承的,并且可以實現(xiàn)相應的功能。

  • 使用UE新建一個第一人稱游戲來實現(xiàn)接口實例:TestInterface

    2. 新建一個C++的接口類:HittedInterface

  • 打開VS2019編輯C++代碼
  • // Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include "UObject/Interface.h" #include "HittedInterface.generated.h"// This class does not need to be modified. /** class UHittedInterface 這個類是為了在UE中使用反射系統(tǒng),不需要處理 */ UINTERFACE(MinimalAPI) class UHittedInterface : public UInterface {GENERATED_BODY() };/****/ class TESTINTERFACE_API IHittedInterface {GENERATED_BODY()// Add interface functions to this class. This is the class that will be inherited to implement this interface. public:/**BlueprintNativeEvent除了實現(xiàn)C++調用藍圖外,同樣會調用一個本地方法,本地方法的實現(xiàn)為 聲明的函數(shù)名+_Implementation在實現(xiàn)中應該是:void hitByProjectile_Implementation()override;這樣就可以實現(xiàn)一次調用,兩個實現(xiàn)(c++實現(xiàn),藍圖實現(xiàn))*/UFUNCTION(BlueprintNativeEvent, BlueprintCallable)void hitByProjectile(); }; // Fill out your copyright notice in the Description page of Project Settings. #include "HittedInterface.h" // Add default functionality here for any IHittedInterface functions that are not pure virtual.
  • 新建一個Actord類HittedActor繼承自接口IHittedInterface

  • // Fill out your copyright notice in the Description page of Project Settings.#pragma once#include "CoreMinimal.h" #include "HittedInterface.h" #include "GameFramework/Actor.h" #include "HittedActor.generated.h"UCLASS() class TESTINTERFACE_API AHittedActor : public AActor, public IHittedInterface {GENERATED_BODY()public:// Sets default values for this actor's propertiesAHittedActor();protected:// Called when the game starts or when spawnedvirtual void BeginPlay() override;public:// Called every framevirtual void Tick(float DeltaTime) override;public:void hitByProjectile_Implementation()override;}; // Fill out your copyright notice in the Description page of Project Settings.#include "HittedActor.h" #include "Kismet/KismetSystemLibrary.h"// Sets default values AHittedActor::AHittedActor() {// 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;}// Called when the game starts or when spawned void AHittedActor::BeginPlay() {Super::BeginPlay();}// Called every frame void AHittedActor::Tick(float DeltaTime) {Super::Tick(DeltaTime);}void AHittedActor::hitByProjectile_Implementation() {//在HUD界面顯示內容GEngine->AddOnScreenDebugMessage(-1, 2.0, FColor::Red, UKismetSystemLibrary::GetDisplayName(this) + TEXT(" hitting...")); }
  • 在子彈擊中后調用這個函數(shù),在類ATestInterfaceProjectile中實現(xiàn)
  • void ATestInterfaceProjectile::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit) {//Actor被子彈擊中后,調用接口IHittedInterface* hitedActor = Cast<IHittedInterface>(OtherActor);if (hitedActor) {hitedActor->Execute_hitByProjectile(OtherActor);}// Only add impulse and destroy projectile if we hit a physicsif ((OtherActor != nullptr) && (OtherActor != this) && (OtherComp != nullptr) && OtherComp->IsSimulatingPhysics()) {OtherComp->AddImpulseAtLocation(GetVelocity() * 100.0f, GetActorLocation());Destroy();} }
  • 在UEEditor 中創(chuàng)建AHittedActor的藍圖類BP_HittedActor
  • 在藍圖中添加一個立方體

    保存以后,把BP_HittedActor拖動到場景中

    可以在跑【類設置】中查看接口繼承情況

  • 運行程序

    aaa
  • 總結

    以上是生活随笔為你收集整理的UE4 实现C++蓝图接口的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。