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

歡迎訪問 生活随笔!

生活随笔

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

c/c++

UE4 C++:Interface接口

發布時間:2023/12/9 c/c++ 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 UE4 C++:Interface接口 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

這部分官方文檔寫的很明白,建議直接去看官方文檔:

虛幻引擎中的接口 | 虛幻引擎5.0文檔 (unrealengine.com)

概述:

  • 簡單的說,接口提供一組公共的方法,不同的對象中繼承這些方法可以有不同的具體實現
  • 任何使用接口的類都必須實現這些接口
  • 實現解耦
  • 解決多繼承的問題

C++使用接口

定義接口

聲明藍圖可調用接口函數

  • 用UFUNCTION宏BlueprintCallable聲明藍圖可調用,還必須使用BlueprintImplementableEvent或BluepeintNativeEvent說明,而且函數不能為虛函數
  • 通過聲明virtual虛函數,是的派生類可重載

代碼

ReactToTriggerInterface.h

#pragma once #include "CoreMinimal.h" #include "UObject/Interface.h" #include "ReactToTriggerInterface.generated.h"// 無需更改 // UINTERFACE類不是實際的接口;它是一個空白類,它的存在只是為了向虛幻引擎反射系統確保可見性。 UINTERFACE(MinimalAPI) class UReactToTriggerInterface : public UInterface {GENERATED_BODY() };//開頭字母"U"必須改為"I"。 class TIPS_API IReactToTriggerInterface {GENERATED_BODY()public:// 純虛函數,實現類必須實現接口virtual void ReactToTrigger_PureVirtual() = 0;// 虛函數,在接口本身的 .h 或 .cpp 文件中提供默認實現.實現類可覆蓋virtual void ReactToTrigger_Virtual();//實現類可以在藍圖和C++中實現接口UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category="Trigger Reaction")void ReactToTrigger_NativeEvent1(int32 number);//實現類可以在藍圖和C++中實現接口UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Trigger Reaction")bool ReactToTrigger_NativeEvent2(int32 number);//實現類在藍圖中實現接口UFUNCTION(BlueprintCallable, BlueprintImplementableEvent, Category = "Trigger Reaction")void ReactToTrigger_ImplementableEvent(); };

ReactToTriggerInterface.cpp

#include "ReactToTriggerInterface.h" void IReactToTriggerInterface::ReactToTrigger_Virtual() {// unimplemented(); //該宏將在執行代碼行時發出調試語句, 中斷UE_LOG(LogTemp, Warning, TEXT("ReactToTrigger_Virtual 被調用, From 接口本身")); }

實現接口

MyDrone.h

#include "ReactToTriggerInterface.h" #include "MyDrone.generated.h"UCLASS() class TIPS_API AMyDrone : public ADrone, public IReactToTriggerInterface {GENERATED_BODY()public:virtual void ReactToTrigger_PureVirtual() override;//可藍圖調用,貌似通用寫法UFUNCTION(BlueprintCallable, BlueprintNativeEvent, Category = "Trigger Reaction")void ReactToTrigger_NativeEvent1(int32 number);virtual void ReactToTrigger_NativeEvent1_Implementation(int32 number) override;//藍圖可調用,,貌似和上面沒區別virtual bool ReactToTrigger_NativeEvent2_Implementation(int32 number) override;//void ReactToTrigger_ImplementableEvent();需要在藍圖實現 };

MyDrone.cpp

#include "MyDrone.h"void AMyDrone::ReactToTrigger_PureVirtual() {UE_LOG(LogTemp, Warning, TEXT("ReactToTrigger_PureVirtual 被調用, From MyDrone")); }void AMyDrone::ReactToTrigger_NativeEvent1_Implementation(int32 number) {UE_LOG(LogTemp, Warning, TEXT("ReactToTrigger_NativeEvent1 被調用, From MyDrone")); }bool AMyDrone::ReactToTrigger_NativeEvent2_Implementation(int32 number) {UE_LOG(LogTemp, Warning, TEXT("ReactToTrigger_NativeEvent2 被調用, From MyDrone"));return true; }

虛幻引擎中的接口 | 虛幻引擎5.0文檔 (unrealengine.com)

總結

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

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