UCLASS(Blueprintable, BlueprintType)class USolusDataSingleton : public UObject{ GENERATED_BODY()public: USolusDataSingleton(const FObjectInitializer& ObjectInitializer); UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Solus Data Singleton") TArraySolusTreeBlueprints; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Solus Data Singleton") UTexture2D* SolusT2D; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Solus Data Singleton") FVector SolusEssentialVector; UPROPERTY(EditAnywhere, BlueprintReadWrite, Category="Solus Data Singleton") FString SolusCoreFilePath;};
SolusDataSingleton.cpp
#include "Solus.h"#include "SolusDataSingleton.h"USolusDataSingleton::USolusDataSingleton(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){ SolusCoreFilePath = "E:/Solus"; SolusEssentialVector = FVector(9000,0,0);}
Create The BP_ClassXXX of the CPP Class
Project Settings->Engine->General, 将Game Singleton Class设置为BP_ClassXXX
Easily Set Default Values Any Time!
Function Library To Access Anywhere in BP
SolusDataSingletonLibrary.h
#pragma once //Data Singleton Class#include "SolusDataSingleton.h" #include "SolusDataSingletonLibrary.generated.h" //note about UBlueprintFunctionLibrary// This class is a base class for any function libraries exposed to blueprints.// Methods in subclasses are expected to be static, and no methods should be added to the base class. UCLASS()class SOLUS_API USolusDataSingletonLibrary : public UBlueprintFunctionLibrary{ GENERATED_BODY()public: USolusDataSingletonLibrary(const FObjectInitializer& ObjectInitializer); UFUNCTION(BlueprintPure, Category="Solus Data Singleton") static USolusDataSingleton* GetSolusData(bool& IsValid); };
SolusDataSingletonLibrary.cpp
#include "Solus.h"#include "SolusDataSingletonLibrary.h"//// USolusDataSingletonLibrary USolusDataSingletonLibrary::USolusDataSingletonLibrary(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer){ } USolusDataSingleton* USolusDataSingletonLibrary::GetSolusData(bool& IsValid){ IsValid = false; USolusDataSingleton* DataInstance = Cast(GEngine->GameSingleton); if(!DataInstance) return NULL; if(!DataInstance->IsValidLowLevel()) return NULL; IsValid = true; return DataInstance;}