【UE4 基础知识】一些相关宏:Core、UBT、平台

Core 宏

主要用于反射

  • UCLASS()

  • USTRUCT()

  • UENUM()

  • UPROPERTY()

  • UFUNCTION()

  • UMETA()

  • UPARAM()

  • GENERATED_BODY() 和 GENERATED_UCLASS_BODY() 的区别 ( from UE4 C++ UCLASS构造函数易出错分析 )

    • GENERATED_BODY() (现在一般都用这个了,另外一个属于比较老的版本)
      如果定义的是GENERATED_BODY(),那么意味着我不需要使用父类的构造函数,也就是说,我不能直接使用父类的声明,但是,我需要去实现的时候,我就必须自己去声明,否则就会报错。

      UCLASS()
      class MYCHARACTER_API AMyCharacter : public ACharacter
      {
          GENERATED_BODY()
      public:
          AMyCharacter(const FObjectInitializer& ObjectInitializer);    
      };
      
      //如果不去声明自己的构造函数,就会报错: error C2084
      AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer)
          : Super(ObjectInitializer)
      {}  
      
    • GENERATED_UCLASS_BODY()
      如果定义的是GENERATED_UCLASS_BODY(),那么意味着我使用父类的构造函数,也就是说,我不需要为自己声明构造函数,直接去实现父类声明那个构造函数。

      UCLASS()
      class MYCHARACTER_API AMyCharacter : public ACharacter
      {
          GENERATED_UCLASS_BODY()
      };
      
      AMyCharacter::AMyCharacter(const FObjectInitializer& ObjectInitializer)
          : Super(ObjectInitializer)
      {}
      
      // 如果还去声明自己的构造函数,就会报错: error C2535:
      UCLASS()
      class MYCHARACTER_API AMyCharacter : public ACharacter
      {
          GENERATED_UCLASS_BODY()       
      public:
          AMyCharacter(const FObjectInitializer& ObjectInitializer);
      };
      
  • 可查看 Engine\Source\Runtime\CoreUObject\Public\UObject\ObjectMacros.h

  • 本文原创链接


UBT 相关

  • WITH_EDIROT (cpp)

    #if WITH_EDITOR    //是否在用编辑器编译
    ...
    #endif
    
    if(GIsEdior)  // GIsEdior 全局变量,是否再编辑(PIE)模式下运行
    
  • WITH_EDITORONLY_DATA (.h 反射需要)
    包裹 UPROPERTY()、UFunction()

    #if WITH_EDITORONLY_DATA 
    UPROPERTY()
        FString ActorName;
    #endif
    
  • 所有 \Engine\Source\Runtime\Core\Public\Misc\Build.h

    /*----------------------------------------------------------------------------
    	Mandatory bridge options coming from UBT, do not modify directly!
    ----------------------------------------------------------------------------*/
    
    /**
     * Whether we are compiling with the editor; must be defined by UBT
     */
    #ifndef WITH_EDITOR
    	#define WITH_EDITOR	0 // for auto-complete
    	#error UBT should always define WITH_EDITOR to be 0 or 1
    #endif
    
    /**
     * Whether we are compiling with the engine; must be defined by UBT
     */
    #ifndef WITH_ENGINE
    	#define WITH_ENGINE	0 // for auto-complete
    	#error UBT should always define WITH_ENGINE to be 0 or 1
    #endif
    

运行模式相关

  • 使用实例

    #if UE_BUILD_SHIPPING
     // do mobile specific stuff
    #endif
    
  • \Engine\Source\Runtime\Core\Public\Misc\Build.h

    /*--------------------------------------------------------------------------------
    	Build configuration coming from UBT, do not modify
    --------------------------------------------------------------------------------*/
    
    // Set any configuration not defined by UBT to zero
    #ifndef UE_BUILD_DEBUG
    	#define UE_BUILD_DEBUG				0
    #endif
    #ifndef UE_BUILD_DEVELOPMENT
    	#define UE_BUILD_DEVELOPMENT		0
    #endif
    #ifndef UE_BUILD_TEST
    	#define UE_BUILD_TEST				0
    #endif
    #ifndef UE_BUILD_SHIPPING
    	#define UE_BUILD_SHIPPING			0
    #endif
    #ifndef UE_GAME
    	#define UE_GAME						0
    #endif
    #ifndef UE_EDITOR
    	#define UE_EDITOR					0
    #endif
    #ifndef UE_BUILD_SHIPPING_WITH_EDITOR
    	#define UE_BUILD_SHIPPING_WITH_EDITOR 0
    #endif
    #ifndef UE_BUILD_DOCS
    	#define UE_BUILD_DOCS				0
    #endif
    
    /** 
     *   Whether compiling for dedicated server or not.
     */
    #ifndef UE_SERVER
    	#define UE_SERVER					0
    #endif
    

平台相关

  • 使用示例

    #if PLATFORM_IOS || PLATFORM_ANDROID
     // do mobile specific stuff
    #endif
    
    // Runtime/Core/Public/HAL/PlatformProcess.h
    #if PLATFORM_WINDOWS
    #include "Windows/WindowsPlatformProcess.h"
    #elif PLATFORM_PS4
    #include "PS4/PS4Process.h"
    #elif PLATFORM_XBOXONE
    #include "XboxOne/XboxOneProcess.h"
    #elif PLATFORM_MAC
    #include "Mac/MacPlatformProcess.h"
    #elif PLATFORM_IOS
    #include "IOS/IOSPlatformProcess.h"
    #elif PLATFORM_ANDROID
    #include "Android/AndroidProcess.h"
    #elif PLATFORM_HTML5
    #include "HTML5/HTML5PlatformProcess.h"
    #elif PLATFORM_LINUX
    #include "Linux/LinuxPlatformProcess.h"
    #elif PLATFORM_SWITCH
    #include "Switch/SwitchPlatformProcess.h"
    #endif
    
  • 平台宏定义 \Engine\Source\Runtime\Core\Public\HAL\Platform.h

    // define all other platforms to be zero
    //@port Define the platform here to be zero when compiling for other platforms
    #if !defined(PLATFORM_WINDOWS)
    	#define PLATFORM_WINDOWS 0
    #endif
    #if !defined(PLATFORM_XBOXONE)
    	#define PLATFORM_XBOXONE 0
    #endif
    #if !defined(PLATFORM_MAC)
    	#define PLATFORM_MAC 0
    	// If PLATFORM_MAC is defined these will be set appropriately in
    	// MacPlatform.h
    	#define PLATFORM_MAC_X86 0
    	#define PLATFORM_MAC_ARM64 0
    #endif
    #if !defined(PLATFORM_PS4)
    	#define PLATFORM_PS4 0
    #endif
    #if !defined(PLATFORM_IOS)
    	#define PLATFORM_IOS 0
    #endif
    #if !defined(PLATFORM_TVOS)
    	#define PLATFORM_TVOS 0
    #endif
    #if !defined(PLATFORM_ANDROID)
    	#define PLATFORM_ANDROID 0
    #endif
    #if !defined(PLATFORM_ANDROID_ARM)
    	#define PLATFORM_ANDROID_ARM 0
    #endif
    #if !defined(PLATFORM_ANDROID_ARM64)
    	#define PLATFORM_ANDROID_ARM64 0
    #endif
    #if !defined(PLATFORM_ANDROID_X86)
    	#define PLATFORM_ANDROID_X86 0
    #endif
    #if !defined(PLATFORM_ANDROID_X64)
    	#define PLATFORM_ANDROID_X64 0
    #endif
    #if !defined(PLATFORM_ANDROID_VULKAN)
    	#define PLATFORM_ANDROID_VULKAN 0
    #endif
    #if !defined(PLATFORM_ANDROIDGL4)
    	#define PLATFORM_ANDROIDGL4 0
    #endif
    #if !defined(PLATFORM_LUMINGL4)
    	#define PLATFORM_LUMINGL4 0
    #endif
    #if !defined(PLATFORM_LUMIN)
    	#define PLATFORM_LUMIN 0
    #endif
    #if !defined(PLATFORM_APPLE)
    	#define PLATFORM_APPLE 0
    #endif
    #if !defined(PLATFORM_LINUX)
    	#define PLATFORM_LINUX 0
    #endif
    #if !defined(PLATFORM_LINUXAARCH64)
    	#define PLATFORM_LINUXAARCH64 0
    #endif
    #if !defined(PLATFORM_SWITCH)
    	#define PLATFORM_SWITCH 0
    #endif
    #if !defined(PLATFORM_FREEBSD)
    	#define PLATFORM_FREEBSD 0
    #endif
    #if !defined(PLATFORM_UNIX)
    	#define PLATFORM_UNIX 0
    #endif
    #if !defined(PLATFORM_MICROSOFT)
    	#define PLATFORM_MICROSOFT 0
    #endif
    #if !defined(PLATFORM_HOLOLENS)
    #define PLATFORM_HOLOLENS 0
    #endif
    

参考

posted @ 2021-11-04 14:11  砥才人  阅读(3547)  评论(0编辑  收藏  举报